|
Java™ by example!
|
|
|
How is my .properties ResourceBundle found?
Suppose we have a .properties file called LabelsBundle_nl_BE.properties and assume also our default locale is en_US. To get access to that bundle, you can invoke ResourceBundle.getBundle("LabelsBundle", "nl", "BE"). Notice that a resource bundle consists of two parts: a family name and a locale part. The locale part contains a language code, a country code and an optional variant code. In our example, it will try to locate the bundle in the following way:
Notice that once the .propreties file has been located, it will still continue to look for parent resourcebundles by shortening the locale part delimited by an underscore. Now assume we only have a .properties file called LabelsBundle_en and we invoke ResourceBundle.getBundle("LabelsBundle", "nl", "BE"). Notice here that it will start from our desired locale and build its way up the tree, including searching for a match with the default locale.
Assume we have a .properties file called LabelsBundle_nl_BE.properties and a .properties file called LabelsBundle.properties.
In this example, LabelsBundle.properties has become a parent resource bundle of LabelsBundle_nl_BE. You can use the property keys from both these files through the instance that you get back from invoking ResourceBundle.getBundle(...). Also notice that if the resource bundle has been found using the desired locale, the default locale will not be used in looking for a parent. The default locale is only used if locating a bundle with the desired locale failed. As a last note, you must specify the fully qualified name for the family name (or baseclass) when requesting a resource bundle object. For example, if LabelsBundle_nl_BE.properties is in the package mypackage, you will have to say:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|