Spring:设置语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6434309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:33:37  来源:igfitidea点击:

Spring: Setting up locale

springlocalizationinternationalization

提问by NRaf

I've setup Spring as specified in the following guide: http://www.springbyexample.org/examples/basic-webapp-internationalization-spring-config.html

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

If I was to append ?locale=fr, for example, to the end of an URL the locale will change to French.

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

However, in my case, I want to set the locale when the user logs in as this information is associated with their profile. I've tried to use localeResolver.setLocale(request, response, new Locale("fr")) (where localeResolver is an instance of SessionLocaleResolver) to specify the locale however this doesn't have any effect.

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

Any idea what I'm doing wrong? Am I approaching this issue in the correct way?

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

回答by John29

localeResolver.setLocale works fine for me, try something like this:

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

applicationContext

应用上下文

<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

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

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

my.message=Message (default language)

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

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

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

my.message=Message in English

my.message=英文消息

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

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

my.message=Message in French

my.message=法语消息

Controller

控制器

@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";
    }
}

With this code I get "Message in French", if I change "fr" to "en" I get "Message in English", and without setLocale call I get "Message (default language)". Changing StringUtils.parseLocaleString("fr") to new Locale("fr") gives the same results.

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

回答by blandger

I would recommend try to set up default locale as:

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

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

Some helpful info is in blog post Configuring locale switching with Spring MVC 3.

一些有用的信息在博客文章中使用 Spring MVC 3 配置语言环境切换

回答by Jarek Wichrowski

Example:

例子:

@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;
    }
}

回答by harun ugur

 @Bean
 public LocaleResolver localeResolver() {
     SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
     Locale locale = new Locale("tr", "TR");
     sessionLocaleResolver.setDefaultLocale(locale);
     return sessionLocaleResolver;
 }

回答by bhagyas

You can probably take a look at Spring Roo project. There is an internationalization add on from Spring which is being used in Spring Roo which enables quick switching of Locale within a Spring Web application automatically generated from Roo.

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

回答by Ji?í Vypěd?ík

How do you determine that the locale has not been set? If you expect, the correct locale to be present in HttpServletRequest, this isn't true — its value is handled by the servlet container and is therefore immutable. Instead, you should rely that Spring will inject a proper value to a method parameter with class Localein your controller. Another way to obtain the locale is by using RequestContextUtils.getLocale(HttpServletRequest request)directly.

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