国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母);

主要涉及3个类: Locale用来设置定制的语言和国家代码

       ResourceBundle 用来加载国际化资源文件

       MessageFormat 用来格式化占位符

//先看结构:

在创建国际化资源文件时,如我在resources文件下建了一个i18n文件夹,下放了3个资源文件,idea自动会生成一个Resource Bundle 'messages'文件夹,这个文件夹不是真实存在的,打开项目所在文件夹后,找不到它的;如图:

资源文件的命名格式:前缀+"_"+"语言代码"+地区代码+".properites";(

Locale[] locales = Locale.getAvailableLocales();//获取世界可用的语言地区表示

)

如上图所示,我定义了3个资源文件夹,其中一个没有语言代码和地区代码的那个是默认资源,也就是要找的资源文件都不存在时,就会去那里找;

查找顺序,如果你指定Locale locale=new Locale("en_US"),那它就会找到messages_en_US.properties,如果你写错了一个单词Locale locale=new Locale("en_USAA");找不到的话,它就会找本地的语言(

Locale locale2 = Locale.getDefault(); //获取默认语言地区

),如我们在中国就会找本地的中国语言包messages_zh_CN.properties,如果没有本地的语言包messages_zh_CN.properties,就会去messages.properties这里找

资源文件的内容格式为:key=value,其中value值可以含有占位符,格式{数字},例如" 你的订单号{0}的订单,金额{1}元";

key和value值在properties文件中都不用加双引号

注意:

资源文件中的所有字符都必须是 ascll 码 ,不能保存为中文的 ,Java 中提供 了 native2ascll 工具用于将中文转化为 ascll 码 。所以在编写 properties 文件的时候写的是中文 ,一回车就自动被编码了 。

在idea工具中,可以设置显示为中文:

后端做国际化用得比较多的情况是错误码国际化:具体代码如下:

 @Test
public void test2(){
Locale locale1 = new Locale("en_US");
Locale locale2 = Locale.getDefault(); //获取默认语言地区
System.out.println(locale2.getCountry()); //CN
System.out.println(locale2.getDisplayCountry()); //中国
System.out.println(locale2.getDisplayLanguage()); //中文
System.out.println(locale2.getDisplayName()); //中文(中国)
System.out.println(locale2.getDisplayLanguage(locale1)); //以某种语言显示语言,这里是Chinese
System.out.println(locale2.getLanguage()); //zh 语言代表符
System.out.println(locale2.toLanguageTag()); //语言标签 格式语言-国家 这里是zh-CN
//Locale自定义了很多语言跟国家常量 如中国 和中文,德国和德文
Locale china = Locale.CHINA; // zh-Cn
Locale chinese = Locale.CHINESE; //ZH
System.out.println(china.toLanguageTag()); //ZH-CN
System.out.println(chinese.toLanguageTag()); //ZH
Locale german = Locale.GERMAN; //de
Locale germany = Locale.GERMANY; //de-DE
System.out.println(german.toLanguageTag());//de
System.out.println(germany.toLanguageTag());//de-DE
Locale[] locales = Locale.getAvailableLocales();//获取世界可用的地区
for (Locale locale : locales) {
System.out.println(locale.toLanguageTag());
}
ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n/messages",locale2); //这里的baseName也可以表示成i18n.messages形式,messages是虚拟的包名 String s = resourceBundle.getString("103014"); //我在资源文件存了:103014=订单号为{0}的订单已经支付
MessageFormat messageFormat = new MessageFormat(s);
String format = messageFormat.format(new String[]{"100002222"});
System.out.println(format);//输出值(订单号为{100002222}的订单已经支付) } //稍微封装:
private String getErrorMessage(String language,String errorCode,String ...params){
Locale locale=null;
if(StringUtils.isEmpty(language)){
locale=new Locale("zh_CN");
} locale=new Locale(language);
ResourceBundle bundle = ResourceBundle.getBundle("i18n/messages",locale);
String msg = bundle.getString(errorCode);
if(StringUtils.isEmpty(msg)){
return null;
}
MessageFormat messageFormat = new MessageFormat(msg);
String format = messageFormat.format(params);
return format; }
@Test
public void testI18n() throws IOException { //key=value 103032=Order No.{0} and No.{1} has not been linked
System.out.println(getErrorMessage("en_US", "103032", "dddd", "vvvvvv")); }//输出为Order No.dddd and No.vvvvvv has not been linked

补充:注意下面的区别:下划线和横线用在不同方法,建议使用

Locale locale = new Locale("en_US");

Locale locale2 =Locale.forLanguageTag("en-US"); //推荐使用这种方式
 

//springboot项目中使用国际化,非常简单:

@RunWith(SpringRunner.class)
@SpringBootTest
public class JestTest {
@Autowired
private MessageSource messageSource; @Test
public void testI18n() throws IOException { Locale locale2 = Locale.forLanguageTag("en-US"); Object[] arr = {"dddd", "vvvvvv"};
//key=value 103032=Order No.{0} and No.{1} has not been linked
System.out.println(messageSource.getMessage("103032", arr, locale2));
//输出结果:Order No.dddd and No.vvvvvv has not been linked } }
需要在application.yml中配置:
messages:
always-use-message-format: false # Whether to always apply the MessageFormat rules, parsing even messages without arguments.
basename: i18n/messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations.
cache-duration: # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
encoding: UTF-8 # Message bundles encoding.
fallback-to-system-locale: true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
use-code-as-default-message: false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.
如果报这个:org.springframework.context.NoSuchMessageException: No message found under code '103032' for locale 'en_us'
非常有可能是上面的messages在application.yml的位置没配置对,messages要配置在spring的下一级,如图所示:

 

错误配置如下:

 

												

国际化的实现i18n--错误码国际化以及在springboot项目中使用的更多相关文章

  1. Springboot项目中Pom.xml报错

    摘要:使用idea,两次在maven上浪费时间,第一次不知道怎么就解决了,第二次记录一下解决办法 参考博客地址: https://blog.csdn.net/u013129944/article/de ...

  2. springboot项目中使用spring-data-Redis对map序列化时报错

    错误信息: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at org.spri ...

  3. springboot项目中常遇到的问题-初学者最容易犯的错

    1.在spring中有两个main方法 2.在idea中少提代码类了,或者某类中代码依赖关系没解决掉

  4. 安装APK的错误码(PackageManager.java)

    安装APK的错误码,定义在android源码中的这个文件中:frameworks\base\core\java\android\content\pm\PackageManager.java /** * ...

  5. jQuery国际化插件 jQuery.i18n.properties 【轻量级】

    jQuery.i18n.properties是一款轻量级的jQuery国际化插件,能实现Web前端的国际化. 国际化英文单词为:Internationalization,又称i18n,“i”为单词的第 ...

  6. Web前端国际化之jQuery.i18n.properties

    Web前端国际化之jQuery.i18n.properties jQuery.i18n.properties介绍 国际化是如今Web应用程序开发过程中的重要一环,jQuery.i18n.propert ...

  7. php gettext方式实现UTF-8国际化多语言(i18n)

    php gettext方式实现UTF-8国际化多语言(i18n) 一.总结 一句话总结: 二.php gettext方式实现UTF-8国际化多语言(i18n) 近 来随着i18n(国际化)的逐渐标准化 ...

  8. jquery.i18n.properties前端国际化方案

    如果新项目要做系统国际化, 时下热门的任何一种技术选型都有成熟的方案,比如: vue + vue-i18n angular + angular-translate react + react-intl ...

  9. 【转】i18n实现前端国际化(实例)

    源地址:https://www.jianshu.com/p/ea93efef5155 i18n实现前端国际化(实例) 0.1442018.08.27 16:25:10字数 246阅读 10563 在今 ...

随机推荐

  1. Arduboy基本操作(二)

    Arduboy基本操作(二) 方向键控制物体移动 #include<Arduboy.h> Arduboy arduboy; int i,j; void setup() { arduboy. ...

  2. shell脚本同步私人git仓库

    前言 分别在个人电脑.个人服务器.码云三个地方建立了数据仓库用于保存自己的各种数据,通过git+shell进行数据同步. 此举不仅可以避免因存储损坏.版本更迭.数据误操作等因素带来的各种麻烦,也能实现 ...

  3. TX-LCN 分布式事务框架

    第十章 TX-LCN 分布式事务框架 (Spring Cloud 高级) 一. 什么是分布式事务 分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位 于不同的分布式系统的不同 ...

  4. MySQL主从同步简单介绍&配置

    介绍: 现在mysql集群基本上都是使用一主多从方式,实现读写分离(主库写.从库读).负载均衡.数据备份,以前只是使用从未配置过,今天简单配置一下! mysql主从复制是通过binary log日志实 ...

  5. Apache Tika实战

    Apache Tika实战 Tika 简介 Apache Tika 是一个内容分析工具包,可以检测上千种文件类型,并提取它们的元数据和文本.tika在设计上十分精巧,单一的接口使它易于使用,在搜索引擎 ...

  6. 你可能不了解的java枚举

    枚举在java里也算个老生长谈的内容了,每当遇到一组需要类举的数据时我们都会自然而然地使用枚举类型: public enum Color { RED, GREEN, BLUE, YELLOW; pub ...

  7. Unity坑之 加了Rigidbody后主角反而朝天上飞?

    今儿在做项目的时候,给主角加上一个Rigidbody组件,设置如下图: 然后问题来了,我本来是想让主角通过重力控制,掉到地上,但是加上之后,主角反而朝着天上飞! 这TM什么鬼? 经过多番探查,发现是A ...

  8. [PyTorch 学习笔记] 5.1 TensorBoard 介绍

    本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson5/tensorboard_methods.py http ...

  9. Java 中基本数据类型的变量的转换规则

    基本数据类型之间的转换 变量之间的转换规则 布尔型变量在和其他 7 种基本数据类型做运算时,无法转化为其他的数据类型,所以下面所说的运算都是除了布尔型的其他 7 种基本数据类型之间的转换. 1.自动类 ...

  10. Postman发送POST请求到Spring Boot的正确姿势

    最近用Spring Boot搭建了一些restful api,写起来真的很爽.但是当用Postman测试一些POST请求的接口的时候却遇到一些问题,上网冲浪查了一堆博客资料,发现都讲得不清不楚,于是记 ...