前言

  国际化是项目中不可或缺的功能,本文将实现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>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="?lang=zh_CN">简体中文</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<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国际化的更多相关文章

  1. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  2. springboot 使用i18n进行国际化

    1.i18n介绍 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬 ...

  3. SpringBoot系列之集成Thymeleaf用法手册

    目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...

  4. SpringBoot系列之学习教程汇总

    对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件     >> source down ...

  5. SpringBoot系列之从入门到精通系列教程

    对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...

  6. springboot使用i18n时properties文件中文乱码

    在springboot使用i18n进行国际化文件配置时,文件名为messages_zh_CN.properties的文件中填写中文信息,当使用浏览器进行访问时,出现中文乱码,此时在idea中进行修改s ...

  7. springBoot系列教程07:异常捕获

    发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...

  8. SpringBoot系列(六)集成thymeleaf详解版

    SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介  1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎.  2. Thymeleaf ...

  9. Django1.9开发博客(12)- i18n国际化

    国际化与本地化的目的为了能为各个不同的用户以他们最熟悉的语言和格式来显示网页. Django能完美支持文本翻译.日期时间和数字的格式化.时区. 另外,Django还有两点优势: 允许开发者和模板作者指 ...

随机推荐

  1. mac搭建简单的hls推流服务器遇到的问题(待更新)

    实际操作步骤: 输入brew install nginx-full --with-rtmp-module命令出现以下报错: 需要先安装nginx服务器,运行命令brew tap homebrew/ng ...

  2. entOS7查看开放端口命令

    CentOS7的开放关闭查看端口都是用防火墙来控制的,具体命令如下: 查看已经开放的端口: firewall-cmd --list-ports 开启端口 firewall-cmd --zone=/tc ...

  3. mybatis逆向工程的注意事项,以及数据库表

    1.选择性更新,如果有新参数就更换成新参数,如果参数是null就不更新,还是原来的参数 2.mybatis使用逆向工程,数据库建表的字段user_id必须用下滑线隔开,这样生成的对象private L ...

  4. EDI

    EDI, Electronic Data Interchange,电子数据交换 EDI 商务是指将商业或行政事务按一个公认的标准,形成结构化的事务处理或文档数据格式,从计算机到计算机的电子传输方法.简 ...

  5. 链表加bfs求补图联通块

    https://oj.neu.edu.cn/problem/1387 给一个点数N <= 100000, 边 <= 1000000的无向图,求补图的联通块数,以及每个块包含的点数 由于点数 ...

  6. [Swift]LeetCode821. 字符的最短距离 | Shortest Distance to a Character

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  7. Python档案袋( 进程与协程 )

    Python的进程和线程是使用的操作系统的原生线程和进程,其是去调用操作系统的相应接口实现 进程:之间不可直接共享数据,是资源的集合,进程必须有一个线程 线程:基于进程,之间可直接共享数据,可执行,只 ...

  8. 浅谈React

    浅谈react react是什么?其官网给出了明确定义:A JavaScript library for building user interfaces,一个用于构建用户界面的JavaScript库 ...

  9. Git漏洞允许任意代码执行(CVE-2018-17456)复现

    Git漏洞允许任意代码执行(CVE-2018-17456) 国外安全研究员 joernchen 在 9 月 23 日向 git 官方报告了漏洞的相关细节.10月5日,Git项目披露了一个漏洞,编号为C ...

  10. 陕西省网络空间安全技术大赛部分题目writeup

    签到-欢迎来到CSTC2017 10 欢迎来到CSTC2017 ZmxhZ3tXZWlTdW9GeXVfQmllTGFuZ30= Base64解密:flag{WeiSuoFyu_BieLang} 种棵 ...