【问题标题】:using client machine locale for i18n使用 i18n 的客户端机器语言环境
【发布时间】:2013-06-03 06:29:08
【问题描述】:

我正在尝试根据客户端计算机的操作系统区域设置我的应用程序工作。现在它适用于服务器机器的语言环境。我正在使用字符串框架工作。 Apache Tomcat 7 用作服务器。这是我使用的配置。任何帮助都会得到帮助。

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

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


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


<!--     Register the welcome.properties -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
                    <list>
                        <value>i18n.api/api</value>
                        <value>i18n.exceptions/exceptions</value> 
                        <value>i18n.common/common</value>   
                        <value>i18n.login/login</value>
                        <value>i18n.plan/plan</value>
                        <value>i18n.customer/customer</value>
                        <value>org.springframework.security.messages</value> 
                        <value>org.hibernate.validator.ValidationMessages</value> 
                    </list>
                </property>
    </bean>

【问题讨论】:

  • “让我的应用程序根据客户端机器的操作系统区域设置工作”是什么意思? (任何?)客户应该有什么行为?

标签: java spring internationalization locale


【解决方案1】:

请参阅supported handler method argument types on Spring doc。您可以像这样在处理程序方法中注入用户的区域设置:

@RequestMapping("/home")
public String home(Locale userLocale) {
    // do something with userLocale

    return "home";
}

语言环境的类型是 java.util.Locale。

还可以查看ServletRequestgetLocale() 方法。客户端必须在其请求中提供 Accept-Language 标头,否则使用服务器的语言环境。我假设 Spring 的行为方式与此相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    相关资源
    最近更新 更多