我有两种方法来国际化我的应用程序,我知道还有更多方法可以通过 Resource Bundle 来实现此目的(如果您认为有更好的方法,请解释一下),我对资源包有点陌生,这就是这个疑问合并的原因。
以下是我的做法:
第一个 - ListResourceBundle
import java.util.*;
//http://docs.oracle.com/javase/tutorial/i18n/resbundle/list.html
public class ListDemo {
static void displayValues(Locale currentLocale) {
ResourceBundle stats = ResourceBundle.getBundle("StatsBundle",currentLocale);
Integer gdp = (Integer)stats.getObject("GDP");
System.out.println("GDP = " + gdp.toString());
Integer pop = (Integer)stats.getObject("Population");
System.out.println("Population = " + pop.toString());
Double lit = (Double)stats.getObject("Literacy");
System.out.println("Literacy = " + lit.toString());
} // displayValues
static public void main(String[] args) {
Locale[] supportedLocales = {
//new Locale("en","CA"),
new Locale("ja","JP")
//new Locale("fr","FR")
};
for (int i = 0; i < supportedLocales.length; i ++) {
System.out.println("Locale = " + supportedLocales[i]);
displayValues(supportedLocales[i]);
System.out.println();
}
} // main
} // class
这是包含数据的类
import java.util.*;
public class StatsBundle_ja_JP extends ListResourceBundle {
public StatsBundle_ja_JP() {
this.contents = new Object[][]{{ "GDP", 21300}, { "Population", 125449703}, { "Literacy", 0.99}};
}
@Override
public Object[][] getContents() {
return contents;
}
private final Object[][] contents;
}
注意:这是oracle带来的例子。我想知道为什么当两个类都不在默认包中时它不起作用(我已经有了资源的完全限定名称)。
第二个 - PropertiesResourceBundle
import java.util.Locale;
import java.util.ResourceBundle;
public class InternationalizationDemo {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("oTHER/MessageBundle", Locale.US);
System.out.println("Message in " + Locale.US + ": " + bundle.getString("greeting"));
//changing the default locale to indonasian
Locale.setDefault(new Locale("in", "ID"));
bundle = ResourceBundle.getBundle("oTHER/MessageBundle");
System.out.println("Message in " + Locale.getDefault() + ": " + bundle.getString("greeting"));
}
}
以下是属性:
这两种方法都可以,但是哪一种更好、更快、重量最小?或者还有更好的方法吗?
还想知道何时使用它们中的每一种(PropertiesResourceBundle 和 ListResourceBundle)
最佳答案
它们都足够快。属性资源包稍微慢一点,因为必须解析文件,但这种开销可以忽略不计,并且结果会缓存在内存中。
选择不是基于性能。它基于特征。 ListResourceBundle 更加灵活。它允许使用 Java 执行任何您需要的操作来创建键和值。
属性资源包更易于阅读、编辑和维护。即使非程序员翻译人员也可以轻松编辑这样的文件。
关于您关于默认包的问题,当然可以将您的类和资源包放入您想要的任何包中。如果类 StatsBundle_ja_JP
位于包 com.foo.bar
中,那么您必须使用
ResourceBundle stats = ResourceBundle.getBundle("com.foo.bar.StatsBundle", japanLocale);
同样的规则适用于属性资源包:如果 MessageBundle 属性文件位于包 com.foo.bar
中,则应使用加载它
ResourceBundle stats = ResourceBundle.getBundle("com.foo.bar.MessageBundle", someLocale);
The documentation解释一下:
Note:The baseName argument should be a fully qualified class name. However, for compatibility with earlier versions, Sun's Java SE Runtime Environments do not verify this, and so it is possible to access PropertyResourceBundles by specifying a path name (using "/") instead of a fully qualified class name (using ".").
关于java - 国际化 Java 应用程序(资源包)的优点和缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25321065/