Page 1 of 1
About Question enthuware.ocpjp.v8.2.1152 :
Posted: Thu Apr 19, 2018 6:45 am
by jabenitez
Hi all,
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.
You need to understand this aspect for the purpose of the exam. Please go through
http://docs.oracle.com/javase/7/docs/ap ... assLoader) for further details.
However, I have made a small example in which I can observe that it never takes the default properties (it's attached to this post).
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Thu Apr 19, 2018 10:47 pm
by admin
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?
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Sat Apr 21, 2018 6:48 am
by jabenitez
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.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Sat Apr 21, 2018 9:15 pm
by admin
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.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Wed Apr 25, 2018 4:24 am
by jabenitez
jabenitez wrote: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."
Thanks and best regards.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Wed Apr 25, 2018 4:53 am
by admin
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.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Thu Apr 26, 2018 1:10 am
by jabenitez
Hi!
I understood.
Many thanks and best regards.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Tue Oct 09, 2018 6:25 pm
by Mark7777
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?
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Tue Oct 09, 2018 9:06 pm
by admin
That is correct!
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Sun Jul 23, 2023 3:48 pm
by d.d.Manchev
Hey, Paul, can you please validate the search order:
appmessages_jp_JP
appmessages_jp
appmessages_fr_CA
appmessages_fr
appmessages
correct?
P.S: This is the case it can't find the key in any of the .properties files( assume they exist for the given example )
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Mon Jul 24, 2023 9:43 am
by admin
Yes, that is correct.
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Wed Feb 19, 2025 2:28 am
by pablin13
Just FYI, maybe is not relevant for this question, but the language code for the Japan locale is "ja", not "jp". So the second line of code should be:
Code: Select all
Locale l = new Locale("ja", "JP");
Re: About Question enthuware.ocpjp.v8.2.1152 :
Posted: Wed Feb 19, 2025 5:21 am
by admin
Yes, ja would be better. Doesn't affect the question though.