如何在 WAR 之外外部化 Spring MessageSources 包
我必须在类路径之外外部化用于 i18n 支持的 Spring MessageSources 包(属性文件),以便更轻松地修改属性。我怎样才能做到这一点?
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="test-messages"/>
谢谢!
I have to externalize the Spring MessageSources bundle for i18n support (properties files) outside the classpath in order to modify properties more easily. How can I do that ?
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="test-messages"/>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们构建了一个消息源实现,用于在数据库中查找消息。您所要做的就是创建一个继承自 spring 的 AbstractMessageSource 的 MessageSource 实现(为了获得所有功能,请参阅 javadoc)。
您必须至少实现抽象方法“resolveCode(String, Locale)”(但实现“resolveCodeWithoutArguments(String, Locale)”将提高您的性能),该方法委托给指向该简单表的 DAO,其定义如下this:
代码和区域设置形成唯一索引。
你就完成了。当然,您将添加一些缓存功能,并在 dao 或 MessageSource 级别提供“区域设置降级”行为(即,如果未找到“en_US”,请尝试“en”)。
这非常有效。
We have built a message source implementation that looks up messages in the DB. What you have to do is create a MessageSource implementation that inherits from spring's AbstractMessageSource (in order to gain all features, see javadoc).
You have to implement at minimal the abstract method 'resolveCode(String, Locale)' (but implementing 'resolveCodeWithoutArguments(String, Locale)' will increase your performances), which delegates to a DAO pointing to that simple table, with a definition such as this:
code and locale form a unique index.
And you're done. Of course, you will add some cache capabilities, and provide "locale degradation" behaviour (i.e. if "en_US" is not found, try "en"), either at dao- or MessageSource-level.
This works perfectly.
检查此线程 有关此问题的信息,但我认为将文件放在 tomcat 上下文之外并不是一个好习惯,因为您永远不知道它将部署到您的应用程序的位置。
但如果您需要,您会在那里找到一些非常好的解决方案。
Check this thread for information regarding this issue, but I think is not a good practise to have files outside the tomcat context as you never know where it is going to be deployed your application.
But in case you need, you'll find some pretty nice solutions there.