简体   繁体   English

使用类而不是属性文件或 ListResourceBundles 时 ResourceBundle.getBundle() 的强制语言

[英]Mandatory language for ResourceBundle.getBundle() when using classes instead of properties files or ListResourceBundles

If I have properties like this:如果我有这样的属性:

  • I18N.properties I18N.properties
  • I18N_en.properties I18N_en.properties
  • I18N_en_US.properties I18N_en_US.properties

I can use ResourceBundle.getBundle(Locale.FRANCE) and it will load the default properties (I18N.properties).我可以使用ResourceBundle.getBundle(Locale.FRANCE) ,它会加载默认属性(I18N.properties)。 This also works if I use ListResourceBundle classes instead of properties.如果我使用 ListResourceBundle 类而不是属性,这也有效。

However if I try to use ResourceBundle classes, the rule seems different, I need to have at least a I18N_xx.class , it will not use the default I18N.class : for instance locale en_CA will load I18N_en.class but locale fr_FR (or Locale.FRANCE ) won't load I18N.class , instead I get: However if I try to use ResourceBundle classes, the rule seems different, I need to have at least a I18N_xx.class , it will not use the default I18N.class : for instance locale en_CA will load I18N_en.class but locale fr_FR (or Locale.FRANCE )不会加载I18N.class ,而是我得到:

Can't find bundle for base name I18N, locale fr_FR找不到基本名称 I18N 的捆绑包,语言环境 fr_FR

Furthermore if I add an I18N.properties file, then the getBundle() will find it...此外,如果我添加一个 I18N.properties 文件,那么 getBundle() 会找到它...

Is is normal behavior?是正常行为吗? Am I missing something?我错过了什么吗? The doc seems to hint that this is normal (note that the candidates lists in the examples end on {base name}_{lang} ), but if this is so I don't see the rationale? 该文档似乎暗示这是正常的(请注意,示例中的候选人列表以{base name}_{lang}结尾),但如果是这样,我看不出理由吗? Why should the base default be a properties file?为什么基本默认值应该是属性文件?

OK, so the real answer is, the constructor of my base ResourceBundle class was not public...好的,所以真正的答案是,我的基础ResourceBundle class 的构造函数不是公开的......

However, while investigating this I found another weirdness:然而,在调查这个问题时,我发现了另一个奇怪的地方:

It appears that if it cannot find a resource for the language, instead of going to the base file it tries with the default locale: if my default locale is an English-based locale, say, en_IE then asking for an fr_FR resource yields the en one, and if the default is en_US , fr_FR resources come from the en_US bundle.看来,如果它找不到该语言的资源,而不是转到基本文件,它会尝试使用默认语言环境:如果我的默认语言环境是基于英语的语言环境,例如en_IE ,那么请求fr_FR资源会产生en一,如果默认是en_USfr_FR资源来自en_US包。

In a server, that would be a pain because a server-wide default language wouldn't yield an adequate behavior for all users, but it appears that Java defines a language/country neutral Locale.ROOT static for this purpose.在服务器中,这会很痛苦,因为服务器范围的默认语言不会为所有用户产生适当的行为,但似乎 Java 为此定义了语言/国家中立Locale.ROOT static 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ResourceBundle.getBundle和Websphere - ResourceBundle.getBundle and Websphere ResourceBundle.getBundle() 访问属性文件的默认路径是什么 - What is the default path of ResourceBundle.getBundle() to access the properties file Java ResourceBundle.getBundle如何加载属性文件? - How does Java ResourceBundle.getBundle load properties file? java ResourceBundle.getBundle()不明确 - java ResourceBundle.getBundle() undeterministic 如何忽略ResourceBundle.getBundle中的defaultLocale? - How to ignore defaultLocale in ResourceBundle.getBundle? Java ResourceBundle.getBundle缺少资源异常 - Java ResourceBundle.getBundle missing resource exception 为什么ResourceBundle.getBundle(String str)无法从Websphere中的默认属性文件中找到值 - Why ResourceBundle.getBundle(String str) cannot find values from default properties file in Websphere ResourceBundle.getBundle(class,locale)没有获得默认捆绑 - ResourceBundle.getBundle(class,locale) don't get default Bundle 为什么ResourceBundle.getBundle(String,Locale)忽略了Locale? - Why is ResourceBundle.getBundle(String, Locale) ignoring the Locale? 在Android应用程序中使用ResourceBundle.getBundle()的正确方法是什么? - What is the proper way to use ResourceBundle.getBundle() in an Android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM