ResourceBundle 用于加载properties文件,并通过key获取文件中的值。
通过静态方法实例化ResourceBundle:
static ResourceBundle |
getBundle(String baseName, Locale locale) |
baseName 由两部分组成:类路径+properties文件的基本名称
匹配的properties文件名依次为:
properties文件的基本名称+"_"+locale.getLanguage()+"_"+locale.getCountry+".properties"
properties文件的基本名称+"_"+locale.getLanguage()+".properties"
properties文件的基本名称+".properties"
ResourceBundle 在baseName指定的类路径下,按上面顺序依次查找并加载properites文件,直到成功加载某个properties文件或抛出未找到异常(MissingResourceException)
举例:
baseName= com.app.message Locale = Local.CHINA
则加载的properties文件为: 类路径com/app/下
message_zh_CN.properties
message_zh.properties
message.properties
加载成功后就可以获取properties文件中的信息:
boolean |
containsKey(String key) 确定给定 key 是否包含在此 ResourceBundle 及其父包中。 |
abstract Enumeration<String> |
getKeys() 返回键的枚举。 |
Locale |
getLocale() 返回此资源包的语言环境。 |
Object |
getObject(String key) 从此资源包或它的某个父包中获取给定键的对象。 |
String |
getString(String key) 从此资源包或它的某个父包中获取给定键的字符串。 |
String[] |
getStringArray(String key) 从此资源包或它的某个父包中获取给定键的字符串数组。 |
protected abstract Object |
handleGetObject(String key) 从此资源包中获取给定键的对象。 |
protected Set<String> |
handleKeySet() 返回只 包含在此 ResourceBundle 中的键的 Set 。 |
Set<String> |
keySet() 返回此 ResourceBundle 及其父包中包含的所有键的 Set 。 |
protected void |
setParent(ResourceBundle parent) 设置此包的父包。 |
ResourceBundle 一般用于提取国际化消息,根据Locale 的不同,而定位到不同的properties文件,这些properties文件的key都相同,值是用不同语言描述,因此可以获得不同的语言消息。
为了支持不同国家的语言,properties文件的内容需要转换为unicode编码(\uxxxx)。
eclipse 编辑properties 时,录入的中文会自动转换为unicode编码。
也可以使用命令:native2ascii
native2ascii -encoding 源编码格式 源文件 目标文件
native2ascii -encoding UTF-8 message_zh_CN_src.properties message_zh_CN.properties
也可以使用 ResourceBundle 来切换配置信息,
举例:new Local("jdbc","db1") 或new Local("jdbc","db2")
用来切换jdbc连接
public void testResourceBundler(){ //类路径 test下message.properties中配置: // city = \u4E2D\u56FD\u6C88\u9633 ResourceBundle resultBundle = ResourceBundle.getBundle("test.message",Locale.CHINA); String city = resultBundle.getString("city"); System.out.println(city);//中国沈阳 //类路径 test下config_jdbc_db1.properties中配置: // jdbc.url=jdbc:oracle:thin:@192.168.14.14:1521:db01 resultBundle = ResourceBundle.getBundle("test.config",new Locale("jdbc","db1")); String jdbcUrl = resultBundle.getString("jdbc.url"); System.out.println(jdbcUrl);//jdbc:oracle:thin:@192.168.14.14:1521:db01 //类路径 test下config_jdbc_db2.properties中配置: // jdbc.url=jdbc:oracle:thin:@192.168.14.15:1521:db02 resultBundle = ResourceBundle.getBundle("test.config",new Locale("jdbc","db2")); jdbcUrl = resultBundle.getString("jdbc.url"); System.out.println(jdbcUrl);//jdbc:oracle:thin:@192.168.14.15:1521:db02 }
MessageFormat 用于格式化字符串,如字符串中有{0},{1},等,使用参数代替。
public void testMessageFormat(){ Object params = new Object []{"china"}; MessageFormat messageFormat = new MessageFormat(" your city is {0}"); System.out.println(messageFormat.format(params));//your city is china double num = 12345.123; params = new Object []{num}; messageFormat = new MessageFormat("the number is {0,number}"); System.out.println(messageFormat.format(params));//the number is 12,345.123 messageFormat = new MessageFormat("the number is {0,number,integer}"); System.out.println(messageFormat.format(params));//the number is 12,345 messageFormat = new MessageFormat("the number is {0,number,currency}"); System.out.println(messageFormat.format(params));//the number is ¥12,345.12 messageFormat = new MessageFormat("the number is {0,number,percent}"); System.out.println(messageFormat.format(params));//the number is 1,234,512% messageFormat = new MessageFormat("the number is {0,number,#.#}"); System.out.println(messageFormat.format(params));//the number is 12345.1 messageFormat = new MessageFormat("the number is {0,number,#.##%}"); System.out.println(messageFormat.format(params));//the number is 1234512.3% messageFormat = new MessageFormat("the number is {0,number,#,###.##}"); System.out.println(messageFormat.format(params));//the number is 12,345.12 params = new Object []{new Date()}; messageFormat = new MessageFormat("current time is {0,date}"); System.out.println(messageFormat.format(params));//current time is 2014-4-3 messageFormat = new MessageFormat("current time is {0,date,short}"); System.out.println(messageFormat.format(params));//current time is 14-4-3 messageFormat = new MessageFormat("current time is {0,date,medium}"); System.out.println(messageFormat.format(params));//current time is 2014-4-3 messageFormat = new MessageFormat("current time is {0,date,long}"); System.out.println(messageFormat.format(params));//current time is 2014年4月3日 messageFormat = new MessageFormat("current time is {0,date,full}"); System.out.println(messageFormat.format(params));//current time is 2014年4月3日 星期四 messageFormat = new MessageFormat("current time is {0,date,yyyy-MM-dd hh:mm:ss}"); System.out.println(messageFormat.format(params));//current time is 2014-04-03 11:41:24 }
相关推荐
NULL 博文链接:https://singleant.iteye.com/blog/1485781
国际化插件-ResourceBundle Editor
ResourceBundle Editor (eclipse-rbe)是一个用于编辑本地化的属性文件(localized properties files)。它让你通一个屏幕就能够同时管理所有相关联属性文件中的key/value信息。它所具有的功能包括:自动为Keys分类,...
这里面有详细的处理maven项目中resources目录下的资源文件,方便快捷的得到或设置资源文件中的内容,本人亲测,里面还有示例
i18n Locale ResourceBundle MessageFormat 使用例子 在struts2学习开始前需要普及的知识点
java.util.ResourceBundle使用详解
ResourceBundle:资源包,包括图片源代码
本文主要介绍javaWEB国际化的知识,这里整理了详细的资料及实现代码,有兴趣的小伙伴可以参考下
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 3。使用java.util.PropertyResourceBundle类的构造函数 示例: InputStream in = new BufferedInputStream(new FileInputStream...
j18n替代了java.util.ResourceBundle以支持Unicode属性文件。 您可以将UTF8字符放入* .properties中,唯一的代码更改是将“ ResourceBundle.getBundle(..)”替换为“ J18n.getBundle(..)”。
下面小编就为大家带来一篇ResourceBundle类在jsp中的国际化实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ResourceBundle 是一个固执的 Symfony 包,用于帮助开发 REST API。 它为您做出一些架构决策,使您能够更多地关注应用程序的领域。 它使用尽可能少的魔法,使其更易于理解、调试和扩展。 ##Prerequisites ...
开发它是为了简化创建 ResourceBundle 源的过程,而不必每次使用 genrb 重新编译源文件。 这个项目既不是真正的一部分,也不是。 用法 : 测试文件 <?php use Adoy \ ICU \ ResourceBundle \ ResourceBundle ;...
NULL 博文链接:https://leiwuluan.iteye.com/blog/369909
private ResourceBundle resourceBundle; public CommonParam() { propertyFileName = "com/test/dao/SysConfig"; resourceBundle = ResourceBundle.getBundle(propertyFileName); } public String ...
可视化的插件编写,也可以转化国际化语言,挺不错的小工具
ResourceBundleEditor 资源文件编辑器 eclipse的资源文件编辑插件 经常在做国际化的时候编辑properties文件。转换文件字符总是让人比较繁琐。有了这个插件所见即所得。我们输入中文的时候自动就转换完毕了。...
ResourceBundle Locale 3、日期时间格式化:类型转换 String--------->java.util.Date java.util.Date--------->String DateFormat 4、数字的格式化:货币 5、批量国际化MessageFormat 占位符:aaaa{索引...