Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Localization question (Resource bundle)  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What will the following code fragment print when compiled and run?  



Assume that only the following two properties files (contents of the file is shown below the name of the file) are accessible to the code.  

1. mymsgs.properties
okLabel=OK
cancelLabel=Cancel    

2. mymsgs_en_UK.properties
okLabel=YES
noLabel=NO


Answer:
noLabel : NO
okLabel : YES
cancelLabel : Cancel


Hey, I am doing this question but could not figure out why does this code output "noLabel : NO  okLabel : YES  cancelLabel : Cancel". Please help, Thanks!
 
Marshal
Posts: 64115
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us whiich bookthat was from, and page number, so as to reduce copyright problems.
Remembering that resource bundle files go for the most specific first using the language (“en”) and country extensons (“UK”), you are going to get yes, OK, Cancel. Try changing one of the bundle files to read, “Yep”, “Nope”, “Forget it”, and see what happens (‍)
 
Micheal Bush
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please tell us whiich bookthat was from, and page number, so as to reduce copyright problems.
Remembering that resource bundle files go for the most specific first using the language (“en”) and country extensons (“UK”), you are going to get yes, OK, Cancel. Try changing one of the bundle files to read, “Yep”, “Nope”, “Forget it”, and see what happens (‍)




Thanks. It is actually from Enthuware. And it prints : "noLabel : NO okLabel : YES cancelLabel : Cancel". I could not figure out why.
 
Enthuware Software Support
Posts: 4304
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is as given in the explanation:


mymsgs.properties is the base file for this resource bundle. Therefore, it will be loaded first. Since the language and region specific file is also present (_en_UK), it will also be loaded and the values in this file will be superimposed on the values of the base file.

 
Micheal Bush
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:The reason is as given in the explanation:


mymsgs.properties is the base file for this resource bundle. Therefore, it will be loaded first. Since the language and region specific file is also present (_en_UK), it will also be loaded and the values in this file will be superimposed on the values of the base file.




Thanks, Paul, but I still have two questions which I am quite confuse about.

1. Why does mysmsgs.properties load first? It should always load the most specific one. I've read the link provided in the solution. It did not mention that the base file will be load firstly. In this case, the mymsgs_en_UK.properties is available. Why does it need to load base file anyway? The API link says it gonna generate candidate bundle names and getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle.

2. Hypothetically speaking, the base file was loaded first. The okLabel from mymsgs_en_UK.properties should superimpose okLabel in mymsgs.properties. Why does the noLabel prints firstly?
 
Paul Anilprem
Enthuware Software Support
Posts: 4304
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Micheal Bush wrote:

Paul Anilprem wrote:The reason is as given in the explanation:


mymsgs.properties is the base file for this resource bundle. Therefore, it will be loaded first. Since the language and region specific file is also present (_en_UK), it will also be loaded and the values in this file will be superimposed on the values of the base file.




Thanks, Paul, but I still have two questions which I am quite confuse about.

1. Why does mysmsgs.properties load first? It should always load the most specific one. I've read the link provided in the solution. It did not mention that the base file will be load firstly. In this case, the mymsgs_en_UK.properties is available. Why does it need to load base file anyway? The API link says it gonna generate candidate bundle names and getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle.

2. Hypothetically speaking, the base file was loaded first. The okLabel from mymsgs_en_UK.properties should superimpose okLabel in mymsgs.properties. Why does the noLabel prints firstly?




Actually, the explanation is simplified so that the result can be easily determined. The complete and exact steps of loading of resource bundles is not as trivial. See this part from JavaDoc API :


Once a result resource bundle has been found, its parent chain is instantiated. If the result bundle already has a parent (perhaps because it was returned from a cache) the chain is complete.

Otherwise, getBundle examines the remainder of the candidate locale list that was used during the pass that generated the result resource bundle. (As before, candidate bundle names where the final component is an empty string are omitted.) When it comes to the end of the candidate list, it tries the plain bundle name. With each of the candidate bundle names it attempts to instantiate a resource bundle (first looking for a class and then a properties file, as described above).

Whenever it succeeds, it calls the previously instantiated resource bundle's setParent method with the new resource bundle. This continues until the list of names is exhausted or the current bundle already has a non-null parent.

Once the parent chain is complete, the bundle is returned.



API description also gives an example where it shows that if your locale is Locale("fr", "CH"), MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class will be loaded.

Only if a key is not found in the child bundle, MyResources_fr_CH.class, the value from parent  MyResources.class. One can visualize this as the entries from the most specific bundle being superimposed on the base bundle.


Regarding printing of values, I don't think there is much to it except that is just using some order while iterating through the keys. It is printing the right values though - the value of okLabel is indeed coming from mymsgs_en_UK.properties bundle and not from the base mymsgs.properties bundle. noLabel is not present in mymsgs_en_UK.properties and so it is coming from mymsgs.properties

 
Micheal Bush
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:

Micheal Bush wrote:

Paul Anilprem wrote:The reason is as given in the explanation:


mymsgs.properties is the base file for this resource bundle. Therefore, it will be loaded first. Since the language and region specific file is also present (_en_UK), it will also be loaded and the values in this file will be superimposed on the values of the base file.




Thanks, Paul, but I still have two questions which I am quite confuse about.

1. Why does mysmsgs.properties load first? It should always load the most specific one. I've read the link provided in the solution. It did not mention that the base file will be load firstly. In this case, the mymsgs_en_UK.properties is available. Why does it need to load base file anyway? The API link says it gonna generate candidate bundle names and getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle.

2. Hypothetically speaking, the base file was loaded first. The okLabel from mymsgs_en_UK.properties should superimpose okLabel in mymsgs.properties. Why does the noLabel prints firstly?




Actually, the explanation is simplified so that the result can be easily determined. The complete and exact steps of loading of resource bundles is not as trivial. See this part from JavaDoc API :


Once a result resource bundle has been found, its parent chain is instantiated. If the result bundle already has a parent (perhaps because it was returned from a cache) the chain is complete.

Otherwise, getBundle examines the remainder of the candidate locale list that was used during the pass that generated the result resource bundle. (As before, candidate bundle names where the final component is an empty string are omitted.) When it comes to the end of the candidate list, it tries the plain bundle name. With each of the candidate bundle names it attempts to instantiate a resource bundle (first looking for a class and then a properties file, as described above).

Whenever it succeeds, it calls the previously instantiated resource bundle's setParent method with the new resource bundle. This continues until the list of names is exhausted or the current bundle already has a non-null parent.

Once the parent chain is complete, the bundle is returned.



API description also gives an example where it shows that if your locale is Locale("fr", "CH"), MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class will be loaded.

Only if a key is not found in the child bundle, MyResources_fr_CH.class, the value from parent  MyResources.class. One can visualize this as the entries from the most specific bundle being superimposed on the base bundle.


Regarding printing of values, I don't think there is much to it except that is just using some order while iterating through the keys. It is printing the right values though - the value of okLabel is indeed coming from mymsgs_en_UK.properties bundle and not from the base mymsgs.properties bundle. noLabel is not present in mymsgs_en_UK.properties and so it is coming from mymsgs.properties



Thanks you very much!
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!