在 Android 应用程序的对象中本地化状态消息的最佳实践是什么?
我正在尝试本地化我的 Android 应用程序,但是我发现只要有字符串,我就需要一个 Context()
。这包括我的其他对象中的一些异常和状态消息。
我觉得如果我需要将上下文传递给应用程序中的每个其他对象只是为了翻译字符串,那会很混乱。
谁能告诉我如何以不太复杂的方式实现这一点?
I am trying to localize my Android apps, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个复杂的应用程序,甚至可能具有与用户界面内容无关的层,您可能会考虑返回对象而不是字符串,并在用户界面层中使用访问者模式或其他一些技术从这些对象创建字符串。你应该有一个上下文。然而,这是以添加可能的许多新类为代价的。
您可以使用单例“ContextProvider”,而不是传递上下文,可以从应用程序的所有位置查询上下文。但请注意,与在构造函数中传递 Context 的方法相比,此方法可能会导致可测试代码较少和“隐藏”依赖项。
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.