I have a serious question about resource bundles because in the explanation of this question says literally:
While retrieving a message bundle, you are passing a locale explicitly (jp JP). Therefore, it will first try to load appmessages_jp_JP.properties. Since this file is not present, it will look for a resource bundle for default locale. Since you are changing the default locale to "fr", "CA", it will look for appmessages_fr_CA.properties, which is present. This file contains "bonjour" for "greetings", which is what is printed.
Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle. Every effort is made to load a resource bundle if one is not found and there are several fall back options. As a last resort, it will try to load a resource bundle with no locale information i.e. appmessages.properties in this case. An exception is thrown when even this resource bundle is not found.
When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
If you like our products and services, please help us by posting your review here.
admin wrote:When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
Hi!
I expect that the program outputs "msgs_es_ES tu" (default locale) as according with the answer explanation ("Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle.")
Yes, the explanation is correct. Notice that it says, "when a resource bundle is not found for a given locale,... ". In your example, it did find a bundle for the matching locale i.e. msgs_en.properties, so why are you expecting it to load msgs_es_ES.properties?
Remove msgs_en.properties and then it will load msgs_es_ES.properties.
If you like our products and services, please help us by posting your review here.
admin wrote:When I run the program, i see "msgs tu" in the output. So it is picking up this value from msgs.properties. Can you tell what exactly are you expecting in the output and why?
Hi!
I expect that the program outputs "msgs_es_ES tu" (default locale) as according with the answer explanation ("Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle.")
Regards.
Because in my code, the default locale is "es_ES" and the explanation says: "Observe that when a resource bundle is not found for a given locale, the default locale is used to load the resource bundle."
This line in your code - ResourceBundle rb = ResourceBundle.getBundle("msgs", new Locale("en")); - is looking for a resource bundle for "en". You have msgs_en.properties available. So why would it look for the default resource bundle? It will look for resource bundle for default locale "when a resource bundle is not found for a given locale", which is en in this case.
If you like our products and services, please help us by posting your review here.
Tiny bit confused, Paul. So when appmessages_jp_JP.properties can't be found, it looks for appmessages_jp.properties which likewise can't be found. Then it falls back on appmessages.properties and uses the default locale fr_CA. And this is, sort of, appended to the resource bundle name so it is now looking for appmessages_fr_Ca.properties, which it finds in the following:
#In French CA resource bundle file
greetings=bonjour
I'm assuming this file's name is appmessages_fr_Ca.properties which is being sought. Correct? And if this file doesn't exist, an exception is thrown. Am I close?