Return to site

JAVA CERTIFICATION QUESTION: Localization with ResourceBundle

· java

Assume we have three files with a property named greeting :

Myresource . properties:
greeting=Default greeting 

MyResource_en . properties:
greeting=Hello 

Myresource_fr_fr . properties:
greeting=Bonjour

And a code fragment:

Locale locale = new Locale( "fr", "CA" );
ResourceBundle bundle = ResourceBundle.getBundle( "MyResource", locale );
String value = bundle.getString( "greeting" );
System.out.println( value );

It's known that the default locale is en_US.

What is the output of the given code?

  • Compilation fails
  • A MissingResourceException is thrown
  • Nothing
  • Bonjour
  • Hello
  • Default greeting

 

 

#java #certificationquestion #ocp

Answer: Hello

Since the desired locale is fr_CA and the default one is en_US, resources are searched in the following order:

MyResource_fr_CA
MyResource_fr
MyResource_en_US
MyResource_en
MyResource

A resource for the fr_CA locale is missing; hence, the program falls back to MyResource_en.properties where it finds a value for the required property.