;文件格式的列表(java.util.List<String>) FORMAT_PROPERTIES 包含“java.properties”的,仅允许properties 文件格式的列表(java.util.List<String>) FORMAT_DEFAULT 既包含“java.class”也包含“java.properties”的列表(java.util.List<String>)
选用常量 Control.FORMAT_PROPERTIES使得 Control 对象仅搜索 properties 文件,代码如下: Control propOnlyControl = Control.getControl(Control.FORMAT_PROPERTIES); ResourceBundle bundle = ResourceBundle.getBundle("com.sun.demo.intl.res.Warnings",propOnlyControl);
使用 propOnlyControl 变量(Control 的实例),getBundle 方法就忽略以 class 结尾的文件,而只搜索以 properties 结尾的文件。
Locales 是包名称的一部分 ----------------------- 基本名相同的各种本地化绑定,通常是以后缀名来区分。缺省的警告信息的"绑定"(Warnings bundle) 仅是简单地用 Warnings.properties 配置文件实现。然而,当你需要用法文来显示这些警告消息的时候,就要用 Warnings_fr_FR.properties 文件了。使用缺省的 Control ,这些本地化绑定名都是存在于同一个包里的,但是可以改变这些本地化绑定的命名。假设有这样的情形:你想要把同一个绑定的不同的本地化版本放置在各自对应的子目录或者是包里,那么就需要按照如下的样子创建这些 properties 并把它们放到各自对应的文件路径或者是包里:
com/sun/demo/intl/res/root/Warnings.properties com/sun/demo/intl/res/fr_FR/Warnings.properties com/sun/demo/intl/res/ja_JP/Warnings.properties
通过子类化 Control,并且在子类中覆盖以下方法来达成这样的目的。需要被覆盖的方法是:
* getFormats * toBundleName
覆盖 getFormats 方法是因为应用程序仅需要 properties 文件作资源绑定;覆盖 toBundleName方法是因为应用程序需要使用指定的 locale 作为新的“绑定”的包名称的一部分,而不是在“绑定”后面追加 locale 的名称。
[1] [2] [3] [4] 下一页
看看示例代码是怎样通过定制 Control 子类来指定 locale 包名
class SubdirControl extends Control {
//仅搜索 properties 格式的文件 public List<String> getFormats() { return Control.FORMAT_PROPERTIES; } public String toBundleName(String bundleName, Locale locale) { StringBuffer localizedBundle = new StringBuffer(); // Find the base bundle name. int nBaseName = bundleName.lastIndexOf('.'); String baseName = bundleName; // Create a new name starting with the package name. if (nBaseName >= 0) { localizedBundle.append(bundleName.substring(0, nBaseName)); baseName = bundleName.substring(nBaseName+1); } String strLocale = locale.toString(); // Now append the locale identification to the package name. if (strLocale.length() > 0 ) { localizedBundle.append("." + strLocale); } else { localizedBundl上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
|