java Spring MVC Web 应用程序 i18n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/726342/
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
Spring MVC web app i18n
提问by madewulf
In a web application written using spring-MVC, I want to allow users to change the current language by clicking on a link which text is the name of the language.
在使用 spring-MVC 编写的 Web 应用程序中,我希望允许用户通过单击文本为语言名称的链接来更改当前语言。
I have already set up a messageSource and made all my jsp pages find the messages using this messageSource. Currently, the language is changing depending on the locale of the user browser.
我已经设置了一个 messageSource 并使我所有的 jsp 页面都使用这个 messageSource 查找消息。目前,语言正在根据用户浏览器的区域设置而变化。
So, what I want to do now is to allow to change the locale manually.
所以,我现在想做的是允许手动更改语言环境。
I have found that the class SessionLocaleResolver could help, but I do not know how to set it up in my application context file (which name is myAppName-servlet.xml) .
我发现 SessionLocaleResolver 类可以提供帮助,但我不知道如何在我的应用程序上下文文件(名称为 myAppName-servlet.xml)中设置它。
I have defined the bean :
我已经定义了 bean:
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
But in which bean should I plug this ? Furthermore, how do I set a cookie related to locale into an user session ?
但是我应该把它插在哪个 bean 中?此外,如何将与语言环境相关的 cookie 设置到用户会话中?
回答by madewulf
All informations I needed were in the documentation, in front of me, at :
我需要的所有信息都在文档中,在我面前,位于:
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-localeresolver
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-localeresolver
In brief, I adapted the following xml to myAppName-servlet.xml
简而言之,我将以下 xml 改编为 myAppName-servlet.xml
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>
And now, it suffices to access any page with the parameter :
现在,使用参数访问任何页面就足够了:
siteLanguage=locale
to change the locale for the whole site.
更改整个站点的语言环境。
For example : http://localhost:8080/SBrowser/deliveries.html?siteLanguage=frenter code here
例如:http://localhost:8080/SBrowser/deliveries.html?siteLanguage=frenter code here

