Search code examples
javalocalizationinternationalizationlocaleresourcebundle

Sequence of Locales


What exactly is the sequence in which the Locales are used? I have 3 properties file:

Dolphins.properties

name=The Dolphin
age=0

Dolphins_en.properties

name=Dolly
age=4

Dolphins_fr.properties

name=Dolly

My code is:

5: Locale fr = new Locale("fr");

6: Locale.setDefault(new Locale("en", "US"));

7: Resource Bundle b = ResourceBundle.getBundle("Dolphins", fr);

8: b.getString("name");

9: b.getString("age");

The code sets the default Locale to be Dolphins_en then why does it use Dolphins.properties? What am I missing or misunderstanding?

Thanks in advance.


Solution

  • Default locale is used when selecting the bundle chain. It means, that it will be used when you don't specify it: ResourceBundle.getBundle("Dolphins") or when you specify locale where the bundle is not present: ResourceBundle.getBundle("Dolphins", new Locale("cs")).

    But when the bundle is selected then the default locale is not used any more for value selection. It means that when the key age is not present in Dolphins_fr.properties it will use value from the default bundle Dolphins.properties.

    Note: And if the key is not even in Dolphins.properties it will throw MissingResourceException.

    Update: I felt that I saw the same code in the past and finally got it. You can look in Jeanne Boyarsky - OCP Study Guide or here https://coderanch.com/t/685833/certification/OCP-Chapter-Jeanne-Boyarsky, where is exactly the same code and reasons are explained in a better English.