SpringBoot系列——i18n国际化
前言
国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面、js代码、java代码国际化过程记录下来。
代码编写
工程结构

每个文件里面的值(按工程结构循序从上往下)
##################默认值#############################
welcome=Welcome
##################英文#############################
welcome=Welcome
##################简体中文#############################
welcome=欢迎
##################簡體中文#############################
welcome=歡迎
yml配置文件
#注意:在yml文件中添加value值时,value前面需要加一个空格
#2.0.0的配置切换为servlet.path而不是"-"
server:
port: #端口号
servlet:
context-path: /springboot #访问根路径 spring:
thymeleaf:
cache: false #关闭页面缓存
prefix: classpath:/view/ #thymeleaf访问根路径
mode: LEGACYHTML5 messages:
basename: static/i18n/messages #指定国际化文件路径
LocaleConfig.java,
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter { @Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.US);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
} }
controller
单纯的跳转页面即可
@RequestMapping("/i18nTest")
public ModelAndView i18nTest(){
ModelAndView mv=new ModelAndView();
mv.setViewName("i18nTest.html");
return mv;
}
HTML使用,注意要用 #{} 来取值
<!DOCTYPE html>
<!--解决idea thymeleaf 表达式模板报红波浪线-->
<!--suppress ALL -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h3 th:text="#{welcome}"></h3>
<a href="?lang=en_US">English(US)</a>
<a href="?lang=zh_CN">简体中文</a>
<a href="?lang=zh_TW">繁体中文</a>
</body>
</html>
js代码使用
需要先引入jquery插件,自行百度下载或者使用webjar去拉取,可以封装成一个全局方法,用到时直接调用
<script th:src="@{/js/jquery-1.9.1.min.js}"></script>
<script th:src="@{/js/jquery.i18n.properties.js}"></script>
<script th:inline="javascript">
//项目路径
ctx = [[${#request.getContextPath()}]]; //初始化i18n插件
try {
$.i18n.properties({
path: ctx + '/i18n/',
name: 'messages',
language: [[${#locale.language+'_'+#locale.country}]],
mode: "both"
});
} catch (e) {
console.error(e);
} //初始化i18n方法
function i18n(labelKey) {
try {
return $.i18n.prop(labelKey);
} catch (e) {
console.error(e);
return labelKey;
}
} console.log(i18n("welcome"));
</script>
java代码使用
先封装个工具类,直接静态调用
@Component
public class I18nUtil { private static MessageSource messageSource; public I18nUtil(MessageSource messageSource) {
I18nUtil.messageSource = messageSource;
} /**
* 获取单个国际化翻译值
*/
public static String get(String msgKey) {
try {
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return msgKey;
}
}
}
调用
System.out.println(I18nUtil.get("welcome"));
效果展示
默认


英文


中文


繁体


结束语
文章部分参考:玩转spring boot——国际化:https://www.cnblogs.com/GoodHelper/p/6824492.html
代码开源
代码已经开源、托管到我的GitHub、码云:
GitHub:https://github.com/huanzi-qch/springBoot
码云:https://gitee.com/huanzi-qch/springBoot
SpringBoot系列——i18n国际化的更多相关文章
- SpringBoot系列之i18n集成教程
目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...
- springboot 使用i18n进行国际化
1.i18n介绍 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬 ...
- SpringBoot系列之集成Thymeleaf用法手册
目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...
- SpringBoot系列之学习教程汇总
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件 >> source down ...
- SpringBoot系列之从入门到精通系列教程
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...
- springboot使用i18n时properties文件中文乱码
在springboot使用i18n进行国际化文件配置时,文件名为messages_zh_CN.properties的文件中填写中文信息,当使用浏览器进行访问时,出现中文乱码,此时在idea中进行修改s ...
- springBoot系列教程07:异常捕获
发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...
- SpringBoot系列(六)集成thymeleaf详解版
SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介 1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎. 2. Thymeleaf ...
- Django1.9开发博客(12)- i18n国际化
国际化与本地化的目的为了能为各个不同的用户以他们最熟悉的语言和格式来显示网页. Django能完美支持文本翻译.日期时间和数字的格式化.时区. 另外,Django还有两点优势: 允许开发者和模板作者指 ...
随机推荐
- svn idea 修改文件,文件不变色
删除后,重新添加. 我这里是什么也没有选择,选上Subversion后,保存,再修改文件,文件颜色就变了 Settings-->Version Control
- python中的单向循环链表实现
引子 所谓单向循环链表,不过是在单向链表的基础上,如响尾蛇般将其首尾相连,也因此有诸多类似之处与务必留心之点.尤其是可能涉及到头尾节点的操作,不可疏忽. 对于诸多操所必须的遍历,这时的条件是什么?又应 ...
- [LeetCode] Mirror Reflection 镜面反射
There is a special square room with mirrors on each of the four walls. Except for the southwest cor ...
- DOM-节点概念-属性
1.节点的概念 页面中的所有内容,包括标签,属性,文本(文字,空格,回车,换行等),也就是说页面的所有内容都可以叫做节点. 2.节点相关的属性 2.1.节点分类 **标签节点:**比如 div 标签, ...
- Jvm 内存模型 —— GC
一.Jvm 原理 二.Jvm 运行时数据区( Run-Time Data Areas ) (主要是关于 non-stack 区域的详细划分) 从上图可以清楚地看到:程序计数器.Jvm 栈.本地方法栈 ...
- [VUE]object.defineProperty的基本使用
1.object.defineProperty 给一个对象定义一个新的属性或者在修改一个对象现有的属性,并返回这个对象 语法: Object.defineProperty(参数1,参数2,参数3) 参 ...
- Python练手例子(12)
67.输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组. #python3.7 def inp(numbers): for i in range(6): numbers.appen ...
- C语言面试题分类->链表
链表的创建,清空,插入,删除 typedef int (* __compfunc)(const void *, const void *); //Traverse list. Fast macro t ...
- RabbitMQ in Action (2): Running and administering Rabbit
Server management the Erlang node and the Erlang application Starting nodes multiple Erlang applicat ...
- Trie树(字典树)的介绍及Java实现
简介 Trie树,又称为前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也 ...