I18n How-to: ResourceBundle naming
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_
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");
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.