目录

理论

源码


理论

通过获取浏览器中get方法的参数,构造LocaleResolver,从LocaleResolver中调用对应的properties文件。

在MvcConfig中创建这个自己定义的LocaleResolver类!

程序运行截图如下:

源码

程序结构如下:

MyLocaleResolver.java

package internationdemotest.demo.component;import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;public class MyLocaleResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String l = httpServletRequest.getParameter("l");Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(l)){String[] split = l.split("_");locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {}
}

MyMvcConfig.java

package internationdemotest.demo.config;import internationdemotest.demo.component.MyLocaleResolver;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter{@Beanpublic WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("index.html").setViewName("index");}};return adapter;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}
}

index.properties

index.hehe=呵呵#

index_en_US.properties

index.hehe=hehehehehehhehehehehe

index_zh_CN.properties

index.hehe=呵呵

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1 th:text="#{index.hehe}">呵呵</h1><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></body>
</html>

application.properties

spring.messages.basename=i18n.index

Spring Boot通过url设置国际化相关推荐

  1. Spring Boot 架构中的国际化支持实践—— Spring Boot 全球化解决方案

    背景 Spring Boot 主要通过 Maven 或 Gradle 这样的构建系统以继承方式添加依赖,同时继承了 Spring 框架中的优秀元素,减少了 Spring MVC 架构中的复杂配置,内置 ...

  2. 【spring boot】url中传递session id

    前言 tomcat spring boot url中传递session id 默认情况,session id 是通过 cookie 传递的. 使用 cookie 传递 session id 时,默认的 ...

  3. spring boot 缓存redis设置定时过期时间

    前言 本篇文章分享的就是spring boot中的一个轮子,spring cache注解的方式实现接口数据缓存.默认的配置想非常简单,但是有一个弊端是缓存数据为永久缓存,本次将介绍如何设置接口缓存数据 ...

  4. Spring Boot 的logo设置(八)

    Spring Boot 工程启动时会显示一个logo, 我们可以对显示的logo进行相关的设置. 关闭启动logo 想关闭logo可以使用以下两种方法: 在 Spring Boot 的启动类中关闭 i ...

  5. spring boot mail 发送 设置代理服务器

    背景: 公司使用spring cloud 微服务  docker + Rancher + drone + maven 自动化部署. 网络环境 内网环境内部微服务必须通过代理服务器对外访问.邮件发送 必 ...

  6. Spring Boot 启动Banner设置--启动欢迎词!

    Spring Boot启动时会显示如下图标: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | ...

  7. Spring Boot 图片url转换为base64字符串

    通过图片链接将图片转换为base64编码字符串 之前直接向前端传图片文件,遇到跨域问题,一直没有解决,干脆将图片的base64字符串给前端了 参数:String类型,imgurl 图片链接 返回值:S ...

  8. springboot 创建地址_使用 SpringBoot Admin监控Spring Boot 服务

    简介 SpringBoot-Amind是什么?Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件.,可监控的信息包含:应用状态.内存.线程.堆栈等等,比较 ...

  9. Spring Boot国际化(i18n)

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

最新文章

  1. s3c2410上搭建QT/Embedded4.8.5开发环境(四)--安装intel-x86 X11平台qt库qt-everywhere-opensource-src-4.8.5...
  2. linux u盘 挂载 type,Linux挂载U盘报错:mount: unknown filesystem type 'ntfs'
  3. delegate委托
  4. 韶音耳机连不上电脑_运动耳机什么牌子音质好,运动耳机品牌排行榜
  5. LPSTR与char*的转换
  6. vue aplayer 进度条无法拖动_Vue-rideo-player视频播放插件的使用
  7. jenkins java常用插件下载,Jenkins简单介绍以及插件入门
  8. c调用python第三方库_Python使用ctypes模块调用DLL函数之C语言数组与numpy数组传递...
  9. 从0开始学Python,0基础小白
  10. 速度更新!GoCD又曝仨洞,极易遭利用且结合利用可成供应链攻击的新跳板
  11. 卸载oracle9i精简版,oracle9i精简客户端|oracle9i客户端精简版下载_红软基地
  12. PyCharm安装配置Django,启动第一个Django项目Hello World
  13. 【OpenCV学习笔记】【编程实例】一(VS2010/MFC+OPenCV2.4.7读取图片和摄像头)
  14. HTTP1.1与HTTP1.0
  15. html 的scor属性,HTML DOM scrollTop 属性
  16. excel求回归直线方程的公式_“如何在excel中求解任意直线的方程“excel绘制线性回归方程...
  17. JavaScript 3D实时散点图
  18. 苹果M1如何安装Rosetta
  19. Spring MVC的数据绑定(简单数据绑定+POJO类型绑定)
  20. D3 二维图表的绘制系列(十九)封闭图

热门文章

  1. 程序员年薪高达40万,为什么有很多程序员单身?看看网友怎么说!
  2. 飞鸽传书不能用?这里列出所有解决方法!
  3. 【转载】指导教师的局域网聊天
  4. 安全和anti-sapm的HTML5效果如何
  5. 倾听用户呼吁Skype决定开源
  6. Windows函数错误处理
  7. 传闻中“日进斗金”的程序员,你真的了解吗?看完还想当程序员?
  8. 来前端君朋友圈,一起聊前端面试技巧
  9. php脚本哪里找,我在哪里可以找到PHP-FPM RC脚本?
  10. matlab 符号 约束,非线性优化 问题约束函数带有符号的条件函数怎么写?