spring 国际化-i18n
Spring 国际化 I18N
2023-09-11 14:19:27 时间
i18n(其 来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在资讯领域,国际化(i18n)指让产品(出版 物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。 在全球化的时代,国际化尤为重要,因为产品的潜在用户可能来自世界的各个角落。通常与i18n相关的还有L10n(“本地化”的简称)。<摘自百度 百科http://baike.baidu.com/view/372835.htm?fr=aladdin >
代码下载
http://pan.baidu.com/s/1sjNQmfF
Maven依赖
<properties> <springframework>4.0.5.RELEASE</springframework> </properties> <dependencies> <!-- Spring web mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework}</version> </dependency> </dependencies>
在Spring应用中,国际化的配置比较简单,下面四步完成国际化的快速配置
第一步,配置messageSource和localeResolver
<!-- 配置国际化资源文件路径 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <!-- 定义消息资源文件的相对路径 --> <value>messages/message</value> </property> </bean> <!-- 基于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="Language"></property> </bean> <!-- 基于Session的本地化解析器 --> <!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />-->
第二步,编写message_*.properties
message_en.properties
hi=hello something=The People's Republic of China Chinese=Chinese English=English index=Index welcome=Welcome OtherPage=Other Page |
message_zh_CN.properties(汉字已转成unicode码)
hi=\u4F60\u597D something=\u4E2D\u534E\u4EBA\u6C11\u5171\u548C\u56FD Chinese=\u4E2D\u6587 English=\u82F1\u6587 OtherPage=\u5176\u4ED6\u9875\u9762 index=\u9996\u9875 welcome=\u6B22\u8FCE |
第三步,页面引入spring标签库
引入
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> |
使用
< spring:message code = "welcome" ></ spring:message > |
第四步,切换语言
@Autowired
CookieLocaleResolver resolver; //@Autowired SessionLocaleResolver resolver; /** * 语言切换 */ @RequestMapping("language") public ModelAndView language(HttpServletRequest request,HttpServletResponse response,String language){ language=language.toLowerCase(); if(language==null||language.equals("")){ return new ModelAndView("redirect:/"); }else{ if(language.equals("zh_cn")){ resolver.setLocale(request, response, Locale.CHINA ); }else if(language.equals("en")){ resolver.setLocale(request, response, Locale.ENGLISH ); }else{ resolver.setLocale(request, response, Locale.CHINA ); } } return new ModelAndView("redirect:/"); }
已完成国际化的配置,其中请注意SessionLocaleResolver和CookieLocaleResolver的区别,很显然,通过 Session只能对当前的会话有效,Cookie则对Cookie有效期内的会话都有效。在使用Cookie的时候,需要设置Cookie的过期时间, 否则关闭浏览器之后,Cookie即失效了,没有达到目的。当然,也可以保存用户的语言设置信息到数据库,用户登录之后即可将语言改变成用户设置的语言。
相关文章
- 消息队列RabbitMQ与Spring集成
- Spring Boot国际化支持
- Spring: Springboot
- Is Spring Integration 5.5.10 must be based on Java 9 to use or depend on spring integration? #3761
- Spring Boot下的lombok安装以及使用简介
- Spring核心IOC的源码分析
- Spring Cloud使用Hystrix实现容错处理
- spring batch(二):核心部分(1):配置Spring batch
- 利用Spring AOP机制拦截方法一例
- 江帅帅:Spring Boot 底层级探索系列 02 - 自动配置的底层逻辑
- Spring MVC统一异常处理
- spring注解 annotation
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
- spring的context---ServletContext WebApplicationContext---Spring各种上下文的关系详解
- spring框架漏洞整理(Spring Cloud Config路径穿越导致的信息泄露)
- spring框架漏洞整理(Spring Boot Actuator命令执行漏洞)
- spring框架漏洞整理(Spring Framework漏洞)
- spring框架漏洞整理(Spring Data漏洞)
- spring中@Autowire和@Resource的区别在哪里?
- spring mvc Spring Data Redis RedisTemplate [转]
- Spring Security教程(5)---- 国际化配置及UserCache
- spring core源码解读之ASM4用户手册翻译之一asm简介