Oli*_*tek 5 java spring spring-java-config
我在配置Spring MessageSource以忽略我的系统区域设置时遇到问题.当我使用null locale参数调用getMessage时,我希望我的MessageSource选择默认属性文件messages.properties.相反,它选择messages_en.properties.当我将此属性文件的名称更改为messages_fr.properties时,将选择默认属性文件.我的系统区域设置是'en'.所以似乎MessageSource忽略了我设置为false的fallbackToSystemLocale属性.
此行为与Spring版本4.1.4以及4.1.5相同.
MessageSource配置:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="fallbackToSystemLocale" value="false"></property>
<property name="basenames">
<list>
<value>locale/messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
收到消息:
String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);
Run Code Online (Sandbox Code Playgroud)
谢谢你的建议!
fallbackToSystemLocale并不旨在引导消息源在您调用它们时执行的操作locale=null
fallbackToSystemLocale控制当您请求的消息(代码)对于所请求的本地不存在时要执行的操作 - 要么因为根本没有该语言的消息属性文件,要么只是因为消息文件不包含消息代码
另一方面,当您使用getMessage( locale=null)调用messageSource.getMessage("key", null);时,区域设置将通过以下方式设置Locale.getDefault
org.springframework.context.support.AbstractMessageSource:
protected String getMessageInternal(String code, Object[] args, Locale locale) {
...
if (locale == null) {
locale = Locale.getDefault();
}
...
Run Code Online (Sandbox Code Playgroud)
fallbackToSystemLocale在考虑财产之前。
因此,最简单的hack方案(这不是一个工作方案,而是一个 hack),将使用您不支持的语言而不是null:
messageSource.getMessage("key", new Locale("XX"));
| 归档时间: |
|
| 查看次数: |
3394 次 |
| 最近记录: |