java 类中的 Struts2 i18n
我有一个 Struts2 Web 应用程序,它使用 i18n 属性文件进行本地化。 getText
方法在 jsp 和操作类 getText("some.identifier")
中完美运行。
但是我如何在不是动作类的java类中使用它呢?换句话说,类无法访问 getText
方法。
I have a Struts2 web application that uses an i18n properties file for localization.
The getText
method works perfectly in jsp and in a action class getText("some.identifier")
.
But how can i use it in java-classes that are not an action-class? In other words, classes that do not have access to the getText
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西会读取你的资源包
Something like this will read your resource bundle
您实际上不需要重新加载捆绑包。您可以使用以下代码来访问 Struts 已加载的副本:
请记住,ActionContext 是线程本地的,因此如果您尝试从与处理请求的线程不同的线程调用它,你会遇到错误。
如果您需要将参数传递给本地化消息,则该方法的重载形式采用对象数组作为第三个参数。
You don't actually need to re-load the bundle. You can use the following code to tap into the copy that Struts has loaded:
Keep in mind that
ActionContext
is thread local, so if you attempt to call this from a different thread than the one processing the request, you'll run into an error.An overloaded form of the method takes an object array as the third parameter, if you need to pass arguments to the localized message.
您可以使用ResourceBuldle加载属性文件并获取所需的属性。
You can use ResourceBuldle to load the properties file and get the desired properties.