Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 5:46 am
by daniko
Why is the third option false? The default Locale is set to ITALY, so the fallback method should check for a bundle according to the default locale.

I also ran it with Eclipse and it works. Have i missed something?

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 6:44 am
by admin
But the language has been set to "en".
Please post the exact code that you tried and the resource files that you have.

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 8:07 am
by daniko
Ahm, Sorry now i got it.
I wrote in my First comment about a fallback. There is no Fallback because the "en" bundle is found but it is empty, so there is no reason to search for the "it".

I tried it in eclipse with only the IT bundle and that's why it worked. My bad.

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 9:14 am
by admin
In this case, the fallback would be mymsgs_en.properties' parent mymsgs.properties.

mymsgs_it.properties or mymsgs_en_it.properties will not be loaded because the resource bundle is being created for a specific Locale ("en"). If you omit the Locale argument ( i.e. ResourceBundle.getBundle("mymsgs"); ), then files related to the default locale "it" will be used.

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 9:44 am
by daniko

Code: Select all

import java.util.Locale;
import java.util.ResourceBundle;

public class LocaleTest {
	public static void main(String[] args) {
		Locale.setDefault(Locale.ITALY);
		Locale loc = new Locale.Builder().setLanguage("en").build();
		ResourceBundle rb = ResourceBundle.getBundle("mymsgs", loc);
		System.out.println(rb.getString("helloMsg"));
	}
}
This piece of code loads the bundle mymsgs_IT.properties anyway when it's the only bundle in the classpath and the value of helloMsg is printed...
Running in Eclipse

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Tue Aug 04, 2020 10:26 am
by admin
Ok, I guess I will have to go through this again!

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Sat Dec 19, 2020 7:51 am
by borkutip
Hello,
I do not understand, how can mymsgs_IT.properties be loaded.

Locale ITALY = createConstant("it", "IT");
so the property file for Locale.ITALY should be:
mymsgs_it.properties or
mymsgs_it_IT.properties.

So, I think, at least on Linux, mymsgs_IT.properties will never be loaded.
Is this question/explanation windows-related?

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Sat Dec 19, 2020 10:55 am
by admin
Here is the situation:

1. The default Locale is set to Locale.IT That means its language is "it" and country is "IT".

2. A new Locale is being built using Locale loc = new Locale.Builder().setLanguage("en").build(); This means, the language is "en" but the country ( and script and variant) is empty for this Locale.

Now, when you try to load a RB using ResourceBundle.getBundle("mymsgs", loc);, it will look for mymsgs_en.properties only (because country etc. values are empty).

But mymsgs_en.properties is not available, so, getFallbackLocale method is called, which returns the current default locale (here, Locale.IT), and a new search will be made for mymsgs_it_IT.properties and then mymsgs_it.properties

Since mymsgs_it_IT.properties is not available, only mymsgs_it.properties will be loaded (since it is available).

3. Next, the "parent chain" for the resource bundle just loaded (i.e. mymsgs_it_IT.properties) is instantiated. In this case, the parent chain for mymsgs_it_IT.properties will be mymsgs_it and mymsgs.properties (which are not available either as per the problem statement).

Therefore, yes, mymsgs_IT.properties should not be loaded but, since file names are case insensitive on Windows, mymsgs_IT.properties will be loaded on windows.

Re: About Question enthuware.ocpjp.ii.v11.2.3311 :

Posted: Sat Dec 19, 2020 2:23 pm
by borkutip
Hello,

Thank you.