SpringBoot国际化配置
在resource文件下添加如下文件
在login.properties文件下添加键值对
因为国际化的相关类对应的properties对应的文件中
basename是国际化配置文件的真实位置
所以在application.properties配置
spring.messages.basename=i18n.login
编写页面
注意点 国际化的值需要使用#{}
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>登录</title> </head> <body><h1 th:text="#{login.tip}"></h1><form method="post"><input type="text" th:placeholder="#{login.username}" name="username"><br><input type="password" th:placeholder="#{login.password}" name="password"><br><button type="submit">[[#{login.btn}]]</button><br><a th:href="@{/login.html(l='zh_CN')}">中文</a><a th:href="@{/login.html(l='en_US')}">English</a></form> </body> </html>
配置解析器
1.原理
在Spring中有一个国际化的Locale (区域信息对象);里面有一个叫做LocaleResolver (获取区域信息对象)的解析器!
我们去我们webmvc自动配置文件,寻找一下!看到SpringBoot默认配置:
@Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "spring.mvc", name = "locale") public LocaleResolver localeResolver() {// 容器中没有就自己配,有的话就用用户配置的if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver(this.mvcProperties.getLocale());}// 接收头国际化分解AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();localeResolver.setDefaultLocale(this.mvcProperties.getLocale());return localeResolver; }
AcceptHeaderLocaleResolver 这个类中有一个方法
public Locale resolveLocale(HttpServletRequest request) {Locale defaultLocale = this.getDefaultLocale();// 默认的就是根据请求头带来的区域信息获取Locale进行国际化if (defaultLocale != null && request.getHeader("Accept-Language") == null) {return defaultLocale;} else {Locale requestLocale = request.getLocale();List<Locale> supportedLocales = this.getSupportedLocales();if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) {Locale supportedLocale = this.findSupportedLocale(request, supportedLocales);if (supportedLocale != null) {return supportedLocale;} else {return defaultLocale != null ? defaultLocale : requestLocale;}} else {return requestLocale;}} }
那假如我们现在想点击链接让我们的国际化资源生效,就需要让我们自己的Locale生效!
我们去自己写一个自己的LocaleResolver,可以在链接上携带区域信息!
修改一下前端页面的跳转连接:
<a th:href="@{/login.html(l='zh_CN')}">中文</a> <a th:href="@{/login.html(l='en_US')}">English</a>
编写MyLocalResolver
public class MyLocalResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest request) {//获取参数值以此来获取国家和地区值String language = request.getParameter("l");//获取默认的国家和地区信息Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(language)){String[] array = language.split("_");//封装成localelocale = new Locale(array[0],array[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {} }
注册到Config中
@Configuration //@EnableWebMvc public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {}@Beanpublic LocaleResolver localeResolver(){return new MyLocalResolver();}}
配置Controller,映射地址
@RequestMapping({"login","login.html"}) public String international() {return "login"; }
测试
SpringBoot国际化配置相关推荐
- SpringBoot国际化配置(i8n配置)未生效(完美解决)
最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...
- springboot国际化配置中英文切换
1.新建项目 第一步新建springboot项目,勾选web依赖,或者后面进入在pom.xml中加入也可以. 2.项目结构 项目pom.xml <?xml version="1.0&q ...
- 自学SpringBoot之国际化配置相关的坑
前两天在b站上自学SpringBoot的国际化配置时,浏览器总是报乱码的错,类似??login.tip_zh_CN??, 由于b站上基本都是使用Ideal,我用的是sts2.2.5版本,难免遇到不同, ...
- SpringBoot实现i18n国际化配置(超详细之跟着走就会系列)
一.新增国际化资源文件 在resources文件下新建i18n文件,并新建国际化资源文件.如图: 点击新增Resource Bundle文件. 我们在Resource bundle base name ...
- springboot国际化i18n
springboot国际化 支持多国语言展示 1.自动式国际化:通过浏览器发送请求头携带的信息自动使用国际化 2.手动式国际化:通过点击按钮,动态更改国际化 由于springboot自动配置中已集成国 ...
- SpringBoot的配置详解application
SpringBoot的配置文件application有两种文件格式,两种配置的内容是一致的,只是格式不一致. 1.application.properties 2.application.yml或者a ...
- springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解
在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...
- springboot国际化04
springboot国际化 1 文件结构 2 messages.properties 3 优先级 4 在application.yml中配置 5 其他文件使用 6 浏览器渲染乱码 1 文件结构 Res ...
- Spring Boot Validation提示信息国际化配置
引言 之前介绍过Spring Boot Validation的使用及扩展,可参见:<SpringBoot Validation> 本文在此基础上重点讲解下Spring Boot Valid ...
最新文章
- 小微贷是美团的上坡之路?
- pandas使用max函数和min函数计算dataframe日期(时间)数据列中最大日期和最小日期对应的数据行(maximum and minimum date or time row)
- Rancher第一款Kubernetes操作系统推出
- IntelliJ IDEA License Server 本地搭建教程
- 为MFC中的ListBox添加水平滚动条
- jQuery新版本加载json注意事项。
- MATLAB读取text文件数据,拟合曲线
- 卸载python的正确姿势
- div超出部分点点显示
- phoenix的元数据一般存在哪里_Phoenix常用操作
- 爱奇艺开播助手Flutter跨平台Hybrid实践\n
- excel打不开html格式的文件,excel文件打不开提示格式无效怎么解决【图文】
- Mysql索引失效的几种情况总结
- Excel·VBA单元格重复值标记颜色
- 爬虫学习分享Python下载mp4文件(源码)
- Matplotlib 设置支持中文的默认字体
- 重要知识:身份证复印件的正确使用方法
- shell脚本--杀死进程
- 电影票房排行 API数据接口
- Python爬虫+可视化实例:网易云音乐歌单