Close

Java - Introduction to ResourceBundle

[Last Updated: Jul 28, 2021]

Java class ResourceBundle helps us to create software supporting different languages.

If our application represent texts that need to be translated into various languages, we should store these texts in properties files. These properties files can be loaded by our Java program. Since the properties files are simple text files, they can be created and maintained by the translators. That way we don't have to change the source code in order to represent text in different languages.

These property files (resources) follows a specific naming convention to support different languages:

 basedName_languageCode_countryCode.properties

For example :
myLabels_en_US.properties should contains properties values written in US english.
myLabels_en_GB should contains properties values written in GB (Great Britain) english

A ResourceBundle instance represents a set of one or more properties files related to a specific language.

How to access language specific properties?

Following method provided by ResourceBundle class can be used to load the resource belonging to default locale:

public static final ResourceBundle getBundle(String baseName)

Following method loads a resource bundle using the specified locale:

public static final ResourceBundle getBundle(String baseName, Locale locale)

Following instance method of ResourceBundle can be used to access a specific property via a resource key.

public final String getString(String key)

Language vs Locale

A language is what we speak. Locale is a super set of language. A Locale represents language and other regional specific parameters (e.g. currency symbol, number format, date time format).

Example

Locale specific property resources

src/main/resources/src/app-data_en_US.properties

greeting-message= "Hi there! How are you doing?" 

src/main/resources/src/app-data_de_DE.properties

greeting-message= "Hallo! Wie geht es dir?" 

Java class using ResourceBundle

package com.logicbig.example;

import java.util.Locale;
import java.util.ResourceBundle;

public class ExampleApp {
  public static void main(String[] args) {
      //default locale
      ResourceBundle bundle = ResourceBundle.getBundle("app-data");
      String message = bundle.getString("greeting-message");
      System.out.println(message);

      //different locale
      ResourceBundle bundle2 = ResourceBundle.getBundle("app-data", Locale.GERMANY);
      String message2 = bundle2.getString("greeting-message");
      System.out.println(message2);
  }
}

Output

"Hi there! How are you doing?" 
"Hallo! Wie geht es dir?"

Example Project

Dependencies and Technologies Used:

  • JDK 16 Version Compatibility: 1.1 - 16 Version List
    ×

    Version compatibilities of JDK with this example:

    • 1.1
    • 1.2
    • 1.3
    • 1.4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Versions in green have been tested.

Java - ResourceBundler Quick Example Select All Download
  • java-resource-bundle-example
    • src
      • com
        • logicbig
          • example
            • ExampleApp.java

    See Also