JavaSpringMessageSource似乎忽略了属性fallbackToSystemLocale
我在配置Spring MessageSource以忽略系统区域设置时遇到问题。当我使用null语言环境参数调用getMessage时,我希望MessageSource选择默认属性文件消息。财产。相反,它选择消息。财产。当我将此属性文件的名称更改为messages\u fr.properties时,默认属性文件为choosen。我的系统区域设置为“en”。因此,MessageSource似乎忽略了我设置为false的fallbackToSystemLocale属性
此行为与Spring版本4.1.4和4.1.5相同
消息源配置:
<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>
获取消息:
String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);
谢谢你的建议
# 1 楼答案
fallbackToSystemLocale
不是当您使用locale=null
调用消息源时,它的作用是控制消息源的行为fallbackToSystemLocale
控制当您请求一条对于所请求的本地语言不存在的消息(代码)时要做什么——要么是因为该语言根本没有消息属性文件,要么只是因为消息文件不包含消息代码另一方面,当用
locale=null
(messageSource.getMessage("key", null);
)调用getMessage
时,locale将由Locale.getDefault
设置org。springframework。上下文支持AbstractMessageSource:
在考虑
fallbackToSystemLocale
属性之前因此,最简单的黑客(它不是Workaround,它是黑客)将使用一种您不支持的语言,而不是
null
:messageSource.getMessage("key", new Locale("XX"));