ResourceBundle

Java ResourceBundle Example

In this post, we are going to discuss about the class java.util.ResourceBundle

1. ResourceBundle Class

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user’s locale. In this way, you can write program code that is largely independent of the user’s locale isolating most, if not all, of the locale-specific information in resource bundles.

This allows you to write programs that can:

  • Be easily localized, or translated, into different languages
  • Handle multiple locales at once
  • Be easily modified later to support even more locales

Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be “MyResources“. The family should have a default resource bundle which simply has the same name as its family – “MyResources” – and will be used as the bundle of last resort if a specific locale is not supported. The family can then provide as many locale-specific members as needed, for example a Spanish one named “MyResources_es“.

Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. For example, both “MyResources” and “MyResources_de” may have a String that’s used on a button for canceling operations. In “MyResources” the String may contain “Cancel” and in “MyResources_es” it may contain “Cancelar”.

Before continuing with the ResourceBundle class, let’s talk about a little about the java.util.Locale class.

1.1 Locale Class

A Locale object represents a specific geographical, political, or cultural region, better know as internationalization (often abbreviated “I18N”). Java provides thorough support for localizing the text of your application for most modern languages and dialects. Internationalization programming revolves around the Locale class. The class itself is very simple; it encapsulates a country code, a language code, and a rarely used variant code. Commonly used languages and countries are defined as constants in the Locale class.

The country codes comply with ISO 3166. You will find a complete list of country codes at the RIPE Network Coordination Centre. The language codes comply with ISO 639. A complete list of language codes is online at the US government website. There is no official set of variant codes; they are designated as vendor-specific or platform-specific.

Let’s see an example of using a Resource Bundle file.

2. Executing some code

App.java

package com.javacodegeeks.examples.resourcebundle;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class App {
	private static final Logger logger = Logger.getLogger("App");

	public static void main(String[] args) {

		// ResourceBundle class will use SystemMessages.properties file
		ResourceBundle resourceBundle = ResourceBundle.getBundle(
				"SystemMessages", Locale.getDefault());
		logger.info(resourceBundle.getString("first_name") + ": Armando");
		logger.info(resourceBundle.getString("last_name") + ": Flores");

		// ResourceBundle class will use SystemMessages_es.properties file
		resourceBundle = ResourceBundle.getBundle("SystemMessages",
				Locale.forLanguageTag("es"));
		logger.info(resourceBundle.getString("first_name") + ": Armando");
		logger.info(resourceBundle.getString("last_name") + ": Flores");

		// ResourceBundle class will use SystemMessages_fr.properties file
		resourceBundle = ResourceBundle.getBundle("SystemMessages",
				Locale.FRANCE);
		logger.info(resourceBundle.getString("first_name") + ": Armando");
		logger.info(resourceBundle.getString("last_name") + ": Flores");
	}
}

ResourceBundle - Properties File
ResourceBundle – Properties File

Let’s explain the methods used in the previous code

  • public static final ResourceBundle getBundle(String baseName, Locale locale) – Gets a resource bundle using the specified base name, locale, and class loader. getBundle uses the base name, the specified locale, and the default locale (obtained from Locale.getDefault) to generate a sequence of candidate bundle names. If the specified locale’s language, script, country, and variant are all empty strings, then the base name is the only candidate bundle name. Otherwise, a list of candidate locales is generated from the attribute values of the specified locale (language, script, country and variant) and appended to the base name.
  • public final String getString(String key) – Gets a string for the given key from this resource bundle or one of its parents.
  • public static Locale getDefault() – Gets the current value of the default locale for this instance of the Java Virtual Machine. It is used by many locale-sensitive methods if no locale is explicitly specified.
  • public static Locale forLanguageTag(String languageTag) – Returns a locale for the specified IETF BCP 47 language tag string. If the specified language tag contains any ill-formed subtags, the first such subtag and all following subtags are ignored.

The output of the command java com.javacodegeeks.examples.resourcebundle.App should be similar to:

Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: First Name: Armando
Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: Last Name: Flores
Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: Nombre: Armando
Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: Apellido Paterno: Flores
Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: Nom: Armando
Sep 09, 2014 9:25:46 PM com.javacodegeeks.examples.resourcebundle.App main
INFO: Nom de famille: Flores

3. Download the Eclipse project of this tutorial:

This was an example of how to set use the ResourceBundle Class.

Download
You can download the full source code of this example here : resourcebundleexample.zip

Armando Flores

Armando graduated from from Electronics Engineer in the The Public University Of Puebla (BUAP). He also has a Masters degree in Computer Sciences from CINVESTAV. He has been using the Java language for Web Development for over a decade. He has been involved in a large number of projects focused on "ad-hoc" Web Application based on Java EE and Spring Framework.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button