【问题标题】:Spring: Setting up localeSpring:设置语言环境
【发布时间】:2011-09-20 00:43:04
【问题描述】:

我已按照以下指南中的说明设置 Spring:http://www.springbyexample.org/examples/basic-webapp-internationalization-spring-config.html

例如,如果我将 ?locale=fr 附加到 URL 的末尾,则语言环境将更改为法语。

但是,就我而言,我想在用户登录时设置区域设置,因为此信息与他们的个人资料相关联。我尝试使用 localeResolver.setLocale(request, response, new Locale("fr")) (其中 localeResolver 是 SessionLocaleResolver 的一个实例)来指定语言环境,但这没有任何效果。

知道我做错了什么吗?我是否以正确的方式处理这个问题?

【问题讨论】:

    标签: spring localization internationalization


    【解决方案1】:

    localeResolver.setLocale 对我来说很好,试试这样的:

    应用程序上下文

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="messages/messages" p:fallbackToSystemLocale="false" />
    
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
    

    my_page.jsp

    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    <html>
        <body>
            <p><spring:message code="my.message"/></p>  
        </body>
    </html>
    

    \src\main\resources\messages\messages.properties

    my.message=消息(默认语言)

    \src\main\resources\messages\messages_en.properties

    my.message=英文消息

    \src\main\resources\messages\messages_fr.properties

    my.message=法语消息

    控制器

    @Controller
    @RequestMapping("/")
    public class SampleController {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String welcome(HttpServletRequest request, HttpServletResponse response) {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            localeResolver.setLocale(request, response, StringUtils.parseLocaleString("fr"));
            return "my_page";
        }
    }
    

    使用此代码,我得到“法语消息”,如果我将“fr”更改为“en”,我得到“英语消息”,而没有 setLocale 调用,我得到“消息(默认语言)”。将 StringUtils.parseLocaleString("fr") 更改为 new Locale("fr") 会得到相同的结果。

    【讨论】:

      【解决方案2】:

      我建议尝试将默认语言环境设置为:

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

      博客文章Configuring locale switching with Spring MVC 3 中有一些有用的信息。

      【讨论】:

        【解决方案3】:

        例子:

        @Configuration
        public class i18nConfiguration extends WebMvcConfigurerAdapter {
        
            @Bean
            public LocaleResolver localeResolver() {
                SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
                Locale locale = new Locale("fr", "FR");
                sessionLocaleResolver.setDefaultLocale(locale);
                return sessionLocaleResolver;
            }
        }
        

        【讨论】:

          【解决方案4】:

          您可能可以看看 Spring Roo 项目。 Spring Roo 中使用了来自 Spring 的国际化附加组件,它可以在从 Roo 自动生成的 Spring Web 应用程序中快速切换区域设置。

          【讨论】:

            【解决方案5】:

            您如何确定尚未设置区域设置?如果您期望正确的语言环境出现在HttpServletRequest 中,这是不正确的——它的值由 servlet 容器处理,因此是不可变的。相反,您应该相信 Spring 会在您的控制器中为类 Locale 的方法参数注入适当的值。另一种获取语言环境的方法是直接使用RequestContextUtils.getLocale(HttpServletRequest request)

            【讨论】:

            • 我正在尝试以编程方式设置语言环境。消息仍然是英文的。我改用 ?locale=fr 方法,消息现在被加载为法语。
            • 请提供您如何从环境中检索当前用户语言环境的代码
            【解决方案6】:
             @Bean
             public LocaleResolver localeResolver() {
                 SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
                 Locale locale = new Locale("tr", "TR");
                 sessionLocaleResolver.setDefaultLocale(locale);
                 return sessionLocaleResolver;
             }
            

            【讨论】:

              猜你喜欢
              • 2010-09-09
              • 2018-06-13
              • 1970-01-01
              • 2016-09-07
              • 2012-12-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多