Java 如何使用Spring的i18n机制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20638191/
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
How to use Spring's i18n mechanism?
提问by Niklas
I added localization to my Spring project and it appears to be working but I wonder how I change the language, if the language choice is done based on the browser setting, the HTTP header, a cookie or something else. Is there a way to be explicit as well e.g. taking the locale as a parameter in a way like e.g. hl=de
on the HTTP query string? I also want to allow the user to set the language on a settings page, how can I do that? My implementation looks like this and writes messages in English:
我在 Spring 项目中添加了本地化,它似乎正在运行,但我想知道如何更改语言,如果语言选择是基于浏览器设置、HTTP 标头、cookie 或其他内容完成的。有没有一种方法也可以明确,例如将语言环境作为参数,例如hl=de
在 HTTP 查询字符串上?我还想允许用户在设置页面上设置语言,我该怎么做?我的实现看起来像这样并用英语写消息:
<h4 class="title"><fmt:message key="login.title"/></h4>
<h4 class="title"><fmt:message key="login.title"/></h4>
servlet.xml:
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>
So how can I
那我怎么能
a) Make the locale choice explicit by enabling overriding the locale with a HTTP GET parameter such as hl=de
for German and hl=fr
for French?
a) 通过使用 HTTP GET 参数(例如hl=de
德语和hl=fr
法语)覆盖语言环境,使语言环境选择明确?
b) Let a user choose locale?
b) 让用户选择语言环境?
Update
更新
The interceptor is not working. The XML is:
拦截器不起作用。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>
采纳答案by Emanuele Righetto
a) you defined a bean with id localeChangeInterceptor:
a) 你定义了一个 id 为 localeChangeInterceptor 的 bean:
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
this interceptor enable you to change your locale using the param you choose (in this case: "lang") in your query string (ie: http://mydomain.com/mypage?lang=frfor french)
这个拦截器使您能够使用您在查询字符串中选择的参数(在这种情况下:“lang”)更改您的语言环境(即:http://mydomain.com/mypage?lang= fr法语)
b) you can provide users link for changing locale using point a) c) you selected a default locale: "en". otherwhise locale is choosen using browser language
b) 您可以使用 a) 点为用户提供更改区域设置的链接 c) 您选择了默认区域设置:“en”。否则使用浏览器语言选择语言环境
NOTE: you should use <spring:message code="${msg.value}" arguments="${msg.args}"/>
for your localized string, not fmt, for more integration with spring...
注意:您应该<spring:message code="${msg.value}" arguments="${msg.args}"/>
用于本地化字符串,而不是 fmt,以便与 spring 进行更多集成...
回答by Ralph
You already have configured the LocaleChangeInterceptor
. Its parameter paramName
(you set it to lang
) is the request parameter that changed the locale.
您已经配置了LocaleChangeInterceptor
. 它的参数paramName
(您将其设置为lang
)是更改区域设置的请求参数。
change the configuration to hl
, then you can use this parameter to change it:
将配置更改为hl
,则可以使用此参数进行更改:
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="hl" />
</bean>
to let the user change the local, you only need to add some link to the page
让用户改变本地,你只需要添加一些链接到页面
<a href="${currentPage}?hl=de">German</a>