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.

Address_en.properties
city=Mumbai

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"));
System.out.println(rb.getString("city"));

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 https://docs.oracle.com/javase/tutorial/i18n/resbundle/concept.html

Practice Questions (Answers below)

Q1. Assume you have two property files as follows and the default locale is en_IN.
greetings_en.properties
hello=Howdy

greetings_CA.properties
hello=How are you
What will be the result of running this code?
var CA = new Locale("en", "CA");
System.out.println(Locale.getDefault());
var b = ResourceBundle.getBundle("greetings", CA);
System.out.println(b.getString("hello"));
Choices

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);
Choices
A. var format1 = DateTimeFormatter.ofPattern("MMMM dd', School''s at' hh:mm");
System.out.println(dateTime1.format(format1)); 

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

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");
System.out.println(dateTime1.format(format4));

Answers

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:
greetings_en_CA
greetings_en
greetings_en_IN
greetings

The file greetings_CA.properties will not be considered as the language is not specified and there is no file greetings_en_CA.properties. Hence greetings_en.properties 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 https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html for details.

Leave a Reply

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