How to use localization in Java?

Java 11 Certification Preparation

Section: Localization

Objectives: Implement Localization using Locale, resource bundles, and Java APIs to parse and format messages, dates, and numbers

What are resource bundles and how are they used?

A resource bundle is like a map that contains the locale-specific objects that can be looked up using keys. The resource bundle is commonly stored in a properties file with key/value pairs.

To select the appropriate ResourceBundle, invoke the ResourceBundle.getBundle method.

Assume that you have the property file as shown below.

To read the value of the key ‘city’, we can use the getBundle() method of ResourceBundle class, passing the Locale details as shown below.

Locale.setDefault(new Locale("fr"));
ResourceBundle rb = ResourceBundle.getBundle("Address", new Locale("en"));

To select the appropriate ResourceBundle for a key, Java will look in the order specified below.

  1. ResourceBundle class for the specified Locale (match both language and country)
  2. ResourceBundle class for the specified Locale (match only language)
  3. ResourceBundle class for the default Locale (match both language and country)
  4. ResourceBundle class for the default Locale (match only language)
  5. Use the default resource bundle if no matching locale can be found.

For details, refer

Practice Questions (Answers below)

Q1. Assume you have two property files as follows and the default locale is en_IN.
hello=How are you
What will be the result of running this code?
var CA = new Locale("en", "CA");
var b = ResourceBundle.getBundle("greetings", CA);

A. MissingResourceException is thrown
B. Prints “Howdy”
C. Prints “How are you”
D. Does not compile

Q2. Given the following code. Which of the following will compile and run without errors? (Choose all that apply.)
var dateTime1 = LocalDateTime.of(2020, Month.NOVEMBER, 30, 10, 5, 30);
A. var format1 = DateTimeFormatter.ofPattern("MMMM dd', School''s at' hh:mm");

B. var format2 = DateTimeFormatter.ofPattern("'Join class,! 'hh:mm', time!'");

C. var format3 = DateTimeFormatter.ofPattern("'Time schedule, hh:mm: 'hh:mm");	System.out.println(dateTime1.format(format3));

D. var format4 = DateTimeFormatter.ofPattern("h:mm z");


Q1. B is the correct answer. Here, there is no property file for the specified locale – locale en_CA
If a ResourceBundle class for the specified Locale does not exist, getBundle tries to find the closest match. In this case, getBundle will look for classes in the following order:

The file will not be considered as the language is not specified and there is no file Hence is chosen and the value of the property ‘hello’ (“Howdy”) is printed.

Q2. A, B, and C are correct. D will throw an exception at runtime because the underlying LocalDateTime does not have a time zone specified. If ZonedDateTime was used instead, then the code would have completed and run successfully. Refer for details.

Leave a Reply

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