如何使用Spring的i18n机制? [英] How to use Spring's i18n mechanism?
问题描述
我在我的Spring项目中添加了本地化,它似乎正在工作,但是我想知道如何更改语言,如果基于浏览器设置,HTTP标头,Cookie或其他方式完成了语言选择.是否有一种明确的方法,例如以例如hl=de
是否在HTTP查询字符串上?我还希望允许用户在设置页面上设置语言,我该怎么做?我的实现看起来像这样,并用英语编写消息:
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:
<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>
那我怎么
a)通过启用HTTP GET参数(例如,德语为hl=de
,法语为hl=fr
)来覆盖语言环境来使语言环境选择显式吗?
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?
b)让用户选择语言环境吗?
b) Let a user choose locale?
拦截器不起作用. XML是:
The interceptor is not working. The XML is:
<?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>
推荐答案
a)您定义了一个ID为localeChangeInterceptor的bean:
a) you defined a bean with id localeChangeInterceptor:
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
此拦截器使您可以使用查询字符串(即: http://mydomain.com/mypage?lang=fr (法语)
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=fr for french)
b)您可以使用a)点为用户提供用于更改语言环境的链接 c)您选择了默认语言环境:"en".使用浏览器语言选择其他语言环境
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
注意:您应该对本地化的字符串而不是fmt使用<spring:message code="${msg.value}" arguments="${msg.args}"/>
,以便与spring进行更多集成...
NOTE: you should use <spring:message code="${msg.value}" arguments="${msg.args}"/>
for your localized string, not fmt, for more integration with spring...
这篇关于如何使用Spring的i18n机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!