我如何获得系统的当前语言并将其用于 ResourceBundle?
假设客户端的机器以日语运行,那么我希望我的应用程序切换到日语,如果该语言文件不可用,它应该有一个后备解决方案。
我知道我可以使用 ResourceBundle,但我将如何完成上述设置?
ResourceBundle bundle = ResourceBundle.getBundle("lang", new Locale("jp", "JP"));
我如何获得系统的当前语言并将其用于 ResourceBundle?
假设客户端的机器以日语运行,那么我希望我的应用程序切换到日语,如果该语言文件不可用,它应该有一个后备解决方案。
我知道我可以使用 ResourceBundle,但我将如何完成上述设置?
ResourceBundle bundle = ResourceBundle.getBundle("lang", new Locale("jp", "JP"));
The resource bundle automatically tries the system's current language, and then falls back to the default if that doesn't exist (ResourceBundle.getBundle("lang")
will search in the following order, presuming that the system language is jp_JP
: lang_jp_JP.properties
-> land_jp.properties
-> lang.properties
). You can look at the process ResourceBundle uses to resolve names here
From http://java.sun.com/developer/technicalArticles/Intl/ResourceBundles/
For each language translation of your product, you create a new version of your initial resource bundle. For example, if you create MyResource to store all English text, you will create a similarly named file to store the French text. Resource bundles use a naming convention that distinguishes the potentially numerous versions of essentially the same bundle. Each bundle name consists of a base name and an optional locale identifier. Together, these two pieces uniquely identify a bundle in a package.
Using the above example, the French version of the resources should be named MyResource_fr_FR. The Canadian French version should be MyResource_fr_CA.
Appending the locale name to the base name of the bundle effectively links the bundle's usage to a specific locale. When you ask for the bundle using ResourceBundle.getBundle("MyResource"), the getBundle method appends the default locale identifier to the base name and loads the appropriate bundle. If the locale is fr_CA, then a call to ResourceBundle.getBundle("MyResource") will load the MyResource_fr_CA bundle.