简介

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.MessageSource

    public 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.MessageSourceAutoConfiguration

  • org.springframework.context.support.AbstractApplicationContext#initMessageSource

  • ResourceBundleMessageSource

  • ReloadableResourceBundleMessageSource

    了解这些类,对问题排查会有帮助。

实战

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]

参考

Java 程序的国际化和本地化介绍

国际化资源文件命名规范

springboot-i18n国际化的更多相关文章

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

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

  2. Django1.9开发博客(12)- i18n国际化

    国际化与本地化的目的为了能为各个不同的用户以他们最熟悉的语言和格式来显示网页. Django能完美支持文本翻译.日期时间和数字的格式化.时区. 另外,Django还有两点优势: 允许开发者和模板作者指 ...

  3. Bootstrap-datepicker3官方文档中文翻译---I18N/国际化(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)

    I18N/国际化 这个插件支持月份和星期名以及weekStart选项的国际化.默认是英语(“en”); 其他有效的译本语言在 js/locales/ 目录中, 只需在插件后包含您想要的地区. 想要添加 ...

  4. 配置和修改springboot默认国际化文件

    SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...

  5. i18n(国际化) 和l18n(本地化)时的地域标识代码

    i18n(国际化) 和l18n(本地化)时的地域标识代码 格式如 zh-CN(语言-国家) i18n(国际化) 和l18n(本地化)时的地域标识代码 格式如 zh-CN(语言-国家) 国家说明 语言说 ...

  6. i18n,国际化翻译,excel与js互转

    背景 公司开发新产品时,要求适配多国语言,采用i18n国际化工具,但翻译字典(js的json)还是需要前端自己写的.字典最终需要转换成excel给专业人员翻译,翻译完成后再转换成js字典文件. 如果手 ...

  7. 【JavaWeb】i18n 国际化

    i18n 国际化 什么是 i18n 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问. 希望相同的一个网站,不同人访问的时候 ...

  8. SpringBoot系列——i18n国际化

    前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...

  9. SpringBoot整合国际化I18n

    本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...

  10. SpringBoot的国际化使用

    在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...

随机推荐

  1. Lightoj1018 【状压DP】

    题意: 给你一个坐标系,坐标系上有N个点,然后让你用最少的线,把这些点全部连起来: 思路: (1+15)*15/2=90条线: 然后线上有哪些点就可以知道: 然后按照线上点的个数排序,然后删掉这个线, ...

  2. Scut

    这是一款免费开源的游戏服务器引擎,适用于开发AVG.SLGRPG.MMOG等类型的网络游戏,同时支持Http.WebSocket和Socket协议通讯,支持Window.Mac和Linux多种平台部署 ...

  3. ios代码大全

    http://blog.csdn.net/kepoon/article/details/7763106

  4. 单片机的C语言中位操作用法2

    单片机的C语言中位操作用法 在对单处机进行编程的过程中,对位的操作是经常遇到的.C51对位的操控能力是非常强大 的.从这一点上,就可以看出C不光具有高级语言的灵活性,又有低级语言贴近硬件的特点. 这也 ...

  5. Jquery | 基础 | html()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. [題解]BZOJ_1260_塗色

    簡單的區間dp,結果竟然寫掛了......還掛的很徹底......狗屎 如果區間左右端點相等,那麼不需要在多花一次去刷,對 f [ i+1 ] [ j ],f [ i ] [ j-1 ]取個min, ...

  7. text() html() empty()

    text()             获取中间的文本  不包括标签 html()            获取标签中间的东西 包括标签 与text()是有区别的 empty()         清空标签 ...

  8. Redis的分布式锁

    一.锁的作用 当多线程执行某一业务时(特别是对数据的更新.新增)等操作,可能就会出现多个线程对同一条数据进行修改.其最终的结果一定与你期望的结果“不太一样”,这就与需要一把锁来控制线程排排队了 - j ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C

    Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ser ...

  10. JAVA常用知识总结(十一)——数据库(一)

    项目中用到的不常见sql语法 1:空值不在前的排序 select a.* from WZX_SCZY A order by SCZY_START_TIME desc nulls last (不加nul ...