春天的国际化

时间:2013-10-31 11:25:44

标签: spring internationalization resourcebundle

我有一个现有的Spring / GWT应用程序,我需要添加国际化。我的理解是,我可以使用Spring的“ResourceBundleMessageSource”根据用户位置自动选择适当的messages_ *文件。我试过跟this tutorial但我似乎无法让应用程序用法语显示我的字符串。目前,我已将2个文件messages_en_US.properties和messages_fr_FR.properties添加到我的src / main / resources / i18n文件夹中,并将以下内容添加到applicationContext.XML中:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename">
        <value>classpath:i18n/messages</value>
    </property>
</bean>

<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <property name="interceptors">
       <list>
        <ref bean="localeChangeInterceptor" />
        </list>
    </property>
</bean>

只是想知道1)我是否需要额外的配置/粘合代码和2)如果我可以轻松测试这一点而无需在我的Redhat服务器上将语言/区域设置设置为法语?

2 个答案:

答案 0 :(得分:0)

您的浏览器可能只在Accept-Header中发送“fr”语言标记。 Spring因回退问题而臭名昭着,因此您可能需要将messages_fr_FR.properties复制为messages_fr.properties。

我确信必须有一些方法来配置回退,所以你想要使用messages_en.properties(尝试你的应用程序与其他英语语言环境...),只是messages.properties应该做,但我只是现在太懒惰/累了寻找解决方案。

答案 1 :(得分:0)

这里你需要在spring.xml中指定下面的bean。

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>


    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

    <mvc:interceptors>
     <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    </mvc:interceptors>

在查询字符串中传递 lang = es 时,此功能完全正常。如果仍然存在任何问题。您可以查看工作示例Here