国际化 ,英文叫 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. Navicat12 for Mysql激活

    1      下载 注册机和Navicat网盘下载地址 链接:https://pan.baidu.com/s/1AFpQIlHCXVHc8OuBZ9PAlA  提取码:xvi2 2      安装 2 ...

  2. 03.AOF持久化机制配置与工作流程

    一.AOF持久化的配置 配置文件redis.conf,AOF持久化默认是关闭的,默认是打开RDB持久化 appendonly yes     二.工作流程: 打开AOF持久化机制之后,redis每次接 ...

  3. Git使用感悟

    前言 分支介绍 我们现在开发的分支一般是这样的(基于上面那张图片的): master:上线用的 dev:开发用的 featature_xxx:开发用的 test:测试用的 hotfix:修复bug的 ...

  4. Labview学习之路(五)按钮的机械动作

    布尔类型中有一个按钮是非常重要的控件,他不是只是表示一个确定,输出0或1,下边我们共同探讨一下他的机械动作 单击时转换 释放时转换 保持转换直到释放 单击时触发 释放时触发 保持触发直到释放 单击时转 ...

  5. 使用rabbitmq过程中遇到的问题及解决方法记录。

    OS: Linux ---Centos7RabbitMQ版本:RabbitMQ version: 3.8.1erlang版本:Erlang configuration: Erlang/OTP 22 [ ...

  6. Mac OSX上安装Nginx

    1. 通过brew instal nginx安装 ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.1.el_cap ...

  7. CSDN自定义栏目代码

    今天终于发现了csdn可以操作的地方,有个自定义栏目的地方可以贴HTML代码(只允许最多一个自定义栏目),不能用JS插件 有点难受,就贴了下自己的微信,并且可以直接点击图片发起会话 以下是我的代码,可 ...

  8. cdq分治——bzoj2683简单题

    https://www.lydsy.com/JudgeOnline/problem.php?id=2683 知识点:1.以操作的顺序进行分治  2.cdq分治维护矩阵 3.计算比mid小的给比mid大 ...

  9. basicInterpreter1.01 支持分支语句

    源码:https://files.cnblogs.com/files/heyang78/basicInterpreter-20200531-1.rar 输入: count= print(count) ...

  10. The relationship between Sonarcube coverage and code branch

    Once I was asked to enhance the sonarcube coverage of the class:‘jp.co.XXXXp.DltApiHttpRequestRetryH ...