springBoot 实现中文国际化
一:实现效果如下:
二 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 实现中文国际化的更多相关文章
- SpringBoot起飞系列-国际化(六)
一.前言 国际化这个功能可能我们不常用,但是在有需要的地方还是必须要上的,今天我们就来看一下怎么在我们的web开发中配置国际化,让我们的网站可以根据语言来展示不同的形式.本文接续上一篇SpringBo ...
- SpringBoot系列——i18n国际化
前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...
- springboot + freemarker 实现国际化
前言 写过一篇springboot+freemarker国际化过程中的细节问题,但没有写过具体的国际化实现过程.正好有人在问,我就把我实现的过程贴出来,即使自己知识的一个备份,如果对别人有点用,那是再 ...
- SpringBoot 快速支持国际化i18n
学习目标 快速学会如何在工程中支持国际化语言. 快速查阅 专题阅读:<SpringBoot 布道系列> 源码下载:springboot-locale-i18n — Hey Man,Don' ...
- SpringBoot 配置文件 中文乱码
本方案,支持springboot 很简单 在配置文件中不写中文,写中文的ascll码 直接百度在线转ASCII,用工具 把中文转ASCII码==>\u628a\u4e2d\u6587\u8f6c ...
- SpringBoot + Spring MVC国际化使用示例
项目中需要显示中英文两种语言,所以需要对显示的内容进行国际化,如下是一个示例程序. 程序文件结构,如下图,后面详细列出各文件的代码. 1. 编写maven的pom.xml文件,如下: <proj ...
- Flutter中的日期插件date_format 中文 国际化 及flutter_cupertino_date_picker
今天我们来聊聊Flutter中的日期和日期选择器. Flutter中的日期和时间戳 //日期时间戳转换 var _nowTime = DateTime.now();//获取当前时间 print(_no ...
- springboot自动配置国际化失效分析
最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...
- SpringBoot @RequestBody 中文乱码
今天突然想学习一下Restful风,详细的我就不赘述了,我的理解是同一个请求路径根据请求方式不同进行不同的处理 如四种提交方式,这里推荐一个插件Postman,可以模仿各种请求类型,自行百度安装吧 G ...
随机推荐
- rtx 导出所有部门和用户
1>添加Interop.RTXServerApi.dll的引用 2>RTXServerApi.RTXObjectClass和RTXServerApi.RTXObject实现一样,建议使用R ...
- DOM0级与DOM2级
定义: 0级DOM 分为2个:一是在标签内写onclick事件 二是在JS写onlicke=function(){}函数 1) <input id="myButton" t ...
- 给你的LINUX程序加个文字画LOGO
经常看到很多的程序尤其LINUX程序有文字对应的那种LOGO,好酷炫啊. 研究了好久试了各种方法,后来在GOOGLE中搜索到一个软件叫:figlet 下载地址:http://www.figlet.or ...
- 从零开始学习前端JAVASCRIPT — 9、JavaScript基础RegExp(正则表达式)
1:正则的概念 正则表达式(regular expression)是一个描述字符规则的对象.可以用来检查一个字符串是否含有某个子字符串,将匹配的子字符串做替换或者从某个字符串中取出符合某个条件的子串等 ...
- jQuery Mobile的使用方法
安装: <link rel="stylesheet" href="下载到本地的文件目录.min.css" media="screen" ...
- Linq善解人意之通过MSDN对14个“查询关键字“逐个解剖
linq中存在的 14个关键字 网址: https://msdn.microsoft.com/zh-cn/library/bb310804.aspx from: 迭代变量 where:对数据源进行逻辑 ...
- Mac安装Tomcat
1. 到Tomcat官网下载,如下找tar格式文件: http://ftp.twaren.net/Unix/Web/apache/tomcat/tomcat-8/v8.0.41/bin/apache- ...
- eclipse开发hadoop2.2.0程序
在 Eclipse 环境下可以方便地进行 Hadoop 并行程序的开发和调试.前提是安装hadoop-eclipse-plugin,利用这个 plugin, 可以在 Eclipse 中创建一个 Had ...
- SQL存储过程编写,包含临时表
create proc R_ProductUse_M(@id1 int,@id2 int,@id3 int) as begin ---临时表1 select a.id1,b.id2,c.id3 int ...
- stderr和stdout(printf、fprintf、sprintf)(转)
本文转自:http://www.cnblogs.com/zhangyabin---acm/p/3203745.html 先区分一下:printf,sprintf,fprintf. 1,printf就是 ...