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. STL——模拟实现空间配置器

    目录 问题 SGI版本空间配置器-std::alloc 一级空间配置器 二级空间配置器 Refill.chunkAlloc函数 最后,配置器封装的simple_alloc接口 问题 我们在日常编写C+ ...

  2. SSH使用自定义私钥进行登录

    ssh -i /root/.ssh/id_rsa root@192.168.1.2 -i指定了私钥文件的路径

  3. Knockout.Js官网学习(selectedOptions绑定、uniqueName 绑定)

    selectedOptions绑定 selectedOptions绑定用于控制multi-select列表已经被选择的元素,用在使用options绑定的<select>元素上. 当用户在m ...

  4. 线程安全的CopyOnWriteArrayList介绍

    证明CopyOnWriteArrayList是线程安全的 先写一段代码证明CopyOnWriteArrayList确实是线程安全的. ReadThread.java import java.util. ...

  5. CentOS中环境变量和配置文件

    什么是环境变量 bash shell用一个叫做 环境变量(environment variable) 的特性来存储有关shell会话和工作环境的信息.即允许在内存中存储数据,使得在程序或shell中运 ...

  6. 如何在Angular优雅编写HTTP请求

    原文:https://segmentfault.com/a/1190000010570799 ----------------------------------------------------- ...

  7. 树莓派(RespberryPi)安装手记

    购买了两台树莓派,显示器接口是HDMI的,所以需要HDMI高清线连接到显示器,再加上SD卡做硬盘以及无线USB-WIFI,就可以玩一玩树莓派这个小东西了.以下是安装手记. 首先是制作“启动光盘”,其实 ...

  8. CentOS下防御或减轻DDoS攻击方法(转)

    查看攻击IP 首先使用以下代码,找出攻击者IP netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 将会得 ...

  9. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  10. 简单的redis 的list应用

    error_reporting(E_ALL); if(empty($a)){ echo 111; }else{ echo 3333; } die; phpinfo();die; $redis = ne ...