I'm developing an Android library.
This library has a utility class which contains static functions which include strings in them (mostly for detailed descriptions of various errors).
I want to localize these strings. Therefore, in the library, I've put these strings inside res/values/strings.xml
.
In order to use these strings in my library code, I need to call getString()
but this method requires a Context
. The context MUST be the library context, since the strings are only defined in the library resources.
My research so far
I've searched for similar questions about using string resources inside static functions, but the answers given won't work in my case. The 2 popular answers were:
Pass a context as an argument to the static functions - I think this won't work for me because the code using the utility class is the app using the library (not the library itself). This means the only possible context the caller could pass would be the app's context - but I need the library's context.
Retrieve a context statically by accessing the
Application
object (usually via a singleton) and callinggetApplicationContext()
- I think this won't work for the same reason, the context will be the app's context and not the library's.
Any ideas?
I don't think I'm attempting anything peculiar. Developing a library for sharing and code reuse is perfectly normal. Having static utility functions in this library is perfectly normal. The usage of strings in these functions to describe error conditions is perfectly normal. And to want to localize these strings is normal. Why is this so difficult?!