Skip to content

Latest commit

 

History

History
304 lines (271 loc) · 8.62 KB

02-resource-bundle.asc

File metadata and controls

304 lines (271 loc) · 8.62 KB

Resource Bundle

Objective
Build a resource bundle for a locale and call a resource bundle from an application.

The candidate is expected to understand and analyze the use of resource bundles and its relationship to the Locale class.

When coding an internationalized application, it is common to use resource bundles. These are files, usually .properties or Java classes, that store Strings. Each file contains different language Strings, or Locales.

Before proceeding, understand the execution of the main method in the following example and what is presented on the console after its execution.

src/org/j6toj8/localization/resourcebundle/ResourceBundle_Complete.java
link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_Complete.java[role=include]
../../../resources/Text.properties
link:../../../resources/Text.properties[role=include]
../../../resources/Text_pt.properties
link:../../../resources/Text_pt.properties[role=include]
../../../resources/Text_pt_BR.properties
link:../../../resources/Text_pt_BR.properties[role=include]
../../../resources/Text_es.properties
link:../../../resources/Text_es.properties[role=include]
../../../resources/Text_es_ES.properties
link:../../../resources/Text_es_ES.properties[role=include]
Saída no console
 -- Default Locale (en_US) --
tripod - tripod
keyboard - keyboard
glass - glass
paper - paper
phone - phone
rubber - rubber
sticker - sticker
pen - pen

 -- Locale es_ES --
tripod - tripod
keyboard - teclado
glass - 	vaso
paper - paper
phone - phone
rubber - rubber
sticker - sticker
pen - pen

 -- Locale pt_BR --
tripod - tripod
keyboard - keyboard
glass - glass
paper - papel
phone - phone
rubber - rubber
pen - caneta
sticker - sticker
  1. The name of Locale is the filename suffix, and the default resource bundle has no suffix. Examples:

    • Text.properties → Default Locale

    • Text_pt_BR.properties → Locale pt_BR

    • Text_pt.properties → Locale pt

    • Text_es_ES.properties → Locale es_ES

    • Text_es.properties → Locale es

  2. The .properties file can be expressed with 3 different separators: = (equal), : (colon) or a blank space.

    ../../../resources/Text.properties
    link:../../../resources/Text.properties[role=include]

    The most common is using = to separate properties, but all 3 work the same way.

  3. In .properties files, lines that start with # or ! are comments.

    ../../../resources/Text_pt_BR.properties
    link:../../../resources/Text_pt_BR.properties[role=include]
    ../../../resources/Text_es_ES.properties
    link:../../../resources/Text_es_ES.properties[role=include]
  4. In .properties files, only spaces at the end of the line are considered.

    ../../../resources/Text_pt.properties
    link:../../../resources/Text_pt.properties[role=include]

    In this example, you cannot see, but there are 3 spaces at the end of the line. The result is the same as writing paper=paper{sp}{sp}{sp}.

  5. In .properties files, if you end the line with a backslash, you can break the line.

    ../../../resources/Text_es.properties
    link:../../../resources/Text_es.properties[role=include]

    In this example, it would be the same as typing on a single line: keyboard=teclado.

  6. In .properties files, you can also use Java characters such as \t and \n.

    ../../../resources/Text_es_ES.properties
    link:../../../resources/Text_es_ES.properties[role=include]

    In this example, there is a tab before the word vaso. You can see in the first example of this chapter that the word vaso was printed on the console with a left tab.

  7. You can retrieve all resource bundle keys and values programmatically.

    src/org/j6toj8/localization/resourcebundle/ResourceBundle_KeysProgrammatically.java
    link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_KeysProgrammatically.java[role=include]
    console output
    tripod - tripod
    keyboard - keyboard
    glass - glass
    paper - papel
    phone - phone
    rubber - rubber
    pen - caneta
    sticker - sticker
  8. The resource bundle can also be a Java class.

    resource/Text_fr_CA.java
    link:../../../resources/Text_fr_CA.java[role=include]
    src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaBundle.java
    link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaBundle.java[role=include]
    console output
    verre
  9. When using Java classes, one advantage is that you can store values other than String.

    resource/Text_fr_FR.java
    link:../../../resources/Text_fr_FR.java[role=include]
  10. The file nomenclature is the same for Java classes and .properties files, changing only the extension.

    Note that the .properties files are named Text_xx_XX.properties, and the .java files are named Text_xx_XX.java. Programmatically, both are used in the same way.

  11. If there is a .properties file and a Java class for the same Locale, the Java class is used.

    resource/Text_fr_CA.java
    link:../../../resources/Text_fr_CA.java[role=include]
    resource/Text_fr_CA.properties
    link:../../../resources/Text_fr_CA.properties[role=include]
    src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaClassTakesPrecedence.java
    link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaClassTakesPrecedence.java[role=include]
    console output
    stylo
    verre
    clavier

    Note that the values presented in the console are those defined in the Text_fr_CA.java file, showing that the Java class takes precedence over a .properties file for the same Locale.

  12. When searching for a resource bundle, Java tries to find a file with the exact Locale. If not find, search in the following order:

    1. A file of the same language, but without the country;

    2. A file from the default Locale;

    3. A standard Locale file, but without the country;

    4. A file without Locale, which is the default resource bundle;

    5. Throws MissingResourceException if not found.

      For example, when running the application with the default Locale en_US, and requesting a Locale pt_BR, the resource bundle search order would be as follows:

      1. Text_pt_BR → Exact Locale

      2. Text_pt → Requested Locale, without country

      3. Text_en_US → Default Locale

      4. Text_en → Default Locale, without country

      5. TextResource Default Bundle

        src/org/j6toj8/localization/resourcebundle/ResourceBundle_NotExactLocale.java
        link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_NotExactLocale.java[role=include]
        Saída no console
        pt_BR
        it

        Note that the default Locale is pt_BR. So it was used when requesting a resource bundle for zh_CN, as there is no bundle for this Locale.

        On the other hand, when requesting a resource bundle for Locale it_CH, he found the closest one, which would be Locale it, but without a specific country.

  13. More specific files inherit keys and values from more generic files if they do not have them.

    src/org/j6toj8/localization/resourcebundle/ResourceBundle_Inheritance.java
    link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_Inheritance.java[role=include]
    ../../../resources/Text.properties
    link:../../../resources/Text.properties[role=include]
    ../../../resources/Text_pt.properties
    link:../../../resources/Text_pt.properties[role=include]
    ../../../resources/Text_pt_BR.properties
    link:../../../resources/Text_pt_BR.properties[role=include]
    console output
    Locale: pt_BR
    caneta
    papel
    keyboard

    Note that in this example a resource bundle with the exact pt_BR Locale was found. However, not all keys were found in this file:

    • pen was found in the Text_pt_BR.properties file

    • paper was found in the Text_pt.properties file

    • keyboard was found in the Text.properties file

References