博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 进行国际化信息(i18n)解析
阅读量:3960 次
发布时间:2019-05-24

本文共 3151 字,大约阅读时间需要 10 分钟。

Spring Boot自动配置国际化信息解析

@ConfigurationProperties(prefix = "spring.messages")public class MessageSourceAutoConfiguration {
/** * Comma-separated list of basenames (essentially a fully-qualified classpath * location), each following the ResourceBundle convention with relaxed support for * slash based locations. If it doesn't contain a package qualifier (such as * "org.mypackage"), it will be resolved from the classpath root. */ private String basename = "messages"; //我们的配置文件可以直接放在类路径下叫messages.properties,springboot自动进行配置 @Bean public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); if (StringUtils.hasText(this.basename)) {
//设置国际化资源文件的基础名(去掉语言国家代码的) messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray( StringUtils.trimAllWhitespace(this.basename))); } if (this.encoding != null) {
messageSource.setDefaultEncoding(this.encoding.name()); } messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); messageSource.setCacheSeconds(this.cacheSeconds); messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat); return messageSource; }

application.properties下配置加载的自定义国际化配置文件名称

1. 创建国际化语言配置文件(命名规范请看:)

在这里插入图片描述

2. application.properties中配置国际化语言配置文件自定义名称(包名.自定义名称)

spring.messages.basename=i18n/index

在这里插入图片描述

3.编写properties文件

点击Resource Bundle进入资源绑定
右击创建属性
在这里插入图片描述
添加属性并为属性赋国际化语言值
在这里插入图片描述
4.使用thymeleaf中的 “ #{属性名} ” 表达式获取国际化语言值
在这里插入图片描述

注意: 如果拿不到值显示: ??login.tip_zh_CN?? 表示资源路径问题

如果配置了application.properties中的自定义国际化语言路径,就有可能是版本问题
以下是我使用的thymeleaf版本
还有一种就是将:/换成.
spring.messages.basename=i18n.index

3.0.11.RELEASE
2.2.2

5.创建区域解析器

/** * 区域信息解析器 * 作用:解析国际化语言 * 执行:每一次请求,LocaleResolver都会保存当前请求的本地化信息 * 创建人: 渣高帆 
* 创建时间: 2020/6/13 23:10
* JDK 1.8 */public class MyLocaleResolver implements LocaleResolver {
@Override public Locale resolveLocale(HttpServletRequest request) {
String area = request.getParameter("area"); //获取当前的语言环境 Locale locale = Locale.getDefault(); //如果传递的参数不为空 if(!StringUtils.isEmpty(area)){
//通过下划线分割获取当前语言代码和国家代码信息 String[] split = area.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}}

6.编写配置文件

/** * 创建人: 渣高帆 * 创建时间: 2020/6/13 11:03 * JDK 1.8 * 作用:配置类 * * @Configuration标识这是一个配置类,SpringBoot进行自动配置 */@Configurationpublic class MyMvcConfig implements WebMvcConfigurer {
/* * 静态页面跳转器 * 只能走静态页面 */ @Override public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); } /** * 作用:将区域解析器交给Spring托管 * @return 返回区域解析器 */ @Bean public LocaleResolver localeResolver() {
return new MyLocaleResolver(); }}

7.这里是使用href的形式进行切换(ajax目前还没试用)

在这里插入图片描述
每次请求都会去走一遍区域解析器,区域解析器通过传递的参数更改页面展示内容

转载地址:http://amqzi.baihongyu.com/

你可能感兴趣的文章
看看你对Linux到底了解多少?
查看>>
网上看到的:ARM入门最好的文章(转)
查看>>
中国最美情诗100句
查看>>
javascript注册window的onload事件问题研究
查看>>
客户端技术分页控件javascript+css,可用于任何服务器端技术
查看>>
学习Swing 的网站[转]
查看>>
Google App engine 的第一个应用 midispot
查看>>
提问的智慧
查看>>
关于dom4j无法解析xmlns问题及生成非UTF-8字符集乱码问题的解决
查看>>
很好的一篇文章 如果让我重做一次研究生 王汎森
查看>>
保护U盘批处理文件
查看>>
hibernate 自动导入sql 文件import.sql 国际化编码的问题的解决方案
查看>>
第七颗头骨 & 忘魂花 凤凰
查看>>
李小龙哲学之言
查看>>
[心情] 如果有一天
查看>>
[Linux] 常用 linux 系统命令及维护备忘
查看>>
[Linux] 关于 Ext4 HowTo
查看>>
[杂记] 新年物语&关于Mysql引擎性能测试
查看>>
[心得] 近期更新&关于Infobright
查看>>
[杂记] 流量统计 & 短信接口
查看>>