简介

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. iOS11 与 iPhone X适配的那些坑(持更中...)

    目录 问题列表 1.适配iPhoneX 屏幕原则 2.适配过程一些常量的设置 3..iPhone X 上运行有黑色区域问题 4.iOS11导航栏适配 5.出现UIScrollview 漂移问题(基本都 ...

  2. 使用dynamic关键词 CS1969错误

    添加 Microsoft.CSharp.dll 引用即可 不需要添加using Microsoft.CSharp 这类namespace

  3. 百度搜索:有关Baiduspider的10个问题

    猫宁!!! 参考链接: http://help.baidu.com/question?prod_id=99&class=476&id=2996 https://ziyuan.baidu ...

  4. Validation(1)

    站在巨人的肩膀上 Java Bean Validation 最佳实践 参数校验是我们程序开发中必不可少的过程.用户在前端页面上填写表单时,前端js程序会校验参数的合法性,当数据到了后端,为了防止恶意操 ...

  5. DRF教程5-API接口文档和接口测试

    配置 配置好,就可以访问web页面 pip install coreapi #安装依赖 from rest_framework.documentation import include_docs_ur ...

  6. js_jquery

    引用 jQuery 是一个 JavaScript 库,不需要安装,直接引用就行 <!-- jQuery --> <script src="/static/vendors/j ...

  7. POJ-1258-Agri Ned

    链接:https://vjudge.net/problem/POJ-1258#author=fuxianda 题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么 ...

  8. linux资源性能指标

    1.cpu Running:正在运行的进程 Waiting:已准备就绪,等待运行的进程 Blocked:因为等待某些事件完成而阻塞的进程,通常在等待I/O  命令获取数据: vmstat 1其中: u ...

  9. 18000 Two String 暴力。——— 读题

    http://acm.scau.edu.cn:8000/uoj/mainMenu.html 18000 Two String 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 ...

  10. centos7 更换jdk版本

    查看java版本   java -version 如果有java版本(如果没有直接看红色虚线以下的) 输入 rpm -qa | grep java会显示以下几条内容: ******* ******** ...