一:实现效果如下:

二 SpringBoot 国际化配置

1、创建国际化配置文件(3个):

messages.properties

messages.user.name=用户名
messages.user.password=密码
messages.user.btn=登录

messages_en_US.properties

messages.user.name=UserName
messages.user.password=Password
messages.user.btn=Sign In
messages.keyword=keyword

messages_zh_CN.properties 

messages.user.name=\u7528\u6237\u540d
messages.user.password=\u5bc6\u7801
messages.user.btn=\u767b\u5f55
messages.keyword=\u5173\u952e\u8bcd

SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.messages.basename:

#表示放在classpath的i18n文件夹,文件前缀为mess
spring.messages.basename=i18n/messages

2.自定义国际化语言解析器  

package com.joyny.ecas.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale; /**
* 自定义国际化语言解析器
*
*/
public class MyLocaleResolver implements LocaleResolver { private static final String I18N_LANGUAGE = "i18n_language";
private static final String I18N_LANGUAGE_SESSION = "i18n_language_session"; @Override
public Locale resolveLocale(HttpServletRequest req) {
String i18n_language = req.getParameter(I18N_LANGUAGE);
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(i18n_language)) {
String[] language = i18n_language.split("_");
locale = new Locale(language[0], language[1]); //将国际化语言保存到session
HttpSession session = req.getSession();
session.setAttribute(I18N_LANGUAGE_SESSION, locale);
}else {
//如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对
HttpSession session = req.getSession();
Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);
if(localeInSession != null) {
locale = localeInSession;
}
}
return locale;
} @Override
public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) { } }

3、把国际化语言解析器放到Spring容器中:

package com.joyny.ecas.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//WebMvcConfigurerAdapter
//使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等
//@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc
@Configuration
public class CustomMvcConfig implements WebMvcConfigurer { /**
* 配置自己的国际化语言解析器
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
} /**
* 配置自己的拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
} }

4、页面显示及切换国际化操作:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.ib{
display: inline-block;
}
.ml20{
margin-left: 20px;
}
.mt20{
margin-top: 20px;
}
</style>
</head>
<body>
<div>
<div>[[#{messages.user.name}]]:<input th:placeholder="#{messages.user.name}"/></div>
</div>
<div>
<div>[[#{messages.user.password}]]:<input th:placeholder="#{messages.user.password}"/></div>
</div>
<div>
<div><button>[[#{messages.user.btn}]]</button></div>
</div> <div class="mt20">
<span class="ib"><a th:href="@{/mapdemo/hello(i18n_language=zh_CN)}">中文</a></span>
<span class="ib ml20"><a th:href="@{/mapdemo/hello(i18n_language=en_US)}">英文</a></span>
</div> </body>
</html>

  

  

springBoot 实现中文国际化的更多相关文章

  1. SpringBoot起飞系列-国际化(六)

    一.前言 国际化这个功能可能我们不常用,但是在有需要的地方还是必须要上的,今天我们就来看一下怎么在我们的web开发中配置国际化,让我们的网站可以根据语言来展示不同的形式.本文接续上一篇SpringBo ...

  2. SpringBoot系列——i18n国际化

    前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...

  3. springboot + freemarker 实现国际化

    前言 写过一篇springboot+freemarker国际化过程中的细节问题,但没有写过具体的国际化实现过程.正好有人在问,我就把我实现的过程贴出来,即使自己知识的一个备份,如果对别人有点用,那是再 ...

  4. SpringBoot 快速支持国际化i18n

    学习目标 快速学会如何在工程中支持国际化语言. 快速查阅 专题阅读:<SpringBoot 布道系列> 源码下载:springboot-locale-i18n — Hey Man,Don' ...

  5. SpringBoot 配置文件 中文乱码

    本方案,支持springboot 很简单 在配置文件中不写中文,写中文的ascll码 直接百度在线转ASCII,用工具 把中文转ASCII码==>\u628a\u4e2d\u6587\u8f6c ...

  6. SpringBoot + Spring MVC国际化使用示例

    项目中需要显示中英文两种语言,所以需要对显示的内容进行国际化,如下是一个示例程序. 程序文件结构,如下图,后面详细列出各文件的代码. 1. 编写maven的pom.xml文件,如下: <proj ...

  7. Flutter中的日期插件date_format 中文 国际化 及flutter_cupertino_date_picker

    今天我们来聊聊Flutter中的日期和日期选择器. Flutter中的日期和时间戳 //日期时间戳转换 var _nowTime = DateTime.now();//获取当前时间 print(_no ...

  8. springboot自动配置国际化失效分析

    最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...

  9. SpringBoot @RequestBody 中文乱码

    今天突然想学习一下Restful风,详细的我就不赘述了,我的理解是同一个请求路径根据请求方式不同进行不同的处理 如四种提交方式,这里推荐一个插件Postman,可以模仿各种请求类型,自行百度安装吧 G ...

随机推荐

  1. 关于oraclize使用最好的一篇文章

    https://medium.com/@msolomon44/using-apis-in-your-ethereum-smart-contract-with-oraclize-95656434292e ...

  2. CentOS/RedHat安装Python3

    CentOS/RedHat安装Python3 摘自:https://blog.csdn.net/mvpboss1004/article/details/79377019 CentOS/RedHat默认 ...

  3. smarty中用truncate来截取中英文字符串及避免中文乱码问题

    smarty中用truncate来截取含有中英文的字符串,可能会出现中文乱码问题.字符串截取长度不一问题,下面是新建个扩展函数,或修改原Truncate函数方法也可以的.扩展smarty/plugin ...

  4. VMware vSphere之vCenter

    1.vCenter是什么? vCenter Server 是 vSphere管理员使用的主要管理工具,首先,通过它可实现对数据中心所有主机的单点控制,也就是提供集中式管理.其次,提供基本的基础结构操作 ...

  5. 更改oracle数据库字符集

    A.oracle server 端 字符集查询  select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LAN ...

  6. Firefox 43无法安装xpi的问题

    Firefox 43无法安装xpi的问题 说明:Firefox 42将默认禁止安装未签名扩展   强制禁用这个首选项(高级用户): 你可以在 Firefox 配置编辑页面 (about:config ...

  7. Android 文件存放路径【转】

    对于应用携带的静态数据,可以放置在应用的assets目录或者res,raw目录下.对于assets目录下的静态数据,存在当文件最大支持1MB的局限,读取方式如下: 1 InputStream is = ...

  8. 设计模式20:Memento 备忘录模式(行为型模式)

    Memento 备忘录模式(行为型模式) 对象状态的回溯 对象状态的变化无端,如何回溯.恢复对象在某个点的状态? 动机(Motivation) 在软件构建过程中,某些对象的状态在转换过程中,可能由于某 ...

  9. JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序

    大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...

  10. Python 数据分析—第九章 数据聚合与分组运算

    打算从后往前来做笔记 第九章 数据聚合与分组运算 分组 #生成数据,五行四列 df = pd.DataFrame({'key1':['a','a','b','b','a'], 'key2':['one ...