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严格校验,如果不了解 ...
随机推荐
- now code——处女座的期末复习
题目描述 快要期末考试了,处女座现在有n门课程需要考试,每一门课程需要花ai小时进行复习,考试的起始时间为bi,处女座为了考试可以不吃饭不睡觉,处女座想知道他能否复习完所有的科目(即在每一门考试之前复 ...
- [Xcode 实际操作]八、网络与多线程-(17)使用网址会话对象URLSession向远程服务器上传图片
目录:[Swift]Xcode实际操作 本文将演示如何通过网址会话对象URLSession向远程服务器上传图片. 网址会话对象URLSession具有在后台上传和下载.暂停和恢复网络操作.丰富的代理模 ...
- 修改jq weui自定义对话框点击确定按钮不关闭对话框
如果我们在对话框给用户输入值时,当用户输入空值点击确定按钮时,应该给个提示然后让用户继续输入. 如果在方法里使用 return false;,结果用户输入空值时对话框还是会关闭.正确做法如下: 先设置 ...
- javaScript中for-in语句
for-in语句是一种精准的迭代语句,用来枚举对象的属性 实例: <!DOCTYPE html><html><head> <title>For-In S ...
- 多次页面跳转后pop回主界面的问题
最近写代码的时候出了点BUG, 查阅资料后终于解决了. 问题原因大概是: 项目中所有的viewController都是继承自一个封装好的viewController. navigationbar, n ...
- Requests 入门
首先直接通过管理员运行cmd,然后执行 pip install requests 就可以直接安装Requests库了 有个最基本的语句 r = requests.get(url) 通过request ...
- Gym - 101810C ACM International Collegiate Programming Contest (2018)
bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...
- linux下svn服务器搭建步骤
安装步骤如下: 1.yum install subversion 2.输入rpm -ql subversion查看安装位置,如下图: 我们知道svn在bin目录下生成了几个二进制文件. 输入 sv ...
- python学习之内部函数:
python内置函数表:https://docs.python.org/3/library/functions.html 1 判断数据类型的函数:isinstance(变量, 待要判断的类型) 2判断 ...
- jQuery addClass() 源码解读
addClass: function( value ) { var classes, elem, cur, clazz, j, i = 0, len = this.length, proceed = ...