violette generic
violette lujoso
tb864615.OCPJAVASE17PT.c14.046
Given the following three property files, what does the following method output?
toothbrush.properties
color=purple
type=generic
toothbrush_es.properties
color=morado
type=lujoso
toothbrush_fr.properties
color=violette
stacktrace wrote:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name toothbrush, locale fr
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Tim Holloway wrote: default values coming from toothbrush.properties. You are overriding the Locale, so the fact that you set the default locale to es_MX shouldn't matter here.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Paul Clapham wrote:
So the code from the book does actually produce the "es-MX" Locale
violette generic
Locale.getDefault: es_MX
Anil Philip wrote:From my understanding, it should go to the default locale if it cannot find the exact-match property file.
So I don't know why it doesn't.
Paul Clapham wrote:That's what the tutorial I linked to says too. (I'm pretty sure it says that.)
1) Look for the resource bundle for the requested locale, followed by the one for the default locale.
2) For each locale, check the language/country, followed by just the language.
3) Use the default resource bundle if no matching locale can be found.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Anil Philip wrote:1) Look for the resource bundle for the requested locale, followed by the one for the default locale.
Paul Clapham wrote:You could test that by adding an es-MX property file to your setup.
violette generic
Locale.getDefault: es_MX
color=dazzle
type=bling
Anil Philip wrote:The output remains the same. See screenshot.
Paul Clapham wrote:No, wait a minute, I'm still confused by Locale terminology. The Locale.Builder documentation talks about "language" and "region" and Locale talks about "language" and "country". For example there's a Locale called CANADA_FRENCH whose name is "fr-CA". The meaning of the thing is "French as used in Canada". Likewise "es-MX" is "Spanish as used in Mexico" and "es" is "Spanish default usage".
country (region)
ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code. You can find a full list of valid country and region codes in the IANA Language Subtag Registry (search for "Type: region"). The country (region) field is case insensitive, but Locale always canonicalizes to upper case.
Well-formed country/region values have the form [a-zA-Z]{2} | [0-9]{3}
Example: "US" (United States), "FR" (France), "029" (Caribbean)
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Java starts out by looking for a properties file with the requested locale. In this case, the
requested locale is the fr language, which it finds with toothbrush_fr.properties.
For this reason, the default locale of es_MX is ignored, and the first value for color printed
is violette. Next, it looks for a value for type property. Since it doesn’t find it in the first
properties file, it checks the default toothbrush.properties, where it does find it, and
prints generic. For these reasons, option B is correct.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stephan van Hulst wrote:You seem confused what "default locale" really means.
Java handles the logic of picking the best available resource bundle for a given key. It tries to find the most specific value. Table 11.11 shows what Java goes through when asked for resource bundle Zoo with the locale new Locale("fr", "FR") when the default locale is U.S. English.
TABLE 11.11 Picking a resource bundle for French/France with default locale English/US
see screenshot
As another way of remembering the order of Table 11.11, learn these steps:
Look for the resource bundle for the requested locale, followed by the one for the default locale.
For each locale, check the language/country, followed by just the language.
Use the default resource bundle if no matching locale can be found.
Jeanne Boyarsky wrote:
And to use different words since Anil is confused,
Anil Philip wrote:
Jeanne Boyarsky wrote:
And to use different words since Anil is confused,
Please see the screenshot of the table from the book. Step 2 and Step 3.
It looks for fr and then goes to the default en_US.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stephan van Hulst wrote:I see. Yes, that's incorrect.
Either steps 1, 2, 5, 6 are taken if you explicitly specify a locale, or steps 3, 4, 5, 6 are taken if you don't explicitly specify a locale.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:The locale is specified but there are no matching files so Java goes on to the default locale.
Jeanne Boyarsky wrote:Next, it looks for a value for type property. Since it doesn’t find it in the first
properties file, it checks the default toothbrush.properties, where it does find it, and
prints generic. For these reasons, option B is correct.
Stephan van Hulst wrote:I double checked it, you're correct.
That's extremely disappointing behavior.
That just makes me want to set the default locale to some obscure locale that I know will never be used in my application.
Anil Philip wrote:
Jeanne Boyarsky wrote:The locale is specified but there are no matching files so Java goes on to the default locale.
Jeanne Boyarsky wrote:Next, it looks for a value for type property. Since it doesn’t find it in the first
properties file, it checks the default toothbrush.properties, where it does find it, and
prints generic. For these reasons, option B is correct.
But the book does not say that now it starts looking for properties, and it just shoots up to the generic toothbrush.properties.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:The chapter part does explain it more thoroughly (ex: the table)
The JVM will provide data from en_US only if there is no matching fr_FR or fr resource bundle. If it finds a fr_FR or fr resource bundle, then only those bundles, along with the default bundle, will be used.
Stephan van Hulst wrote:No. You are once again confusing "default bundle" and "default locale". To avoid confusion, I prefer to say "root bundle" instead of "default bundle".
Stephan van Hulst wrote:
There are two separate phases that you need to consider in isolation from each other. Phase one is getting the resource bundle. Phase two is getting a string from the found resource bundle.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ira Go wrote:Here is a javadoc page for the default Resource Bundle Search and Loading Strategy.
Good night. Drive safely. Here's a tiny ad for the road:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|