ResourceBundle class in Java

ResourceBundle class in Java

  1. ResourceBundle class
  2. Commonly used methods of ResourceBundle class
  3. Example of ResourceBundle class

The ResourceBundle class is used to internationalize the messages. In other words, we can say that it provides a mechanism to globalize the messages.

The hardcoded message is not considered good in terms of programming, because it differs from one country to another. So we use the ResourceBundle class to globalize the massages. The ResourceBundle class loads these informations from the properties file that contains the messages.

Conventionally, the name of the properties file should be filename_languagecode_countrycode for example MyMessage_en_US.properties.


Commonly used methods of ResourceBundle class

There are many methods in the ResourceBundle class. Let’s see the commonly used methods of the ResourceBundle class.

  • public static ResourceBundle getBundle(String basename) returns the instance of the ResourceBundle class for the default locale.
  • public static ResourceBundle getBundle(String basename, Locale locale) returns the instance of the ResourceBundle class for the specified locale.
  • public String getString(String key) returns the value for the corresponding key from this resource bundle.

Example of ResourceBundle class

Let’s see the simple example of ResourceBundle class. In this example, we are creating three files:

  • MessageBundle_en_US.properties file contains the localize message for US country.
  • MessageBundle_in_ID.properties file contains the localize message for Indonaisa country.
  • InternationalizationDemo.java file that loads these properties file in a bundle and prints the messages.

MessageBundle_en_US.properties

MessageBundle_in_ID.properties

InternationalizationDemo.java

  1. import java.util.Locale;
  2. import java.util.ResourceBundle;
  3. public class InternationalizationDemo {
  4.  public static void main(String[] args) {
  5.   ResourceBundle bundle = ResourceBundle.getBundle(“MessageBundle”, Locale.US);
  6.   System.out.println(“Message in “+Locale.US +“:”+bundle.getString(“greeting”));
  7.   //changing the default locale to indonasian 
  8.   Locale.setDefault(new Locale(“in”“ID”));
  9.   bundle = ResourceBundle.getBundle(“MessageBundle”);
  10.   System.out.println(“Message in “+Locale.getDefault()+“:”+bundle.getString(“greeting”));
  11.  }
  12. }
Output:Message in en_US : Hello, how r u?
       Message in in_ID : halo, apa kabar?
download this example of ResourceBundle class
Next TopicInternationalizing Date
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

ResourceBundle class in Java ResourceBundle class Commonly used methods of ResourceBundle class Example of ResourceBundle class The ResourceBundle class is used to internationalize the messages. In other words, we can say that it provides a mechanism to globalize the messages. The hardcoded message is not considered good in terms of programming, because it differs from one country […]

Tags:

....
...