【问题标题】:How to use Spring's i18n mechanism?如何使用 Spring 的 i18n 机制?
【发布时间】:2013-12-17 15:32:41
【问题描述】:

我在我的 Spring 项目中添加了本地化,它似乎正在工作,但我想知道我如何更改语言,如果语言选择是基于浏览器设置、HTTP 标头、cookie 或其他内容完成的。有没有办法明确,例如以类似的方式将语言环境作为参数,例如hl=de 在 HTTP 查询字符串上?我还想允许用户在设置页面上设置语言,我该怎么做?我的实现看起来像这样,并用英文写消息:

<h4 class="title"><fmt:message key="login.title"/></h4>

servlet.xml:

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

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

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

<bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

那我该怎么办

a) 通过启用使用 HTTP GET 参数(例如德语为 hl=de 和法语为 hl=fr )覆盖区域设置来明确选择区域设置?

b) 让用户选择区域设置?

更新

拦截器不工作。 XML 是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <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.SessionLocaleResolver">
    <property name="defaultLocale" value="sv" />
</bean>

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

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="100000"/>
    </bean>

</beans>

【问题讨论】:

    标签: java spring spring-mvc internationalization


    【解决方案1】:

    a) 你定义了一个 id 为 localeChangeInterceptor 的 bean:

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

    此拦截器使您能够使用您在查询字符串中选择的参数(在本例中为“lang”)更改您的语言环境(即:http://mydomain.com/mypage?lang=fr 用于法语)

    b) 您可以使用点 a) 为用户提供更改语言环境的链接 c) 您选择了默认语言环境:“en”。否则使用浏览器语言选择区域设置

    注意:您应该使用&lt;spring:message code="${msg.value}" arguments="${msg.args}"/&gt;作为本地化字符串,而不是 fmt,以便与 spring 进行更多集成...

    【讨论】:

    • 谢谢。但是切换语言不起作用,我不知道为什么。我的代码中一定有一些错误,因为本地化永远不会改变,即使我使用语言环境更改参数lang=fr,它始终是默认值。为什么?
    • 对于我使用的语言环境解析器 bean:&lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"&gt; &lt;property name="defaultLocale" value="en" /&gt; &lt;/bean&gt; 此外,尝试使用:&lt;mvc:interceptors&gt; &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt; &lt;property name="paramName" value="language" /&gt; &lt;/bean&gt; &lt;/mvc:interceptors&gt;
    • @EmanueleRighetto 谢谢,但即使我将?language=sv 放入URL 并在文件messages_sv.propertiesmessages_en.properties 中进行了本地化,我仍然可以获得默认语言。为什么?我将尝试更改默认值并查看它的作用,但除了默认的英文消息之外,我无法获得其他消息。
    • 似乎没有调用拦截器..您可以尝试对其进行子类化并进行一些调试..
    • 尝试覆盖 preHandle(..) 方法并在那里放置调试代码(记得返回 true,否则请求将不会被转发到控制器..)
    【解决方案2】:

    您已经配置了LocaleChangeInterceptor。它的参数paramName(你设置为lang)就是改变locale的请求参数。

    把配置改成hl,然后就可以用这个参数改了:

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

    要让用户改变本地,你只需要在页面上添加一些链接

    <a href="${currentPage}?hl=de">German</a>
    

    @见JavaDoc: LocalChangeInterceptor#setParamName

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2012-05-15
      • 1970-01-01
      • 2016-01-28
      • 2019-09-19
      • 1970-01-01
      相关资源
      最近更新 更多