I am trying to localize my Android apps,开发者_如何学C however I found out the I would need a Context()
where ever I have a string. This including some exception and status message in my other objects.
I feel that it is confusing if I need to pass a context to every other objects in my apps just to get a string translated.
Can anyone show me some lights on how to implement this in a less complex ways?
If you have a complex app, maybe even with layers that have nothing to do with user interface stuff, you might think about returning objects instead of strings and use the visitor pattern or some other technique in the user interface layer to create strings from these objects. You should have a Context there. However, this comes at the price of adding possible many new classes.
Instead of passing a Context around, you might instead use a Singleton "ContextProvider" which can be queried for a Context from all around your app. Note however, that this approach might lead to less testable code and "hidden" dependencies compared with the approach of passing a Context in the constructor.
精彩评论