springboot 使用i18n进行国际化
1、i18n介绍
i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。
2、页面元素国际化:
pom文件引入thymeleaf依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
新增一个html文件hello.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
<head>
<title>hello</title>
</head>
<body>
<span>
<label th:text="#{welcome}"></label>
</span>
</body>
</html>
SpringBoot 默认支持国际化的,在resources/下定义国际化文件,名称必须以messages开头,因为 MessageSourceAutoConfiguration 类中指定了前缀。
messages.properties
welcome = 欢迎使用i18n(默认)
messages_zh_CN.properties
welcome = 欢迎使用i18n(中文)
messages_en_US.properties
welcome = welcome to use i18n(english)
访问接口
@Controller
public class HelloController { @RequestMapping(value = "hello")
public String hello() {
return "hello";
}
}
启动项目访问http://localhost:8080/hello就可以看到效果
3、修改默认messages配置前缀
上面使用的是messages默认的配置,即直接放在resources/目录下,一般项目中会使用自己的目录存放,如放在resources/i18n/目录下
在application配置中添加
#i18n
spring:
messages:
encoding: UTF-8
basename: i18n/messages
加好之后重新访问即可
4、代码中使用国际化
//注入 MessageSource 对象,通过 getMessage 方法获取信息
@Autowired
private MessageSource messageSource; //使用
messageSource.getMessage("welcome", null, locale);
说明:第一个参数是国际化文件的key,第二个参数是key对应value中的占位符数据(如welcome=欢迎使用{0}中的{0}就是占位符,0表示是第一个,对应数据中的第一个值),第三个是当前区域
5、会话区域解析器SessionLocaleResolver
注入 Bean
//注入 Bean,会话区域解析器只针对当前会话有效
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
//设置默认区域,
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
接口控制器:
@RequestMapping("/i18n")
public String changeSessionLanauage(HttpServletRequest request, String lang){
System.out.println(lang);
if(CommonConsts.LANG_ZH.equals(lang)){
//代码中即可通过以下方法进行语言设置
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,new Locale("zh","CN"));
}else if(CommonConsts.LANG_EN.equals(lang)){
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,new Locale("en","US"));
}
return "redirect:/hello";
}
其中request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,new Locale("zh","CN"));用于切换当前会话区域
前端页面hello.html修改:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<script th:src="@{js/jquery.min.js}"></script>
<script th:src="@{js/hello.js}"></script>
</head>
<body>
<p><label th:text="#{welcome}"></label></p><br/>
<span th:text="#{lang}"></span>
<select id="locales">
<option value=""></option>
<option value="zh" th:text="zh"></option>
<option value="en" th:text="en"></option>
</select>
</body>
</html>
hello.js文件
$(function () {
$("#locales").change(function() {
var lang = $("#locales").val();
if (lang != "") {
window.location.replace("/i18n?lang=" + lang);
}
});
});
需要同时作用于Cookie时,修改接口控制器:
@RequestMapping("/i18n2")
public String changeSessionLanauage2(HttpServletRequest request, HttpServletResponse response, String lang){
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if(CommonConsts.LANG_ZH.equals(lang)){
localeResolver.setLocale(request, response, new Locale("zh","CN"));
}else if(CommonConsts.LANG_EN.equals(lang)){
localeResolver.setLocale(request, response, new Locale("en","US"));
}
return"redirect:/hello";
}
6、使用参数进行语言切换
使用拦截器来拦截请求接口中的参数来实现语言切换
注入区域切换拦截bean
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
//对请求路径中的参数lang进行拦截
lci.setParamName("lang"); return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
hello.html添加修改:
点击切换语言:
<a href="/hello?lang=zh_CN">简体中文</a>
<a href="/hello?lang=en_US">English(US)</a><br>
项目启动后点击链接测试效果即可
7、访问乱码问题解决
项目源码:github
springboot 使用i18n进行国际化的更多相关文章
- springboot 使用i18n进行国际化乱码解决
方式1.设置国际化的编码和你使用的编译器(IDEA之类)一致,如编译器为UTF-8则在application配置文件中添加 #i18n spring: messages: encoding: UTF- ...
- springboot使用i18n时properties文件中文乱码
在springboot使用i18n进行国际化文件配置时,文件名为messages_zh_CN.properties的文件中填写中文信息,当使用浏览器进行访问时,出现中文乱码,此时在idea中进行修改s ...
- SpringBoot系列——i18n国际化
前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...
- springboot、Thymeleaf、国际化的简单使用
1.项目体系结构 (1)知识体系 springboot:省去了很多繁琐的配置,如:视图解析器.前端控制器等 thymeleaf:获取controller数据逼能够进行展示 集合:用于存储数据,此练习没 ...
- struts.custom.i18n.resources国际化
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom ...
- struts.custom.i18n.resources国际化详解(一)
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom ...
- Go Revel - i18n(国际化)
##Messages `Messages`信息是对内容提供翻译的外部文本片段.revel提供了组织每一种语言文本片段的message文件.自动区域查找.基于cookie覆盖的消息嵌套和参数. 术语表: ...
- Angular i18n(国际化方案)
一.引言 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无 ...
- I18n问题 国际化
http://www.cnblogs.com/guaniu/archive/2012/01/18/2325556.html java国际化 1.了解缺省Locale是由操作系统决定的,Locale是由 ...
随机推荐
- 用故事解析setTimeout和setInterval(内含js单线程和任务队列)
区别: setTimeout(fn,t): 延迟调用,超过了时间就调用回调函数,返回一个id,使用clearTimeout(id)取消执行. 注意:取消了里面的回调函数就不执行了哦,而不是取消的时候就 ...
- springboot之swagger快速启动
springboot之swagger快速启动 简介 介绍 可能大家都有用过swagger,可以通过ui页面显示接口信息,快速和前端进行联调. 没有接触的小伙伴可以参考官网文章进行了解下demo页面. ...
- 牛客网暑期ACM多校训练营(第三场) C Shuffle Cards 平衡树 rope的运用
链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 Eddy likes to play cards game since there are al ...
- MPA JS CSS预处理方案
1.WebPack 添加配置文件webpack.config.js,直接在当前目录运行 webpack. var basepath = '/root/webapps/happ'; var glob = ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...
- 011 实例2-Python蟒蛇绘制
目录 一."Python蟒蛇绘制"问题分析 1.1 Python蟒蛇绘制 二."Python蟒蛇绘制"实例编写 三.运行效果 3.1 程序关键 四." ...
- Elasticsearch在Java中的增删改查
public class ElasticAPI { private static RestClient restClient; static { restClient=RestClient.build ...
- (转)为什么HashMap中链表长度超过8会转换成红黑树
原博地址:https://blog.csdn.net/xingfei_work/article/details/79637878 HashMap在jdk1.8之后引入了红黑树的概念,表示若桶中链表元素 ...
- Java Web总结(二)-- 上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接 ...
- HashMap和ConcurrentHashMap的区别,HashMap的底层源码
HashMap本质是数组加链表,根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap在HashMap的基础 ...