java中的字符串资源文件
用Java编程,创建资源文件来保存不同语言的应用程序消息的更好方法是什么?
如果我使用属性文件,则每次需要在代码中引用消息时,我都必须编写类似的内容:
ResourceBundle.getBundle("test.messages.messageFile").getString("Message1")
存在另一种形式来使用消息并生成代码清理器吗?也许创建一个带有引用此消息的静态常量的类?
Programming in Java, what's the better way to create resource files to save the messages of the application in different languages?
If I use a properties file, inside the code everytime that I need refer to a message I have to write something like that:
ResourceBundle.getBundle("test.messages.messageFile").getString("Message1")
Exists another form to use messages and generate a code cleaner? Maybe create a class with static constants that refer to this messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 ResourceBundle,但声明它并将其用作变量。
如果您将此变量设置为
public static
,将会有所帮助you can use ResourceBundle, but declare it and use it as a variable.
it would help if you make this variable
public static
这是标准方式。但是没有什么会强迫您不将包存储在字段中:
您也可以将其封装到具有以下方法的 Messages 类中:
这完全取决于您。
This is the standard way. But nothing forces you to not store the bundle in a field:
You may also encapsulate it into a Messages class that has the following method:
It's all up to you.
您可以尝试国际化变得更容易。
这是基于注释并为参数替换提供类型安全性。
you can try Internationalization made easier.
This is based on annotations and gives type safety for argument substitution.