问题描述
我正在使用java.util.ResourceBundle
类和属性文件来本地化Web应用程序.
I am localizing a web application using a java.util.ResourceBundle
class and property files.
我有两个语言环境fr_FR和en_US,并且我想使用en_US作为默认语言,所以我编写了以下文件:
I have two locales, fr_FR and en_US, and I want to use en_US as the default, so I wrote the following files :
-
messages_fr_FR.properties
带有fr_FR消息 -
messages.properties
带有en_US消息
messages_fr_FR.properties
with fr_FR messagesmessages.properties
with en_US messages
我的问题是 ResourceBundle.getBundle(BUNDLE_NAME,语言环境)方法在使用默认属性文件之前回退Locale.getDefault()
,这意味着如果JVM语言环境设置为fr_FR,ResourceBundle.getBundle("name", new Locale("en", "US"))
返回fr_FR束.
My problem is that the ResourceBundle.getBundle(BUNDLE_NAME, locale) method fall back Locale.getDefault()
before using the default property file, which means if the JVM Locale is set to fr_FR,ResourceBundle.getBundle("name", new Locale("en", "US"))
returns the fr_FR bundle.
我可以重命名messages.properties
文件,但这可能会返回MissingResourceException以及其他所需的默认语言环境.
I could rename the messages.properties
file, but this could returns a MissingResourceException with other desired and default locales.
如何在不复制属性文件或调用Locale.setDefault
的情况下忽略系统区域设置?
How can I ignore the System Locale, without duplicating the property file or calling Locale.setDefault
?
推荐答案
您可以使用自定义ResourceBundle.Control
来实现此目的,方法是显式重载getFallbackLocale
或使用:
You can do that by using a custom ResourceBundle.Control
, either by overloading the getFallbackLocale
explicitly, or by using :
ResourceBundle.getBundle("name", new Locale("en", "US"), ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES))
这篇关于如何忽略系统默认语言环境以检索resourceBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!