DecimalFormatSymbols 具有非正统区域设置的意外区域特定值

DecimalFormatSymbols has unexpected region specific values for unorthodox locales

DecimalFormatSymbols 的帮助下,您可以请求基于区域设置的特征,例如小数分隔符或千位分隔符。

只要您为常用的语言标签(例如 de-ATen-US)请求它,它就会按预期工作。但是,如果您混合使用语言-国家/地区组合,它就会表现得很奇怪。特别是,让我们看一下千位分隔符。 (英语为 ,,德语为 .

    System.out.println("en-US: " + DecimalFormatSymbols.getInstance(Locale.US).getGroupingSeparator());
    System.out.println("de-DE: " + DecimalFormatSymbols.getInstance(Locale.GERMANY).getGroupingSeparator());
    System.out.println("de-US: " + DecimalFormatSymbols.getInstance(new Locale.Builder().setLanguage("de").setRegion("US").build()).getGroupingSeparator());
    System.out.println("de: "+DecimalFormatSymbols.getInstance(new Locale.Builder().setLanguage("de").build()).getGroupingSeparator());
    System.out.println("DE: " + DecimalFormatSymbols.getInstance(new Locale.Builder().setRegion("DE").build()).getGroupingSeparator());
    System.out.println("ru-RU: " + DecimalFormatSymbols.getInstance(new Locale.Builder().setLanguage("ru").setRegion("RU").build()).getGroupingSeparator());
    System.out.println("RU: " + DecimalFormatSymbols.getInstance(new Locale.Builder().setRegion("RU").build()).getGroupingSeparator());

结果是:

en-US: ,
de-DE: .
de-US: .
de: .
DE: ,
ru-RU: 0x160
RU: ,

对于de-US,它表示一个点作为分隔符,代表德语中的分隔符,但不代表美国。好像它只考虑了语言标签。

如果我创建的语言环境似乎只有国家/地区信息(缺少语言),那么返回的总是英文分隔符格式。

我该如何正确解决这个问题?我想要语言环境中最具体信息的格式。对于de,我想要德文的。对于 de-US 我想要英文格式。

与区域设置相关的信息,例如 DecimalFormatSymbols, are generally stored in the Java Runtime Library in ResourceBundle 个文件。

阅读 javadoc 以获得完整的详细信息,但相关部分是:

Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be "MyResources". The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported. The family can then provide as many locale-specific members as needed, for example a German one named "MyResources_de".

If there are different resources for different countries, you can make specializations: for example, "MyResources_de_CH" contains objects for the German language (de) in Switzerland (CH). If you want to only modify some of the resources in the specialization, you can do so.

因此,符号查找将使用 language-country 组合。如果未找到,它将尝试仅使用 language。否则它将使用具有默认值的基本文件。

getGroupingSeparator 的默认值为 ,,因此这是您为 DERU 等不受支持的区域设置获得的值。