Using a ResourceBundle for localization

      No Comments on Using a ResourceBundle for localization

There is a nice java tutorial from Sun how to use ReseouceBundle for localization: http://java.sun.com/docs/books/tutorial/i18n/resbundle/propfile.html.

It’s actually rather simple: Just create a new ResourceBundle object by invoking the getBundle method, specifying the base name and Locale:

ResourceBundle labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);

The getBundle method first looks for a class file that matches the base name and the Locale. If it can’t find a class file, it then checks for properties files. When the getBundle method locates the correct properties file, it returns a PropertyResourceBundle object containing the key-value pairs from the properties file.

To retrieve the translated value from the ResourceBundle, invoke the getString method as follows:

String value = labels.getString(key);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.