springboot-i18n国际化
简介
In computing, internationalization and localization are means of adapting computer software to different languages, regional peculiarities and technical requirements of a target locale.
术语
i18n
internationalization
英 [ˌɪntəˌnæʃnəlaɪ'zeɪʃn],国际化。由于首字母"i"和末尾字母"n"间有18个字符,所以简称i18n。
internationalization指为了使应用程序能适应不同的语言和地区间的变化而不作系统性的变化所采取的设计措施。
l10n
localization, 本地化。
由于首字母"l"和末尾字母"n"间有10个字母,所以简称l10n。
localization指为了使应用软件能够在某一特定语言环境或地区使用而加入本地特殊化部件和翻译后文本的过程。locale: 指语言和区域进行特殊组合的一个标志
一般语言_地区可以确定一个特定类型的本地化信息。
基名_语言_地区.properties
- 语言由两个小写字母表示,具体代码是由ISO-639标准定义。
- 地区由两个大写字母表示,由ISO-3166标准定义的。
- 基名,basename,一般是业务代码。例如:
ValidationLogin.properties
常用配置:
i18n_zh_CN.properties:中国大陆的,中文语言的资源
i18n_en_US.properties:美国地区,英文语言的资源
i18n.properties:默认资源文件,如果请求相应的资源文件不存在,将使用此资源文件
JDK的支持
java.util.Local
SpringBoot的支持
org.springframework.context.MessageSourcepublic interface MessageSource {
String getMessage(String var1, Object[] var2, String var3, Locale var4); String getMessage(String var1, Object[] var2, Locale var3) throws NoSuchMessageException; String getMessage(MessageSourceResolvable var1, Locale var2) throws NoSuchMessageException;
}
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfigurationorg.springframework.context.support.AbstractApplicationContext#initMessageSourceResourceBundleMessageSourceReloadableResourceBundleMessageSource了解这些类,对问题排查会有帮助。
实战
SpringBoot提供两种配置方式。
Java配置
重新定义org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration#messageSource中的bean。
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
application配置
spring.messages.basename=i18n
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration#messageSource 根据配置文件,自动创建beanmessageSource.


代码演示
文件配置:
email.server=mail.163.com

RestController
@RestController
public class MessageSourceTestController {
@Autowired
private MessageSource messageSource;
@GetMapping("/mail")
public String getUsers() {
return messageSource.getMessage("email.server", null, Locale.CHINA);
}
}
请求接口,获取配置文件中的值。

遇到的问题
接手一个老项目,restful框架还是自研的,切换为springboot框架后,国际化配置遇到些问题。
整理如下。
MessageSource null
org.springframework.context.support.AbstractApplicationContext#initMessageSource,初始化时,没有检测到messageSourceBean,会默认提供一个空的实现DelegatingMessageSource。

解决办法,直接实例化,一个bean。参考上文java配置
@ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)
应该是个bug,参考Spring Boot MessageSourceAutoConfiguration,实际开发中,可忽略。
debug=true
application.properties中添加debug=true,在应用启动的时候,会把自动化配置、bean等信息打印出来。
2019-06-13 14:03:42.740 DEBUG 18680 --- [ main] ationConfigEmbeddedWebApplicationContext : Using MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[messages/messages]]
MessageSourceAutoConfiguration matched:
- ResourceBundle found bundle URL [file:/D:/git/github/spring-boot-samples/spring-boot-sample-il8n/target/classes/messages/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)
- @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)
没有检测到messageSourceBean,会默认提供一个空的实现
[2019-06-13 14:19:43.453] [main] [DEBUG] [igServletWebServerApplicationContext:739 ] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3f2ab6ec]
参考
springboot-i18n国际化的更多相关文章
- SpringBoot 国际化配置,SpringBoot Locale 国际化
SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...
- Django1.9开发博客(12)- i18n国际化
国际化与本地化的目的为了能为各个不同的用户以他们最熟悉的语言和格式来显示网页. Django能完美支持文本翻译.日期时间和数字的格式化.时区. 另外,Django还有两点优势: 允许开发者和模板作者指 ...
- Bootstrap-datepicker3官方文档中文翻译---I18N/国际化(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)
I18N/国际化 这个插件支持月份和星期名以及weekStart选项的国际化.默认是英语(“en”); 其他有效的译本语言在 js/locales/ 目录中, 只需在插件后包含您想要的地区. 想要添加 ...
- 配置和修改springboot默认国际化文件
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...
- i18n(国际化) 和l18n(本地化)时的地域标识代码
i18n(国际化) 和l18n(本地化)时的地域标识代码 格式如 zh-CN(语言-国家) i18n(国际化) 和l18n(本地化)时的地域标识代码 格式如 zh-CN(语言-国家) 国家说明 语言说 ...
- i18n,国际化翻译,excel与js互转
背景 公司开发新产品时,要求适配多国语言,采用i18n国际化工具,但翻译字典(js的json)还是需要前端自己写的.字典最终需要转换成excel给专业人员翻译,翻译完成后再转换成js字典文件. 如果手 ...
- 【JavaWeb】i18n 国际化
i18n 国际化 什么是 i18n 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问. 希望相同的一个网站,不同人访问的时候 ...
- SpringBoot系列——i18n国际化
前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...
- SpringBoot整合国际化I18n
本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...
- SpringBoot的国际化使用
在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...
随机推荐
- [WIP]C语言 realloc的坑
创建: 2019/01/07 题外话,不知不觉又一年过去了,2019也要好好努力. 回到主题,在用动态循环数组实现queue的时候, 由于realloc的原因出现了一些莫名其妙的错误. 先开个题,晚点 ...
- 洛谷 - P2158 - 仪仗队 - 欧拉函数
https://www.luogu.org/problemnew/show/P2158 好像以前有个妹子收割铲也是欧拉函数. 因为格点直线上的点,dx与dy的gcd相同,画个图就觉得是欧拉函数.但是要 ...
- unity 引入 android第三方sdk
unity中调用java代码中介绍了unity调用android java代码的一些基础.引入android开发第三方sdk的操作跟调用java代码的操作相似,只是多了一步引入第三方jar. unit ...
- jq weui 滚动加载的坑
1.一般情况下使用官网给个demo就可以了,如下: var loading = false; //状态标记 $(document.body).infinite().on("infinite& ...
- django框架知识3
1.Django安装: 1.安装版本Django1.11.11 2.安装方式:命令行安装 pycharm安装 2.创建Django项目 1.创建方式:cd到你要创建的目录下 然后输入 Djang ...
- [Usaco2011 Dec]Grass Planting
Description Farmer John has N barren pastures connected by N-1 bidirectional roads, such that there ...
- vue 生命周期钩子函数
实例中的生命周期钩子可以分为以下8种情况: beforeCreate: 实例刚被创建,vue所有属性都还不存在 created: 实例创建完成,但$el还不存在 beforeMount:挂载之前 mo ...
- js动态实现文本框不可编辑状态
两种方法: $("#id").attr("readOnly",false); 不可编辑,可以传值 $("#id").attr("d ...
- Codeforces 1132G(关系转化树+dfn+线段树)
要点 显然要滑动修改维护. 像通常的数列next关系一样建边(单调栈预处理),因为贪心所以是树,然后发现增删只会影响区间内的子(or父,看你连边方向行事)节点,于是使用dfs序建线段树. 为了正确地修 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第七天(非原创)
文章大纲 一.课程介绍二.Redis基础实战三.Redis之高可用.集群.云平台搭建实战四.淘淘商城Jedis整合spring五.项目源码与资料下载六.参考文章 一.课程介绍 一共14天课程(1) ...