更多内容,前往IT-BLOG

一、Spring 编写国际化时的步骤


【1】编写国际化配置文件;
【2】使用 ResourceBundleMessageSource 管理国际化资源文件;
【3】在页面使用 ftp:message 取出国际化内容;

二、SpringBoot编写国际化步骤


【1】创建 i18n目录,并创建 login.properties 国际化默认配置文件,同时创建 login_zh_CN.properties 系统就会自动识别到是配置国际化。就会切换到国际化视图,可以右键 Resource Bundle 'login'——Add——Add Propertie Files To Resource Bundle 快速添加其他国际化文件。
【2】可以通过视图的方式去添加国际化信息:
 
【3】编写国际化配置文件,抽取页面需要显示的国际化信息:

三、国际化原理


【1】进入 MessageSourceAutoConfiguration,发现 SpringBoot 自动配置好了管理国际化资源配置文件的组件;

 1 @ConfigurationProperties(prefix = "spring.messages")
2 public class MessageSourceAutoConfiguration {
3
4 /**
5 * 以逗号分隔的基名列表(本质上是完全限定的类路径位置),每个都遵循ResourceBundle约定,
6 * 并对基于斜线的位置。如果它不包含包限定符(例如“org.mypackage”),它将从类路径根解析。
7 */
8 private String basename = "messages";
9 //我们的配置文件可以直接放在类路径下叫messages.properties;
10
11 @Bean
12 public MessageSource messageSource() {
13 ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
14 if (StringUtils.hasText(this.basename)) {
15 //设置国际化资源文件的基础名(去掉语言国家代码的)
16 messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
17 StringUtils.trimAllWhitespace(this.basename)));
18 }
19 if (this.encoding != null) {
20 messageSource.setDefaultEncoding(this.encoding.name());
21 }
22 messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
23 messageSource.setCacheSeconds(this.cacheSeconds);
24 messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
25 return messageSource;
26 }

【2】如果有中文,需要设置编码格式

【3】如上可知,我们需要在配置文件中设置国际化资源的 basename属性:

1 <span class="hljs-comment"># i18n目录下的login文件</span>
2 spring.messages.basename=i18n.login

【4】去页面获取国际化值(红色部分,国际化用#{})(链接用@{表示}绿色部分):效果:根据浏览器语言的设置切换国际化。

 1 <!DOCTYPE html>
2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
3 <head>
4 <meta http‐equiv="Content‐Type" content="text/html; charset=UTF‐8">
5 <meta name="viewport" content="width=device‐width, initial‐scale=1, shrink‐to‐fit=no">
6
7 <meta name="description" content="">
8 <meta name="author" content="">
9 <title>Signin Template for Bootstrap</title>
10 <!‐‐ Bootstrap core CSS ‐‐>
11 <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
12
13 <!‐‐ Custom styles for this template ‐‐>
14 <link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}"rel="stylesheet">
15 </head>
16 <body class="text‐center">
17 <form class="form‐signin" action="dashboard.html">
18 <img class="mb‐4" th:src="@{/asserts/img/bootstrap‐solid.svg}" src="asserts/img/bootstrap‐solid.svg" alt="" width="72" height="72">
19 <h1 class="h3 mb‐3 font‐weight‐normal" th:text="#{login.tip}">Please signin</h1>
20 <label class="sr‐only" th:text="#{login.username}">Username</label>
21 <input type="text" class="form‐control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
22 <label class="sr‐only" th:text="#{login.password}">Password</label>
23 <input type="password" class="form‐control" placeholder="Password" th:placeholder="#{login.password}" required="">
24
25 <div class="checkbox mb‐3">
26 <label>
27 <input type="checkbox" value="remember‐me"/> [[#{login.remember}]]
28 </label>
29 </div>
30 <button class="btn btn‐lg btn‐primary btn‐block" type="submit" th:text="#{login.btn}">Sign in</button>
31 <p class="mt‐5 mb‐3 text‐muted"> 2017‐2018</p>
32 <a class="btn btn‐sm" th:href="@{/index.html(l='zh_CN')}>中文</a>
33 <a class="btn btn‐sm" th:href="@{/index.html(l='en_US')}>English</a>
34 </form>
35 </body>
36 </html>

【5】浏览器切换,能够实现国际化的原理:国际化Locale(区域信息对象),LocaleResolver(获取区域信息对象)进入WebMvcAutoConfiguration类,SpringBoot 配置了默认的 localResolve,如下:

 1 @Bean
2 @ConditionalOnMissingBean
3 @ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
4 public LocaleResolver localeResolver() {
5 if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
6 return new FixedLocaleResolver(this.mvcProperties.getLocale());
7 }
8 AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
9 localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
10 return localeResolver;
11 }

【6】当点击页面 “中文” or “English” 时切换中英文,页面参照4)信息。这是我们需要自己写一个 Locale,并加入容器中。

 1 /**
2 * 可以在连接上携带区域信息
3 */
4 public class MyLocaleResolver implements LocaleResolver {
5
6 @Override
7 public Locale resolveLocale(HttpServletRequest request) {
8 String l = request.getParameter("l");
9 Locale locale = Locale.getDefault();
10 if(!StringUtils.isEmpty(l)){
11 String[] split = l.split("_");
12 locale = new Locale(split[0],split[1]);
13 }
14 return locale;
15 }
16 @Override
17 public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
18 }
19 }

【7】将自己写的类,加入到 IOC容器中,方法的名字必须是 localeResolver,相当于 bean 的 id。以为默认的 localeResolver会判断容器中是否已经存在了 localeResolver。

1 @Bean
2 public LocaleResolver localeResolver(){
3 return new MyLocaleResolver();
4 }
 

SpringBoot——国际化的更多相关文章

  1. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  2. springboot国际化与@valid国际化支持

    springboot国际化 springboot对国际化的支持还是很好的,要实现国际化还简单.主要流程是通过配置springboot的LocaleResolver解析器,当请求打到springboot ...

  3. SpringBoot 国际化

    一.配置文件 二.application.properties 文件( 让国际化的文件被 SpringBoot 识别 ) spring.messages.basename=i18n.login 三.h ...

  4. Springboot国际化信息(i18n)解析

    国际化信息理解 国际化信息也称为本地化信息 . Java 通过 java.util.Locale 类来表示本地化对象,它通过 “语言类型” 和 “国家/地区” 来创建一个确定的本地化对象 .举个例子吧 ...

  5. 【SpringBoot】SpringBoot 国际化(七)

    本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...

  6. 【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  7. springboot国际化

    Session方式的国际化/** * @descripte 请求中如果有{@Param lang},则按照lang的格式国际化 * @descripte 请求中如果无{@Param lang},但se ...

  8. 12. SpringBoot国际化

    1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤:1).编写国际化配置文件,抽 ...

  9. 使用spring-boot 国际化配置所碰到的乱码问题

    写好html静态页面 ,  也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...

  10. Spring-boot 国际化

    在application.properties文件中配置 spring.message.basename=i18n.login 页面使用 th:text="#{login.tip}" ...

随机推荐

  1. gitee提交过程

    https://gitee.com/ 一个线上代码云端软件开发协作平台 首先注册一个账号 然后添加新的仓库 仓库名称和路径是必填项 然后创建项目 选择  克隆存储数据库 存储库位置是网站获取的git位 ...

  2. 【BOOK】正则表达式

    正则表达式 1. 开源中国-正则表达式测试工具:https://tool.oschina.net/regex/ 2. 匹配规则 3. match() 从字符串起始位置匹配正则表达式 若从起始位置匹配不 ...

  3. jmeter中response data出现乱码的解决方法

    步骤如下:1.进入jmeter\apache-jmeter-5.1.1\bin,打开jmeter.properties2.jmeter.properties中搜索"sampleresult. ...

  4. not eligible for getting processed by all BeanPostProcessors

    描述 这个BUG大的起源是我上线以后,在后台看日志的时候发现一行奇怪的INFO日志: 2022-06-09 23:34:24 [restartedMain] [org.springframework. ...

  5. Apple Sources

    1. libsystem_malloc.dylib的源码 https://opensource.apple.com/tarballs/libmalloc/ .这里有多个版本(例如用otool找到iOS ...

  6. 修改mysql 一张表中某列字段值

    UPDATE  表名 SET  字段名 = replace(字段名,'原来值','修改值'): 例: UPDATE pd_purchase SET type_status =replace(type_ ...

  7. c++学习3 转义字符

    一 "/"和某些字符的结合,产生新的字符就叫转义字符. '\0'==ASCII码值的"0". '\n'==换行符. '\t'==tab缩进符. '\a'==发出 ...

  8. Linux磁盘相关工具 -- iostat

    iostat主要用于监控系统设备的IO负载情况,根据这个可以看出当前系统的写入量和读取量,CPU负载和磁盘负载. iostat主要用于输出磁盘IO和CPU统计信息. 1. iostat用法: iost ...

  9. Restful Fast Request 添加前置脚本,实现不同环境免设置token 直接请求

    idea安装Restful Fast Request插件后,进行如下设置,并打开 项目全局参数 对话框 进入前置脚本 tab 编写如下groovy脚本代码(插件脚本语言默认支持groovy,该语言被称 ...

  10. WLAN - AP上线

    1 保证AC,AP互通2 AP上线capwap 1 AP组创建 2 管理域模板 3 AC组和管理域模板绑定 4 指定AC的接口 5 导入AP3 WALN的业务配置 1 安全模板 2 SSID 模板 3 ...