Java国际化,使用ResourceBundle的方式读取配置文件
来源:互联网 发布:cf手游刷等级软件 编辑:程序博客网 时间:2024/06/09 23:11
如Struts,spring等框架以及Tomcat容器,都是通过ResourceBundle的方式从资源文件(如messages.properties)中自动读取并进行资源绑定的。java.util.ResourceBundle类非常地灵活,可以通过设置的Locale(语言环境)来选择读取的文件,进行国际化。
最近在看Tomcat的源码,看到了Tomcat国际化的实现方式,下面写一个例子介绍如何使用:
资源配置文件:
LocalStrings_fr.java:
package terry.codex;import java.util.Collections;import java.util.Enumeration;import java.util.ResourceBundle;/** * @编写人: yh.zeng * @编写时间:2017-12-3 下午4:58:26 * @文件描述: LocalStrings法语配置文件 */public class LocalStrings_fr extends ResourceBundle{@Overrideprotected Object handleGetObject(String key) {if(key.equals("tmpdir")){return "{0} itinéraires désignés avec moins";}return null;}@Overridepublic Enumeration<String> getKeys() { return Collections.enumeration(keySet());}}LocalStrings_en.properties:
tmpdir=Cannot find specified temporary folder at {0}LocalStrings_zh.properties:
tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939LocalStrings.properties:
tmpdir=\u5728{0}\u8def\u5f84\u4e0b\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939
测试例子demo
ResourceBundleTest:
package terry.codex;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;import java.text.MessageFormat;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;/** * @编写人: yh.zeng * @编写时间:2017-12-3 上午12:35:42 * @文件描述: 通过ResourceBundle的方式从资源文件(如messages.properties)中自动读取并进行资源绑定的demo * 可以用于实现国际化 */public class ResourceBundleTest {public static void main(String args[]){ String bundleName = "terry.codex.LocalStrings"; /** * 1、在当前类加载器下,找Java 虚拟机实例的当前默认语言环境值对应的配置文件,会找以下文件内容 * terry/codex/LocalStrings_zh.class * terry/codex/LocalStrings_zh.properties * terry/codex/LocalStrings.class * terry/codex/LocalStrings.properties * 文件内容自上而下覆盖 * LocalStrings_zh.class文件会覆盖掉LocalStrings_zh.properties文件的内容 * LocalStrings_zh.properties文件会覆盖掉LocalStrings.class文件的内容 * LocalStrings.class文件会覆盖掉LocalStrings.properties文件的内容 * 注意:LocalStrings_zh.class文件必须是ResourceBundle的子类,实现handleGetObject和getKeys方法! */ try { System.out.println("Example1:"); ResourceBundle bundle = ResourceBundle.getBundle(bundleName); if(bundle != null){ String value = bundle.getString("tmpdir"); String params[] = new String[]{"D:\\TMP"}; MessageFormat mf = new MessageFormat(value); System.out.println(mf.format(params, new StringBuffer(), null).toString()); }} catch (MissingResourceException ex) {ex.printStackTrace();} /** * 2、Locale.ENGLISH在当前类加载器下找以下文件的内容: * terry/codex/LocalStrings_en.class * terry/codex/LocalStrings_en.properties * terry/codex/LocalStrings.class * terry/codex/LocalStrings.properties * 文件内容自上而下覆盖 * LocalStrings_en.class文件会覆盖掉LocalStrings_en.properties文件的内容 * LocalStrings_en.properties文件会覆盖掉LocalStrings.class文件的内容 * LocalStrings.class文件会覆盖掉LocalStrings.properties文件的内容 * 注意:LocalStrings_en.class文件必须是ResourceBundle的子类,实现handleGetObject和getKeys方法! */ try { System.out.println("Example2:"); ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.ENGLISH); if(bundle != null){ String value = bundle.getString("tmpdir"); String params[] = new String[]{"D:\\TMP"}; MessageFormat mf = new MessageFormat(value); mf.setLocale(Locale.ENGLISH); System.out.println(mf.format(params, new StringBuffer(), null).toString()); }} catch (MissingResourceException ex) {ex.printStackTrace();} /** * 3、在当前类加载器下找以下文件: * terry/codex/LocalStrings_zh.class * terry/codex/LocalStrings_zh.properties * terry/codex/LocalStrings.class * terry/codex/LocalStrings.properties * 文件内容自上而下覆盖 * LocalStrings_zh.class文件会覆盖掉LocalStrings_zh.properties文件的内容 * LocalStrings_zh.properties文件会覆盖掉LocalStrings.class文件的内容 * LocalStrings.class文件会覆盖掉LocalStrings.properties文件的内容 * 注意:LocalStrings_zh.class文件必须是ResourceBundle的子类,实现handleGetObject和getKeys方法! */ try { System.out.println("Example3:"); ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.CHINA); if(bundle != null){ String value = bundle.getString("tmpdir"); String params[] = new String[]{"D:\\TMP"}; MessageFormat mf = new MessageFormat(value); mf.setLocale(Locale.CHINA); System.out.println(mf.format(params, new StringBuffer(), null).toString()); }} catch (MissingResourceException ex) {ex.printStackTrace();} /** * 4、在当前类加载器下找以下文件: * terry/codex/LocalStrings_fr.class * terry/codex/LocalStrings_fr.properties * terry/codex/LocalStrings.class * terry/codex/LocalStrings.properties * 文件内容自上而下覆盖 * LocalStrings_fr.class文件会覆盖掉LocalStrings_fr.properties文件的内容 * LocalStrings_fr.properties文件会覆盖掉LocalStrings.class文件的内容 * LocalStrings.class文件会覆盖掉LocalStrings.properties文件的内容 * 注意:LocalStrings_fr.class文件必须是ResourceBundle的子类,实现handleGetObject和getKeys方法! */ try { System.out.println("Example4:"); ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.FRANCE); if(bundle != null){ String value = bundle.getString("tmpdir"); String params[] = new String[]{"D:\\TMP"}; MessageFormat mf = new MessageFormat(value); mf.setLocale(Locale.FRANCE); System.out.println(mf.format(params, new StringBuffer(), null).toString()); }} catch (MissingResourceException ex) {ex.printStackTrace();} /** * 5、在D:\\loader这个类加载路径下找以下文件: * terry/codex/LocalStrings_zh.class * terry/codex/LocalStrings_zh.properties * terry/codex/LocalStrings.class * terry/codex/LocalStrings.properties * 文件内容自上而下覆盖 * LocalStrings_zh.class文件会覆盖掉LocalStrings_zh.properties文件的内容 * LocalStrings_zh.properties文件会覆盖掉LocalStrings.class文件的内容 * LocalStrings.class文件会覆盖掉LocalStrings.properties文件的内容 * 注意:LocalStrings_zh.class文件必须是ResourceBundle的子类,实现handleGetObject和getKeys方法! */try {bundleName = "terry.codex2.LocalStrings";System.out.println("Example5:"); String fileUrlString = new File("D:\\loader").toURI().toString(); fileUrlString = fileUrlString.replaceAll("!/", "%21/");ClassLoader classLoader = new URLClassLoader(new URL[]{new URL(fileUrlString)}); if (classLoader != null) { try { ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.CHINA, classLoader); if(bundle != null){ String value = bundle.getString("tmpdir"); String params[] = new String[]{"D:\\TMP"}; MessageFormat mf = new MessageFormat(value); mf.setLocale(Locale.CHINA); System.out.println(mf.format(params, new StringBuffer(), null).toString()); } } catch (MissingResourceException ex2) { ex2.printStackTrace(); } }} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
程序运行结果:
Example1:在D:\TMP路径下找不到指定的文件夹Example2:Cannot find specified temporary folder at D:\TMPExample3:在D:\TMP路径下找不到指定的文件夹Example4:D:\TMP itinéraires désignés avec moinsExample5:在D:\TMP路径下找不到指定的文件夹
demo:见https://github.com/zengyh/CodeLibary/tree/master/src/resource/bundle/
阅读全文
0 0
- Java国际化,使用ResourceBundle的方式读取配置文件
- 使用ResourceBundle读取配置文件
- 使用ResourceBundle读取配置文件
- java.util.ResourceBundle读取配置文件
- Java ResourceBundle读取properies配置文件
- 读取配置文件的两种方式ResourceBundle和Properties
- [Java]ResourceBundle和Properties配置文件实现国际化
- java.util.ResourceBundle使用详解 (国际化的原理)
- java 读取properties 的不同方式,ResourceBundle和Porperties
- java.util.ResourceBundle使用详解(国际化)
- java国际化与ResourceBundle使用详解
- Spring调用JDK的ResourceBundle读取配置文件
- ResourceBundle读取配置文件
- 用ResourceBundle 读取配置文件
- ResourceBundle读取配置文件
- ResourceBundle读取配置文件
- 利用ResourceBundle读取配置文件
- ResourceBundle,Properties读取配置文件
- 要拥有自己的核心竞争力“看家本领”
- C语言assert关键字
- 数据库—基本概念
- 如何实现copy一个视频并且改变md5
- Queue
- Java国际化,使用ResourceBundle的方式读取配置文件
- UltraEdit 22、23版本系列32bit、64bit破解方法
- TensorFlow视频教程
- 优秀程序员应具备的15个特性
- logging
- haproxy
- svn版本控制工具
- [BZOJ3110][ZJOI2013]K大数查询 树套树/CDQ分治
- C语言实现的五子棋