Java Spring MessageSource似乎忽略了属性fallbackToSystemLocale
我在配置Spring MessageSource以忽略系统区域设置时遇到问题。当我使用null语言环境参数调用getMessage时,我希望MessageSource选择默认属性文件messages.properties。相反,它选择messages\u en.properties。当我将此属性文件的名称更改为messages\u fr.properties时,默认属性文件为choosen。我的系统区域设置为“en”。因此,MessageSource似乎忽略了我设置为false的fallbackToSystemLocale属性 此行为与Spring版本4.1.4和4.1.5相同 消息源配置:Java Spring MessageSource似乎忽略了属性fallbackToSystemLocale,java,spring,spring-java-config,Java,Spring,Spring Java Config,我在配置Spring MessageSource以忽略系统区域设置时遇到问题。当我使用null语言环境参数调用getMessage时,我希望MessageSource选择默认属性文件messages.properties。相反,它选择messages\u en.properties。当我将此属性文件的名称更改为messages\u fr.properties时,默认属性文件为choosen。我的系统区域设置为“en”。因此,MessageSource似乎忽略了我设置为false的fallback
<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>
谢谢你的建议
fallbackToSystemLocale
不是当您使用locale=null调用消息源时,用于指导消息源所做的操作
fallbackToSystemLocale
控制当您请求一条对于所请求的本地语言不存在的消息(代码)时要执行的操作,这可能是因为该语言没有消息属性文件,或者只是因为消息文件不包含消息代码
另一方面,当您使用locale=null
(messageSource.getMessage(“key”,null);
)调用getMessage
时,locale将由locale.getDefault
设置
org.springframework.context.support.AbstractMessageSource:
在考虑fallbackToSystemLocale
属性之前
因此,最简单的黑客-around(它不是workaround,它是黑客)将使用您不支持的语言,而不是null
:
messageSource.getMessage(“key”,新语言环境(“XX”)代码>谢谢您的回答。不管怎样,你不觉得这种行为不合逻辑吗?我希望在提供空语言环境的情况下使用默认消息属性文件。如果我想使用本地系统,我会做一些类似于messageSource.getMessage(“key”,locale==null?locale.getDefault():locale)的事情代码>。i、 Wicket框架在我写作时解决了这个问题,对我来说似乎更好。我可能会扩展ResourceBundleMessageSource
类并重写getMessageInternal
,以反映我的需要。如果未宣布为最终版本,当然:)
String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);
protected String getMessageInternal(String code, Object[] args, Locale locale) {
...
if (locale == null) {
locale = Locale.getDefault();
}
...