SpringBoot 国际化配置,SpringBoot Locale 国际化

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

附件下载(源码下载)见:http://fanshuyao.iteye.com/blog/2414640

一、效果所下:

二、SpringBoot 国际化配置

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

mess.properties

  1. mess.user.name=用户名
  2. mess.user.password=密码
  3. mess.user.btn=登录

mess_zh_CN.properties

  1. mess.user.name=用户名
  2. mess.user.password=密码
  3. mess.user.btn=登录

mess_en_US.properties

  1. mess.user.name=UserName
  2. mess.user.password=Password
  3. mess.user.btn=Sign In

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

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

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

  1. import java.util.Locale;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import javax.servlet.http.HttpSession;
  5. import org.springframework.web.servlet.LocaleResolver;
  6. import org.thymeleaf.util.StringUtils;
  7. /**
  8. * 自定义国际化语言解析器
  9. *
  10. */
  11. public class MyLocaleResolver implements LocaleResolver{
  12. private static final String I18N_LANGUAGE = "i18n_language";
  13. private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";
  14. @Override
  15. public Locale resolveLocale(HttpServletRequest req) {
  16. String i18n_language = req.getParameter(I18N_LANGUAGE);
  17. Locale locale = Locale.getDefault();
  18. if(!StringUtils.isEmpty(i18n_language)) {
  19. String[] language = i18n_language.split("_");
  20. locale = new Locale(language[0], language[1]);
  21. //将国际化语言保存到session
  22. HttpSession session = req.getSession();
  23. session.setAttribute(I18N_LANGUAGE_SESSION, locale);
  24. }else {
  25. //如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对
  26. HttpSession session = req.getSession();
  27. Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);
  28. if(localeInSession != null) {
  29. locale = localeInSession;
  30. }
  31. }
  32. return locale;
  33. }
  34. @Override
  35. public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {
  36. }
  37. }

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

这里创建了一个自定义的配置类:CustomMvcConfig ,继承WebMvcConfigurerAdapter,可以扩展SpringMvc的功能,包括拦截器,转换器等

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.LocaleResolver;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  6. import com.lqy.springboot.message.locale.MyLocaleResolver;
  7. //使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等
  8. //@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc
  9. @Configuration
  10. public class CustomMvcConfig extends WebMvcConfigurerAdapter {
  11. /**
  12. * 配置自己的国际化语言解析器
  13. * @return
  14. */
  15. @Bean
  16. public LocaleResolver localeResolver() {
  17. return new MyLocaleResolver();
  18. }
  19. /**
  20. * 配置自己的拦截器
  21. */
  22. @Override
  23. public void addInterceptors(InterceptorRegistry registry) {
  24. //super.addInterceptors(registry);
  25. }
  26. }

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

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. <style type="text/css">
  7. .ib{
  8. display: inline-block;
  9. }
  10. .ml20{
  11. margin-left: 20px;
  12. }
  13. .mt20{
  14. margin-top: 20px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div>
  20. <div>[[#{mess.user.name}]]:<input th:placeholder="#{mess.user.name}"/></div>
  21. </div>
  22. <div>
  23. <div>[[#{mess.user.password}]]:<input th:placeholder="#{mess.user.password}"/></div>
  24. </div>
  25. <div>
  26. <div><button>[[#{mess.user.btn}]]</button></div>
  27. </div>
  28. <div class="mt20">
  29. <span class="ib"><a th:href="@{/mess(i18n_language=zh_CN)}">中文</a></span>
  30. <span class="ib ml20"><a th:href="@{/mess(i18n_language=en_US)}">英文</a></span>
  31. </div>
  32. </body>
  33. </html>

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

SpringBoot 国际化配置,SpringBoot Locale 国际化的更多相关文章

  1. SpringBoot Logback配置,SpringBoot日志配置

    SpringBoot Logback配置,SpringBoot日志配置  SpringBoot springProfile属性配置 ================================ © ...

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

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

  3. Springboot:员工管理之国际化(十(3))

    1:IDEA编码设置UTF-8 2:创建国际化文件 i18n\login.properties #默认语言 i18n\login_en_US.properties #英文语言 i18n\login_z ...

  4. SpringBoot学习之验证信息国际化

    以登录为例: 1.controller的登录方法: @RequestMapping("/SSOAuth/login") @ResponseBody public ResponseV ...

  5. springmvc国际化 基于请求的国际化配置

    springmvc国际化 基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主. 项目结构图: 说明:properties文件中为国际化资源文件.格式相关 ...

  6. spring MVC 使用 hibernate validator验证框架,国际化配置

    spring mvc使用hibernate validator框架可以实现的功能: 1. 注解java bean声明校验规则. 2. 添加message错误信息源实现国际化配置. 3. 结合sprin ...

  7. Spring MVC + Velocity实现国际化配置

    国际化介绍 web开发中,国际化是需要考虑的一个问题,而且这个问题一般是越早敲定越好(不然等到系统大了,翻译是个问题).下面是结合实际项目(Spring MVC+Velocity)对实现国际化的一些总 ...

  8. Spring Security教程(5)---- 国际化配置及UserCache

    这一章是为了给后面的讲解打基础的,主要介绍下国际化的配置及UserCache的配置及使用 国际化配置 <!-- 定义上下文返回的消息的国际化 --> <bean id="m ...

  9. Maven下Flex国际化配置

    之前写了flashbulid.initellij下的flex国际化配置,它们都是在本地打包发布的,那么我们的工程用maven管理了,需要自动发布.这时候如何修改flex的pom文件,来让它build的 ...

随机推荐

  1. [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 ...

  2. .NetCore中EFCore的使用整理(二)-关联表查询

    EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core  1.1 1.当前的版本,还不支持延迟加载(Lazy ...

  3. asp.net 简单记录请求的客户端和服务端 处理时间

    最近项目需要简单记录一下 ajax客户端和服务端处理时间,服务端时间的思路是借用BeginRequest和EndRequest事件,为了不影响现有接口返回的数据格式,因此服务处理时间放在respons ...

  4. pyqt QFileDialog

    from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QFileDialog class MyWindow(QtWidgets.QWidget) ...

  5. Unity Alpha融合参数(便查)

    Alpha Blending,中文译作Alpha混合 Blending就是控制透明的.处于光栅化的最后阶段. 这里例如我们给一个模型贴一个材质,那么在某个点计算出来颜色值称为源,而该点之前累积的颜色值 ...

  6. 三种方法教你如何用PHP模拟post提交数据

    php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...

  7. C# 怎么让winform程序中的输入文本框保留上次的输入

    选中TextBox控件,在属性窗格中找到(ApplicationSettings),然后设置它. 绑定配置文件 private Settings settings = new Settings(); ...

  8. .NET 同步与异步 之 警惕闭包(十)

    本随笔续接:.NET 同步与异步 之 原子操作和自旋锁(Interlocked.SpinLock)(九) 至此.同步与异步 相关的常规操作(比较常见的操作).差不多已经介绍完毕. 本随笔就着重说一下闭 ...

  9. 安装二维码、条形码识别工具zbar

    参考:http://blog.csdn.net/gaofuqi/article/details/26698547 http://www.imagemagick.org/download/ImageMa ...

  10. MYSQL 中query_cache_size小结

    1 原理    MySQL查询缓存保存查询返回的完整结果.当查询命中该缓存,会立刻返回结果,跳过了解析,优化和执行阶段. 查询缓存会跟踪查询中涉及的每个表,如果这写表发生变化,那么和这个表相关的所有缓 ...