前言

  国际化是项目中不可或缺的功能,本文将实现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. netcore应用程序部署程序到ubuntu

    运维需求:获取服务器的运行情况,是否CPU.内存较高等,上报到运维系统 环境:ubuntu16.04 工具::netcore2.1.supervisor 程序实现(代码就不贴了)参考:https:// ...

  2. [转]XModem协议

    出处:XModem协议 XModem协议介绍:XModem是一种在串口通信中广泛使用的异步文件传输协议,分为XModem和1k-XModem协议两种,前者使用128字节的数据块,后者使用1024字节即 ...

  3. vue2.0 如何自定义组件(vue组件的封装)

    一.前言 之前的博客聊过 vue2.0和react的技术选型:聊过vue的axios封装和vuex使用.今天简单聊聊 vue 组件的封装. vue 的ui框架现在是很多的,但是鉴于移动设备的复杂性,兼 ...

  4. ArcGIS Runtime For Android 100.3天地图不加载问题

    ArcGIS Runtime 100.3 不加载天地图问题 参考这篇帖子:https://community.esri.com/thread/220496-1003-webtiledlayer-can ...

  5. 安卓开发学习笔记(二):如何用Android Stuidio在res资源下创建xml视图文件

    笔者在看了相关的教程之后发现教程当中的资源已经过时了.当我们在创建了一个新的空白的工程之后,会发现其文件夹下面的分文件夹目录和官方的教程文件结构完全不同,因此会引起很多误解.笔者使用的是最新版的And ...

  6. springboot加ES实现全局检索

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  7. emWin仪表界面设计,含uCOS-III和FreeRTOS两个版本

    第7期:多功能仪表界面设计配套例子:V6-912_STemWin提高篇实验_多功能仪表界面设计(uCOS-III)V6-913_STemWin提高篇实验_多功能仪表界面设计(FreeRTOS) 例程下 ...

  8. [Swift]LeetCode513. 找树左下角的值 | Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  9. Kubernetes 笔记 04 架构是个好东西

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...

  10. vue组件的基本使用,以及组件之间的基本传值方式

    组件(页面上的每一个部分都是组件) 1.三部分:结构(template),样式(style),逻辑(script) 2.组件的作用:复用 3.模块包含组件 4.组件创建:     1.全局组件:Vue ...