0

Can someone please explain what is the relation between Locale and ResourceBundle classes provided by the JDK ? Is it the same as the difference between i18n vs l10n as given in this answer ?

4

1 回答 1

1

Locale 和 ResourceBundle 的关系是,Locale 用于读取正确的属性文件。基本上,ResourceBundle.Control 类接受一个 Locale 对象,并给定属性文件的基本名称,它会尝试找到最匹配的对象。例如:

ResourceBundle rb = ResourceBundle.getbundle("messages", Locale.forLanguageTag("zh-TW");

如果你这样做,ResourceBundle.Control 将查找messages_zh_TW.properties、messages_zh-Hant-TW.properties、messages_zh-Hant.properties、messages_zh.properties,如果找不到任何特定的,最后回退到messages.properties资源文件。

编辑

只是添加最后一件事。如果您省略 Locale 参数,ResourceBundle.getBundle(String)则将根据您的默认 Locale 返回属性文件 - 您可以像这样获得该文件:

Locale default = Locale.getDefault(Locale.Category.DISPLAY);

尽管过多的输入似乎没有必要(在桌面应用程序的上下文中),但始终传递 Locale 是一个好习惯,因为它使代码更容易理解。

于 2012-10-06T09:59:41.740 回答