Java – ResourceBundle example

The java.util.ResourceBundle is a library used for internationalization (multiple languages). It is able to return messages as per the default Locale configured for the system. Such a functionality is used when one develops systems to be used all over the world.

1. How it works?

The library reads a property file based on the locale and name suffix used in the naming of the property. For example, consider a label file named MyLabels_en_US.properties. This file is read by ResourceBundle utility when the Locale is set to en_US which stands for US-English.

Locale represents a region or location with its attributes. Locale is generally used to maintain details about the client using our application. Locale contains attributes of location and language to be used for the respective location. Thus, a Locale assists ResourceBundle to pick the right label file by determining the location to which the user belongs.

2. Example

Let us see the below example for even more clarity on the topic:

MyLabels_en_US.properties

how_are_you = How are you?
MyLabels_ms_MY.properties

how_are_you = apa khabar
Application.java

package com.mkyong;

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

public class Application {

	public static void main(String[] args) {

		// en_US
		System.out.println("Current Locale: " + Locale.getDefault());
		ResourceBundle mybundle = ResourceBundle.getBundle("MyLabels");

		// read MyLabels_en_US.properties
		System.out.println("Say how are you in US English: " + mybundle.getString("how_are_you"));

		Locale.setDefault(new Locale("ms", "MY"));

		// read MyLabels_ms_MY.properties
		System.out.println("Current Locale: " + Locale.getDefault());
		mybundle = ResourceBundle.getBundle("MyLabels");
		System.out.println("Say how are you in Malaysian Malaya language: " + mybundle.getString("how_are_you"));

	}

}

On executing this code, we get the below output:


Current Locale: en_US
Say how are you in US English: How are you?
Current Locale: ms_MY
Say how are you in Malaysian Malaya language: apa khabar

As it can be seen in the output, depending on the default value set for the Locale set. This utility is mainly used in Web Applications where the web browser assists in knowing the Locale of the remote user. This makes it possible to show the website in a specific language as far as available.

Download Source Code

References

  1. Locales Javadocs
  2. ResourceBundle Javadocs
  3. Available Locales

About the Author

author image
Datsabk
A technically sound person, oriented towards learning things logically rather than technically. It is my logical approach that has helped me learn and take up any programming language and start with coding. With a responsibility of Java based Web development professionally, I highly believe in imparting knowledge online for any technical subject that I can handle.

Comments

Leave a Reply

avatar
newest oldest most voted
Sriram Sridharan
Guest
Sriram Sridharan

Hi, this was a neat article. I have a doubt, though. What if I want to display a link like “Click here to reset your password”, and I want this to be localized to multiple languages. In that case, how should I set up my properties file? Because the “Click Here” part can appear anywhere in the sentence depending on the language. Can I have a html markup inside my properties file?