国际化的实现i18n--错误码国际化以及在springboot项⽬中使⽤
国际化,英⽂叫 internationalization 单词太长,⼜被简称为 i18n(取头取尾中间有18个字母);
主要涉及3个类: Locale⽤来设置定制的语⾔和国家代码
       ResourceBundle ⽤来加载国际化资源⽂件
       MessageFormat ⽤来格式化占位符
//先看结构:
在创建国际化资源⽂件时,如我在resources⽂件下建了⼀个i18n⽂件夹,下放了3个资源⽂件,idea⾃动会⽣成⼀个Resource Bundle 'messages'⽂件夹,这个⽂件夹不是真实存在的,打开项⽬所在⽂件夹后,不到它的;如图:
资源⽂件的命名格式:前缀+"_"+"语⾔代码"+地区代码+".properites";(
Locale[] locales = AvailableLocales();//获取世界可⽤的语⾔地区表⽰
)
如上图所⽰,我定义了3个资源⽂件夹,其中⼀个没有语⾔代码和地区代码的那个是默认资源,也就是要的资源⽂件都不存在时,就会去那⾥;
查顺序,如果你指定Locale locale=new Locale("en_US"),那它就会到messages_en_US.properties,如果你写错了⼀个单词Locale locale=new Locale("en_USAA");不到的话,它就会本地的语⾔(
Locale locale2 = Default(); //获取默认语⾔地区
),如我们在中国就会本地的中国语⾔包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 = Default(); //获取默认语⾔地区
System.out.Country()); //CN
System.out.DisplayCountry()); //中国
System.out.DisplayLanguage()); //中⽂
System.out.DisplayName()); //中⽂(中国)
System.out.DisplayLanguage(locale1)); //以某种语⾔显⽰语⾔,这⾥是Chinese
System.out.Language()); //zh 语⾔代表符
springboot结构System.out.LanguageTag()); //语⾔标签格式语⾔-国家这⾥是zh-CN
//Locale⾃定义了很多语⾔跟国家常量如中国和中⽂,德国和德⽂
Locale china = Locale.CHINA; // zh-Cn
Locale chinese = Locale.CHINESE; //ZH
System.out.LanguageTag()); //ZH-CN
System.out.LanguageTag()); //ZH
Locale german = Locale.GERMAN; //de
Locale germany = Locale.GERMANY; //de-DE
System.out.LanguageTag());//de
System.out.LanguageTag());//de-DE
Locale[] locales = AvailableLocales();//获取世界可⽤的地区
for (Locale locale : locales) {
System.out.LanguageTag());
}
ResourceBundle resourceBundle = Bundle("i18n/messages",locale2); //这⾥的baseName也可以表⽰成ssages形式,messages是虚拟的包名
String s = String("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 = Bundle("i18n/messages",locale);
String msg = String(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.Message("103032", arr, locale2));
//输出结果:Order No.dddd and No.vvvvvv has not been linked
}
}
需要在l中配置:
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 ba
sed 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.
如果报这个t.NoSuchMessageException: No message found under code '103032' for locale 'en_us'
⾮常有可能是上⾯的messages在l的位置没配置对,messages要配置在spring的下⼀级,如图所⽰:
错误配置如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。