语言环境序列

Sequence of Locales

使用语言环境的顺序究竟是什么? 我有 3 个属性文件:

Dolphins.properties

name=The Dolphin
age=0

Dolphins_en.properties

name=Dolly
age=4

Dolphins_fr.properties

name=Dolly

我的代码是:

5: Locale fr = new Locale("fr");

6: Locale.setDefault(new Locale("en", "US"));

7: Resource Bundle b = ResourceBundle.getBundle("Dolphins", fr);

8: b.getString("name");

9: b.getString("age");

代码将默认的Locale设置为Dolphins_en那么为什么要使用Dolphins.properties呢?我错过了什么或误解了什么?

提前致谢。

选择捆绑包链时使用默认语言环境。这意味着,当您未指定它时将使用它:ResourceBundle.getBundle("Dolphins") 或当您指定包不存在的区域设置时:ResourceBundle.getBundle("Dolphins", new Locale("cs")).

But when the bundle is selected then the default locale is not used any more for value selection.这意味着当 Dolphins_fr.properties 中不存在密钥 age 时,它将使用默认包中的值 Dolphins.properties

注意: 如果键不在 Dolphins.properties 中,它会抛出 MissingResourceException.

更新: 感觉以前看到一样的代码,终于搞定了。您可以查看 Jeanne Boyarsky - OCP 学习指南 或此处 https://coderanch.com/t/685833/certification/OCP-Chapter-Jeanne-Boyarsky,其中完全相同的代码和原因以更好的英语解释。