国际化 ,英文叫 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. 前端Web APIs 二

    day04 - Web APIs 学习目标: 能够说出常用的3-5个键盘事件 能够知道如何获取当前键盘按下的是哪个键 能够知道浏览器的顶级对象window 能够使用window.onload事件 能够 ...

  2. 焦大:seo思维光年(下)seo操作如何度量化

    http://www.wocaoseo.com/thread-57-1-1.html 如果不能度量就无法进行改进,所以度量化或数据化是网站分析和网站研究必须进行的一个方面,seo也不能例外.我在上篇文 ...

  3. Selenium使用cookis登录,并临时将cookis存储在本地【shelve数据库】

    Python中自带了一个shelve库,可以帮助我们存储一些少量的数据. shelve数据库类似redis,是以[键值对]的方式进行数据的存储,有点像"字典"这种数据结构,存储在本 ...

  4. docker快速搭建php7.2-nginx开发环境

    1.输入命令: docker search -s 100 php 搜索出下面图中列表,选择webdevops/php-nginx. 2.通过docker拉取webdevops/php-nginx镜像, ...

  5. tars 问题汇总

    1. 节点 ip地址变了如何解决.后台不可以改. 2. [ok] 服务如何在本地运行../HelloGo --config.conf

  6. 游戏UI系统设计

    1.需要实现的功能 UI界面的管理(窗体加载.窗体显示.窗体隐藏.窗体销毁等) UI分层次(比如弹窗.广播信息需要在上层) UI界面的出场.入场动画 UI界面的显示效果(比如带透明背景.带高斯模糊背景 ...

  7. Codeforces1348 题解

    AC代码连接 A Phoenix and Balance 显而易见,将前\(\frac{n}{2}-1\)个和最后1个分为1组,剩下的1组即为最优方案. B Phoenix and Science 这 ...

  8. Google Kick Start 2020 Round B T4 Wandering Robot

    题意 一个\(n \times m\)的矩形空间,起点是\((1,1)\),终点是\((n,m)\). 假设当前位于\((x,y)\): 如果当前位于最后一行,那么下一步只能走向\((x,y+1)\) ...

  9. Charles安装教程

    一, 简介及安装 —,charles的使用 1.1   charles的说明 Charles其实是一款代理服务器,通过过将自己设置成系统(电脑或者浏览器)的网络访问代理服务器,然后截取请求和请求结果达 ...

  10. 15_Python的模块module

    1.模块的概述 1.模块是Python程序架构的一个核心概念,每一个以.py结尾的Python源代码文件都是一个模块 2.模块名和标识符的命名规则一样,由数字字母下划线组成且不能以数字开头,也不要和系 ...