有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    fallbackToSystemLocale不是当您使用locale=null调用消息源时,它的作用是控制消息源的行为

    fallbackToSystemLocale控制当您请求一条对于所请求的本地语言不存在的消息(代码)时要做什么——要么是因为该语言根本没有消息属性文件,要么只是因为消息文件不包含消息代码

    另一方面,当用locale=nullmessageSource.getMessage("key", null);)调用getMessage时,locale将由Locale.getDefault设置

    org。springframework。上下文支持AbstractMessageSource:

    protected String getMessageInternal(String code, Object[] args, Locale locale) {
        ...
        if (locale == null) {
            locale = Locale.getDefault();
        }
        ...
    

    在考虑fallbackToSystemLocale属性之前

    因此,最简单的黑客(它不是Workaround,它是黑客)将使用一种您不支持的语言,而不是nullmessageSource.getMessage("key", new Locale("XX"));