Skip to main content

I18n How-to: ResourceBundle naming

Posted by joconner on April 23, 2004 at 4:46 PM PDT

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 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, your new bundle should be named, where represents the target locale for the localization. For example, if we create a Japanese localization, the bundle would be A Canadian English version would be

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");

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,

  1. your application should have a default bundle without any locale extensions
  2. each additional localization will have its own bundle with the same bundle base name but with a extension added just before the filename extension
  3. 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.

Related Topics >>


<p>&nbsp;Hi,</p> <p>The topic is helpful and I have now ...


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