一、前端
经过util包中的ResourceBundle加载:java
首先国际化资源文件放在了classpath下的i18n目录下:python
mymessage_en_US.properties:web
com.website.operation=\u67e5\u8be2\u64cd\u4f5c\u65e5\u5fd7 com.website.write=\u5199\u65e5\u5fd7 com.website.writeLog=\u5199 {0} \u65e5\u5fd7
mymessage_en_US.properties:面试
com.website.operation=queryOperationLog com.website.write=recordLog com.website.writeLog=record {0} Log
利用ResourceBundle加载国际化文件,这里列出四个方法,分别是利用默认Locale、zh_CN、en_US以及带占位符的处理方式。这里须要注意的是BaseName为classpath下的目录+/+国际化文件名前缀,即i18n/mymessagespring
package com.website.controller.utils; import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; /** * @program: website * @description: 获取国际化配置文件 * @author: smallsoup * @create: 2018-07-27 22:32 **/ public class ResourceUtils { public static String getEnglishValueByKey(String key){ Locale locale = new Locale("en", "US"); //使用指定的英文Locale ResourceBundle mySource = ResourceBundle.getBundle("i18n/mymessage", locale); return mySource.getString(key); } public static String getChineseValueByKey(String key){ Locale locale = new Locale("zh", "CN"); //使用指定的中文Locale ResourceBundle mySource = ResourceBundle.getBundle("i18n/mymessage", locale); return mySource.getString(key); } public static String getDeafultValueByKey(String key){ //使用默认的Locale ResourceBundle mySource = ResourceBundle.getBundle("i18n/mymessage"); return mySource.getString(key); } public static String getValueAndPlaceholder(String key){ //使用默认的Locale ResourceBundle mySource = ResourceBundle.getBundle("i18n/mymessage"); String beforeValue = mySource.getString(key); //填充国家化文件中的占位符 String afterValue = MessageFormat.format(beforeValue, "安全"); return afterValue; } }
在controller里面调用ResourceUtils里的方法:编程
@RequestMapping(value = "/projectadd") public String projectAdd(){ LOGGER.warn("projectAdd getChineseValueByKey is {}", ResourceUtils.getChineseValueByKey("com.website.operation")); LOGGER.warn("projectAdd getDeafultValueByKey is {}", ResourceUtils.getDeafultValueByKey("com.website.operation")); LOGGER.warn("projectAdd getEnglishValueByKey is {}", ResourceUtils.getEnglishValueByKey("com.website.operation")); LOGGER.warn("projectAdd getValueAndPlaceholder is {}", ResourceUtils.getValueAndPlaceholder("com.website.writeLog")); return "project/projectadd"; }
启动tomcat打印日志:segmentfault
二、缓存
利用spring的ResourceBundleMessageSourcetomcat
ResourceBundleMessageSource是基于JDK ResourceBundle的MessageSource接口实现类。它会将访问过的ResourceBundle缓存起来,以便于下次直接从缓存中获取进行使用。
和上面不一样的是ResourceUtils的实现,实现以下:
package com.website.controller.utils; import org.springframework.context.support.ResourceBundleMessageSource; import java.util.Locale; /** * @program: website * @description: 获取国际化配置文件 * @author: smallsoup * @create: 2018-07-27 22:32 **/ public class ResourceUtils { private static ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); static { //指定国家化资源文件路径 messageSource.setBasename("i18n/mymessage"); //指定将用来加载对应资源文件时使用的编码,默认为空,表示将使用默认的编码进行获取。 messageSource.setDefaultEncoding("UTF-8"); } public static String getChineseValueByKey(String key){ return messageSource.getMessage(key, null, Locale.CHINA); } public static String getDeafultValueByKey(String key){ return messageSource.getMessage(key, null, null); } public static String getEnglishValueByKey(String key){ return messageSource.getMessage(key, null, Locale.US); } public static String getValueAndPlaceholder(String key){ return messageSource.getMessage(key, new Object[]{"安全"}, null); } }
三、
利用spring的ReloadableResourceBundleMessageSource
ReloadableResourceBundleMessageSource也是MessageSource的一种实现,其用法配置等和ResourceBundleMessageSource基本一致。所不一样的是ReloadableResourceBundleMessageSource内部是使用PropertiesPersister来加载对应的文件,这包括properties文件和xml文件,而后使用java.util.Properties来保存对应的数据。
package com.website.controller.utils; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import java.util.Locale; /** * @program: website * @description: 获取国际化配置文件 * @author: smallsoup * @create: 2018-07-27 22:32 **/ public class ResourceUtils { private static ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); static { //指定国家化资源文件路径 messageSource.setBasename("i18n/mymessage"); //指定将用来加载对应资源文件时使用的编码,默认为空,表示将使用默认的编码进行获取。 messageSource.setDefaultEncoding("UTF-8"); //是否容许并发刷新 messageSource.setConcurrentRefresh(true); //ReloadableResourceBundleMessageSource也是支持缓存对应的资源文件的,默认的缓存时间为永久,即获取了一次资源文件后就将其缓存起来,之后不再从新去获取该文件。这个能够经过setCacheSeconds()方法来指定对应的缓存时间,单位为秒 messageSource.setCacheSeconds(1200); } public static String getChineseValueByKey(String key){ return messageSource.getMessage(key, null, Locale.CHINA); } public static String getDeafultValueByKey(String key){ return messageSource.getMessage(key, null, null); } public static String getEnglishValueByKey(String key){ return messageSource.getMessage(key, null, Locale.US); } public static String getValueAndPlaceholder(String key){ return messageSource.getMessage(key, new Object[]{"安全"}, null); } }
这三种方式最后结果是同样的。
参考:
国际化MessageSource
http://elim.iteye.com/blog/23...
本公众号免费提供csdn下载服务,海量IT学习资源,若是你准备入IT坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java、go、python、springcloud、elk、嵌入式 、大数据、面试资料、前端 等资源。同时咱们组建了一个技术交流群,里面有不少大佬,会不定时分享技术文章,若是你想来一块儿学习提升,能够公众号后台回复【2】,免费邀请加技术交流群互相学习提升,会不按期分享编程IT相关资源。
扫码关注,精彩内容第一时间推给你