SpringMVC作国际化

1.在ApplicationContext-mvc.xml,就是里面有配置以下内容的xml:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

在里面加入以下代码(注意注释):

<!-- 链接式i18n国际化、网址拦截器 -->
<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="basenames">
<list>
<value>i18n/index</value><!-- 配置语言文件的地址的默认配置文件,在这里我是index.properties -->
</list>
</property>
<property name="cacheSeconds" value="-1"/>
</bean>


2.配置语言文件,首先先写一个默认的文件,例如上面我所说的index.properties,默认中文。建议在Properties模式下add或者其他操作,如在Souce模式下编辑需用Unicode 编码编辑。index.properties内容比如为:

\u7CFB\u7EDF\u7BA1\u7406=\u7CFB\u7EDF\u7BA1\u7406
//系统管理=系统管理

这时候如需要繁体,则再建文件index_zh_TW.properties的文件,这个文件名都有固定的模式_XX_XX,XX代表各个语言的简称,可去网上搜。比如再来个英文的就为index_en_US.properties中文的index_zh_CN.properties等,内容如上,右边为转换后的字体,在这里还是建议Properties下编辑,这样Souce就会自动帮你Unicode编码编译好了。

3.在需要作字体转换的页面上,首先加上以下声明作标签用:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

然后在需要作字体转换的地方作修改,比如:

<spring:message code="系统管理"></spring:message>

再来做个链接:

<span>
[url=?locale=zh_CN]中文简体[/url]
[url=?locale=zh_TW]中文繁体[/url]
</span>


好了,这就是本人在SpringMVC上作的国际化配置,也是从网上各种学习而来,也不知道能不能通配所有系统,在这里自我学习一下,如有不足请大家指出谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值