论坛首页 Java企业应用论坛

Spring MVC3 国际化配置

浏览 4988 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-06  
现在要做的一个简单MIS

需要页面有 中文|English

让用户选择自己的语言,改如何实现呢?
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
使用这个 如何配置啊,
中文|English 链接 后台的controller 应该如何设置,才能生效呢?
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
资源文件都配置了
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basename" value="messages" />                 
<property name="useCodeAsDefaultMessage" value="true" />
    </bean>
   发表时间:2012-04-06  
求 解啊,,网上搜了好多资料都没有弄好。。。
0 请登录后投票
   发表时间:2012-04-08  

import java.util.Locale;


        Locale locale = null;
    locale = new Locale("zh", "CN");  //中文
    //locale = new Locale("en", "US");   //英文
    request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);


0 请登录后投票
   发表时间:2012-04-09  
<span>
					<a href="?locale=zh_CN">中文</a> | <a href="?locale=en_US">English</a>  
					<br/><fmt:message key="common.lastlogintime"/><fmt:formatDate value="${sessionScope.BASE_USER_KEY_IN_SESSION.lastLoginTime }" type="both" pattern="yyyy-MM-dd HH:mm:ss"/>
				<br/>





	<mvc:interceptors>
		<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	</mvc:interceptors>
	
	<!-- Saves a locale change using a session-->
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

	<!-- 国际化文件 -->
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="classpath:/config/others/messages" />
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>




这样就可以了,controller代码里面不用写什么。有那个interceptor就可以了。
0 请登录后投票
   发表时间:2012-04-09  
用户每次访问的你的页面的时候,web服务器会通过读取浏览器的locale来确定你所用的语言,然后把locale作为参数传给ResourceBundleMessageSource等对象来显示对应的语言,如果你给用户提供一个强制改变语言的按钮,那么你的程序要用session记住,并且以后的每次请求都从session对象中读取locale。
0 请登录后投票
   发表时间:2012-04-09  
改变界面的语言还相对简单,如果还要改变用户提交内容的不同语言版本则复杂一点。www.oral2060.com是我做的,不仅可以根据用户浏览器来显示对应的语言还可以强制切换语言及其后台提交数据的不同语言版本,可以参考一下。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics