Spring Boot通过url设置国际化
目录
理论
源码
理论
通过获取浏览器中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设置国际化相关推荐
- Spring Boot 架构中的国际化支持实践—— Spring Boot 全球化解决方案
背景 Spring Boot 主要通过 Maven 或 Gradle 这样的构建系统以继承方式添加依赖,同时继承了 Spring 框架中的优秀元素,减少了 Spring MVC 架构中的复杂配置,内置 ...
- 【spring boot】url中传递session id
前言 tomcat spring boot url中传递session id 默认情况,session id 是通过 cookie 传递的. 使用 cookie 传递 session id 时,默认的 ...
- spring boot 缓存redis设置定时过期时间
前言 本篇文章分享的就是spring boot中的一个轮子,spring cache注解的方式实现接口数据缓存.默认的配置想非常简单,但是有一个弊端是缓存数据为永久缓存,本次将介绍如何设置接口缓存数据 ...
- Spring Boot 的logo设置(八)
Spring Boot 工程启动时会显示一个logo, 我们可以对显示的logo进行相关的设置. 关闭启动logo 想关闭logo可以使用以下两种方法: 在 Spring Boot 的启动类中关闭 i ...
- spring boot mail 发送 设置代理服务器
背景: 公司使用spring cloud 微服务 docker + Rancher + drone + maven 自动化部署. 网络环境 内网环境内部微服务必须通过代理服务器对外访问.邮件发送 必 ...
- Spring Boot 启动Banner设置--启动欢迎词!
Spring Boot启动时会显示如下图标: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | ...
- Spring Boot 图片url转换为base64字符串
通过图片链接将图片转换为base64编码字符串 之前直接向前端传图片文件,遇到跨域问题,一直没有解决,干脆将图片的base64字符串给前端了 参数:String类型,imgurl 图片链接 返回值:S ...
- springboot 创建地址_使用 SpringBoot Admin监控Spring Boot 服务
简介 SpringBoot-Amind是什么?Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件.,可监控的信息包含:应用状态.内存.线程.堆栈等等,比较 ...
- Spring Boot国际化(i18n)
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...
最新文章
- s3c2410上搭建QT/Embedded4.8.5开发环境(四)--安装intel-x86 X11平台qt库qt-everywhere-opensource-src-4.8.5...
- linux u盘 挂载 type,Linux挂载U盘报错:mount: unknown filesystem type 'ntfs'
- delegate委托
- 韶音耳机连不上电脑_运动耳机什么牌子音质好,运动耳机品牌排行榜
- LPSTR与char*的转换
- vue aplayer 进度条无法拖动_Vue-rideo-player视频播放插件的使用
- jenkins java常用插件下载,Jenkins简单介绍以及插件入门
- c调用python第三方库_Python使用ctypes模块调用DLL函数之C语言数组与numpy数组传递...
- 从0开始学Python,0基础小白
- 速度更新!GoCD又曝仨洞,极易遭利用且结合利用可成供应链攻击的新跳板
- 卸载oracle9i精简版,oracle9i精简客户端|oracle9i客户端精简版下载_红软基地
- PyCharm安装配置Django,启动第一个Django项目Hello World
- 【OpenCV学习笔记】【编程实例】一(VS2010/MFC+OPenCV2.4.7读取图片和摄像头)
- HTTP1.1与HTTP1.0
- html 的scor属性,HTML DOM scrollTop 属性
- excel求回归直线方程的公式_“如何在excel中求解任意直线的方程“excel绘制线性回归方程...
- JavaScript 3D实时散点图
- 苹果M1如何安装Rosetta
- Spring MVC的数据绑定(简单数据绑定+POJO类型绑定)
- D3 二维图表的绘制系列(十九)封闭图