我正在尝试设置一些消息捆绑。我有 2 个文件:
message.properties
message_en_US.properties
bean 类:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en_US"/>
</bean>
实现:
msgSrc.getMessage(key, null, Locale.getDefault());
一切正常,但我很困惑为什么很多教程都有那个 message.properties
文件,因为如果我摆脱 localeResolver 并将实现设置为
msgSrc.getMessage(key, null, Locale.ENGLISH);
它仍然查看 messages_en_US,如果去掉 localResolver 并且仍然使用 Locale.getDefault,它会使用我的本地计算机,这又是 en_US。
我可以专门设置message.properties
,但本地变量不起作用。
那么在什么情况下会使用message.properties
呢?
我觉得我缺少一个概念。
(有没有办法设置它,以便如果 Spring 在 messages_en_US.properties
中找不到 key ,它会在 messages.properties
中查找?)
最佳答案
我认为如果 spring 找不到指定语言环境的属性(使用 Locale.getDefault 或从系统获取),那么将使用“message.properties”。因此,您只需删除 messages_en_US.properties
文件,并使用 messages.properties
作为 en_us 和其他区域设置的默认值。
关于java - 默认 ResourceBundleMessageSource 有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146372/