Java国际化(i18n)
2024-10-18 22:35:00
Java国际化(i18n)
最近在做一个网站国际化的功能。用Java做开发,使用spring+velocity.
Java提供了对i18n的支持,spring对其做了集成,可以很方便的配置。主要思想就是根据语言来读取不同的配置文件,来显示对应的文本。主要步骤如下:
1. 用两个properties文件来保存“符号”到对应语言的映射。如messages_en.properties和messages_zh.properties, 将其放到工程的classPath下
#messages_en.properties
title=service introduction
#messages_cn.properties title=\u670d\u52a1\u8bf4\u660e
注意中文要使用unicode编码
2. 配置spring的xml文件:
spring提供了多种国际化的支持方式,如基于浏览器,session和cookie等。项目中使用cookie方式
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="604800"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="lang"/>
</bean>
声明1中的资源文件
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
使用拦截器来处理请求,让对应的页面国际化
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> ...
<property name="interceptors" ref="localeChangeInterceptor"></property>
...
或者
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
...
</mvc:interceptor>
</mvc:interceptors>
然后在vm页面中调用占位符就可以了
#springMessage("title")
3. 另外可以写一个controller来切换语言
@RequestMapping("/lang")
public String lang(
HttpServletRequest request, HttpServletResponse response) throws Exception {
String langType = request.getParameter("langType"); if ("en".equals(langType)) {
cookieLocaleResolver.setLocale(request, response, Locale.ENGLISH);
} else {
cookieLocaleResolver.setLocale(request, response, Locale.CHINA);
}
return null;
}
在Java国际化(i18n)中,
vm页面显示内容需要使用 #springMessage("title")
实际运行时发现页面输出$springMacroRequestContext.getMessage($code)。看了一下源代码,#springMessage是一个宏,在spring.vm中定义为
#macro( springMessage $code )$springMacroRequestContext.getMessage($code)#end
原因是因为未找到$springMacroRequestContext...
解决方法:在我们定义视图resolver中加入对spring宏的支持
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
...
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
最新文章
- 1035-Spell checker(模糊匹配)
- Mssql迁移至Oracle 查询优化
- CooMark网页颜色取色表
- 我的c++学习(1)hello world!
- Windows 8 Tips
- bootstrap-轮播图
- CSS3 transforms 3D翻开
- 一致性哈希(consistent hashing)算法
- java functional syntax overview
- L009-oldboy-mysql-dba-lesson09
- RedHat6.1(64bit)安装JDK
- CentOS下MySQL无法正常启动错误
- 关于win7系统的Oracle安装时的[INS-30131]问题的解决方案
- 面向对象的方式进行数据交换网络之间的差异--无缝切换的发展到单机游戏C/S模式
- RESTful WebService 入门实例
- spring-aop 的注释用法
- E-R视图中有关图形的用法
- golang:mime.Decode、mime.DecodeHeader
- Spring-boot logback日志处理
- Java虚拟机11:内存分配原则
热门文章
- Oracle基础——学习笔记
- Perforce 与Source Insight, Visual Studio集成
- 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
- asp.net缓存
- 移动端web开发,click touch tap区别
- [LeetCode] Ternary Expression Parser 三元表达式解析器
- [LeetCode] Flip Game 翻转游戏
- [LeetCode] Plus One 加一运算
- Gone Fishing POJ 1042
- ceph hadoop spark 大数据处理