SpringBoot 国际化配置,SpringBoot Locale 国际化
SpringBoot 国际化配置,SpringBoot Locale 国际化
================================
©Copyright 蕃薯耀 2018年3月27日
http://www.cnblogs.com/fanshuyao/
附件下载(源码下载)见:http://fanshuyao.iteye.com/blog/2414640
一、效果所下:



二、SpringBoot 国际化配置
1、创建国际化配置文件(3个):
mess.properties
- mess.user.name=用户名
- mess.user.password=密码
- mess.user.btn=登录
mess_zh_CN.properties
- mess.user.name=用户名
- mess.user.password=密码
- mess.user.btn=登录
mess_en_US.properties
- mess.user.name=UserName
- mess.user.password=Password
- mess.user.btn=Sign In
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.messages.basename:
- #表示放在classpath的i18n文件夹,文件前缀为mess
- spring.messages.basename=i18n.mess
2、自定义国际化语言解析器
- import java.util.Locale;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.springframework.web.servlet.LocaleResolver;
- import org.thymeleaf.util.StringUtils;
- /**
- * 自定义国际化语言解析器
- *
- */
- 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容器中:
这里创建了一个自定义的配置类:CustomMvcConfig ,继承WebMvcConfigurerAdapter,可以扩展SpringMvc的功能,包括拦截器,转换器等
- 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.WebMvcConfigurerAdapter;
- import com.lqy.springboot.message.locale.MyLocaleResolver;
- //使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等
- //@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc
- @Configuration
- public class CustomMvcConfig extends WebMvcConfigurerAdapter {
- /**
- * 配置自己的国际化语言解析器
- * @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>[[#{mess.user.name}]]:<input th:placeholder="#{mess.user.name}"/></div>
- </div>
- <div>
- <div>[[#{mess.user.password}]]:<input th:placeholder="#{mess.user.password}"/></div>
- </div>
- <div>
- <div><button>[[#{mess.user.btn}]]</button></div>
- </div>
- <div class="mt20">
- <span class="ib"><a th:href="@{/mess(i18n_language=zh_CN)}">中文</a></span>
- <span class="ib ml20"><a th:href="@{/mess(i18n_language=en_US)}">英文</a></span>
- </div>
- </body>
- </html>
================================
©Copyright 蕃薯耀 2018年3月27日
http://www.cnblogs.com/fanshuyao/
SpringBoot 国际化配置,SpringBoot Locale 国际化的更多相关文章
- SpringBoot Logback配置,SpringBoot日志配置
SpringBoot Logback配置,SpringBoot日志配置 SpringBoot springProfile属性配置 ================================ © ...
- springboot自动配置国际化失效分析
最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...
- Springboot:员工管理之国际化(十(3))
1:IDEA编码设置UTF-8 2:创建国际化文件 i18n\login.properties #默认语言 i18n\login_en_US.properties #英文语言 i18n\login_z ...
- SpringBoot学习之验证信息国际化
以登录为例: 1.controller的登录方法: @RequestMapping("/SSOAuth/login") @ResponseBody public ResponseV ...
- springmvc国际化 基于请求的国际化配置
springmvc国际化 基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主. 项目结构图: 说明:properties文件中为国际化资源文件.格式相关 ...
- spring MVC 使用 hibernate validator验证框架,国际化配置
spring mvc使用hibernate validator框架可以实现的功能: 1. 注解java bean声明校验规则. 2. 添加message错误信息源实现国际化配置. 3. 结合sprin ...
- Spring MVC + Velocity实现国际化配置
国际化介绍 web开发中,国际化是需要考虑的一个问题,而且这个问题一般是越早敲定越好(不然等到系统大了,翻译是个问题).下面是结合实际项目(Spring MVC+Velocity)对实现国际化的一些总 ...
- Spring Security教程(5)---- 国际化配置及UserCache
这一章是为了给后面的讲解打基础的,主要介绍下国际化的配置及UserCache的配置及使用 国际化配置 <!-- 定义上下文返回的消息的国际化 --> <bean id="m ...
- Maven下Flex国际化配置
之前写了flashbulid.initellij下的flex国际化配置,它们都是在本地打包发布的,那么我们的工程用maven管理了,需要自动发布.这时候如何修改flex的pom文件,来让它build的 ...
随机推荐
- [YARN] 2.2 GB of 2.1 GB virtual memory used. Killing container.
Spark程序在yarn的集群运行,出现 Current usage: 105.9 MB of 1 GB physical memory used; 2.2 GB of 2.1 GB virtual ...
- .NetCore中EFCore的使用整理(二)-关联表查询
EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core 1.1 1.当前的版本,还不支持延迟加载(Lazy ...
- asp.net 简单记录请求的客户端和服务端 处理时间
最近项目需要简单记录一下 ajax客户端和服务端处理时间,服务端时间的思路是借用BeginRequest和EndRequest事件,为了不影响现有接口返回的数据格式,因此服务处理时间放在respons ...
- pyqt QFileDialog
from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QFileDialog class MyWindow(QtWidgets.QWidget) ...
- Unity Alpha融合参数(便查)
Alpha Blending,中文译作Alpha混合 Blending就是控制透明的.处于光栅化的最后阶段. 这里例如我们给一个模型贴一个材质,那么在某个点计算出来颜色值称为源,而该点之前累积的颜色值 ...
- 三种方法教你如何用PHP模拟post提交数据
php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...
- C# 怎么让winform程序中的输入文本框保留上次的输入
选中TextBox控件,在属性窗格中找到(ApplicationSettings),然后设置它. 绑定配置文件 private Settings settings = new Settings(); ...
- .NET 同步与异步 之 警惕闭包(十)
本随笔续接:.NET 同步与异步 之 原子操作和自旋锁(Interlocked.SpinLock)(九) 至此.同步与异步 相关的常规操作(比较常见的操作).差不多已经介绍完毕. 本随笔就着重说一下闭 ...
- 安装二维码、条形码识别工具zbar
参考:http://blog.csdn.net/gaofuqi/article/details/26698547 http://www.imagemagick.org/download/ImageMa ...
- MYSQL 中query_cache_size小结
1 原理 MySQL查询缓存保存查询返回的完整结果.当查询命中该缓存,会立刻返回结果,跳过了解析,优化和执行阶段. 查询缓存会跟踪查询中涉及的每个表,如果这写表发生变化,那么和这个表相关的所有缓 ...