Posted by joconner
on April 23, 2004 at 4:46 PM PDT
The Java platform loads ResourceBundles for specific locales. The name of your ResourceBundle helps the platform know if it contains the appropriate localization.
Two types of ResourceBundles are provided in the Java platform:
Either one can store localizable resources for your application. Let's assume we use a PropertyResourceBundle.
Starting were we left off, let's assume we have text strings in a bundle named
GreetingResources.properties. Our application loads that bundle with the following:
ResourceBundle res = ResourceBundle.getBundle("com.joconner.demo.GreetingResources");
Once you've loaded the bundle, you can retrieve the localizable text with res.getString("SOME_KEY"). However, how do you create a localized file, and how do you retrieve that bundle?
Create a localized bundle by translating all resource text and placing it in a separate resource bundle. If your original bundle is named GreetingResource.properties, your new bundle should be named GreetingResource_.properties, where represents the target locale for the localization. For example, if we create a Japanese localization, the bundle would be GreetingResource_ja.properties. A Canadian English version would be GreetingResource_en_CA.properties.
The first two lowercase letters of the represent the language, and the next two uppercase letters (if any) represent the region or country for the localization.
When you load bundles, the JRE will search for appropriate bundle for your locale even if you do not specify a locale in the method. Be careful that you do not include the locale designation within the bundle name argument. For example,
res = ResourceBundle.getBundle("com.joconner.demo.GreetingResources");
is correct, but
res = ResourceBundle.getBundle("com.joconner.demo.GreetingResources_ja");
If you want to specify a locale in the method, you must provide the locale as a second argument:
res = ResourceBundle.getBundle("com.joconner.demo.GreetingResources", new Locale("ja"));
- your application should have a default bundle without any locale extensions
- each additional localization will have its own bundle with the same bundle base name but with a extension added just before the filename extension
- the JRE will find and load the appropriate bundle based on the default host locale of your system unless you specify a specific locale in the bundle load method.