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严格校验,如果不了解 ...
随机推荐
- 小程序隐藏或自定义 scroll-view滚动条
css 隐藏滚动条 ::-webkit-scrollbar { width:; height:; color: transparent; } 自定义滚动条样式 ::-webkit-scrollbar ...
- UnityEngine中Animator相关类的说明
---------------------------------------------------------------------- Animator 这个单独写,比较多 AnimationC ...
- 蓝桥杯T42(八数码问题)
题目链接:http://lx.lanqiao.cn/problem.page?gpid=T42 题意:中文题诶- 思路:bfs 将没种九宫格的状态看作一个状态节点,那么只需求起始节点到目标节点的最短路 ...
- js对象—类型和属性特性
前言 权威指南中摘要的,工作中用不到的,重要的js基础. 三类对象两类属性 内置对象(native object) 是由ECMScript规范定义的对象或者类.例如:函数,数组,日期,正则... 宿主 ...
- mysql 配置大小写敏感后不能重启
[问题现象] 在mysql的配置文件my.cnf配置lower_case_table_names=1后不能重启mysql,提示mysql.serviceJob for mysql.service fa ...
- HDU 1875(最小生成树)
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> ...
- collections 中 typing 中对象的引用
from typing import ( Callable as Callable, Container as Container, Hashable as Hashable, Iterable as ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D
Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlo ...
- [已读]javascript语言精粹
又是一本广为赞颂的好书,当时才刚开始学,啃这本书的时候于是比较困难.记得是有介绍一些js中的“精华”与“糟粕”.
- PHP采集利器 Snoopy 试用心得
Snoopy是什么? Snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务. Snoopy的一些特点: * 方便抓取网页的内容 * 方便抓取网页的文本内容 (去 ...