I18n How-to: ResourceBundle naming
Two types of ResourceBundles are provided in the Java platform:
- PropertyResourceBundle
- ListResourceBundle
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_
The first two lowercase letters of the
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");
is not.
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"));
In brief,
- 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.
- Login or register to post comments
- Printer-friendly version
- joconner's blog
- 1527 reads






Comments
<p> Hi,</p> <p>The topic is helpful and I have now ...
by rjalori - 2011-07-19 05:12
Hi,
The topic is helpful and I have now implemented the same in my project. However I get a problem while running the same on MAC OS X. It always picks up the default locale as US English. The code is:
Locale lclLanguage;
ResourceBundle rbLangBundle = null;
lclLanguage = new Locale(Locale.getDefault().getLanguage(),Locale.getDefault().getCountry());
rbLangBundle = ResourceBundle.getBundle("MessagesBundle", lclLanguage);
strMessage = rbLangBundle.getString("PS10001");
Any help will be much appreciated. Thanks