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.
link:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_Complete.java[role=include]
link:../../../resources/Text.properties[role=include]
link:../../../resources/Text_pt.properties[role=include]
link:../../../resources/Text_pt_BR.properties[role=include]
link:../../../resources/Text_es.properties[role=include]
link:../../../resources/Text_es_ES.properties[role=include]
-- 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
-
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
→ Localept_BR
-
Text_pt.properties
→ Localept
-
Text_es_ES.properties
→ Localees_ES
-
Text_es.properties
→ Localees
-
-
The
.properties
file can be expressed with 3 different separators:=
(equal),:
(colon) or a blank space.../../../resources/Text.propertieslink:../../../resources/Text.properties[role=include]
The most common is using
=
to separate properties, but all 3 work the same way. -
In
.properties
files, lines that start with#
or!
are comments.../../../resources/Text_pt_BR.propertieslink:../../../resources/Text_pt_BR.properties[role=include]
../../../resources/Text_es_ES.propertieslink:../../../resources/Text_es_ES.properties[role=include]
-
In
.properties
files, only spaces at the end of the line are considered.../../../resources/Text_pt.propertieslink:../../../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}
. -
In
.properties
files, if you end the line with a backslash, you can break the line.../../../resources/Text_es.propertieslink:../../../resources/Text_es.properties[role=include]
In this example, it would be the same as typing on a single line:
keyboard=teclado
. -
In
.properties
files, you can also use Java characters such as\t
and\n
.../../../resources/Text_es_ES.propertieslink:../../../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 wordvaso
was printed on the console with a left tab. -
You can retrieve all resource bundle keys and values programmatically.
src/org/j6toj8/localization/resourcebundle/ResourceBundle_KeysProgrammatically.javalink:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_KeysProgrammatically.java[role=include]
console outputtripod - tripod keyboard - keyboard glass - glass paper - papel phone - phone rubber - rubber pen - caneta sticker - sticker
-
The resource bundle can also be a Java class.
resource/Text_fr_CA.javalink:../../../resources/Text_fr_CA.java[role=include]
src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaBundle.javalink:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaBundle.java[role=include]
console outputverre
-
When using Java classes, one advantage is that you can store values other than
String
.resource/Text_fr_FR.javalink:../../../resources/Text_fr_FR.java[role=include]
-
The file nomenclature is the same for Java classes and
.properties
files, changing only the extension.Note that the
.properties
files are namedText_xx_XX.properties
, and the.java
files are namedText_xx_XX.java
. Programmatically, both are used in the same way. -
If there is a
.properties
file and a Java class for the sameLocale
, the Java class is used.resource/Text_fr_CA.javalink:../../../resources/Text_fr_CA.java[role=include]
resource/Text_fr_CA.propertieslink:../../../resources/Text_fr_CA.properties[role=include]
src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaClassTakesPrecedence.javalink:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_JavaClassTakesPrecedence.java[role=include]
console outputstylo 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 sameLocale
. -
When searching for a resource bundle, Java tries to find a file with the exact
Locale
. If not find, search in the following order:-
A file of the same language, but without the country;
-
A file from the default
Locale
; -
A standard
Locale
file, but without the country; -
A file without
Locale
, which is the default resource bundle; -
Throws
MissingResourceException
if not found.For example, when running the application with the default Locale
en_US
, and requesting a Localept_BR
, the resource bundle search order would be as follows:-
Text_pt_BR
→ Exact Locale -
Text_pt
→ Requested Locale, without country -
Text_en_US
→ Default Locale -
Text_en
→ Default Locale, without country -
Text
→ Resource Default Bundlesrc/org/j6toj8/localization/resourcebundle/ResourceBundle_NotExactLocale.javalink:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_NotExactLocale.java[role=include]
Saída no consolept_BR it
Note that the default
Locale
ispt_BR
. So it was used when requesting a resource bundle forzh_CN
, as there is no bundle for thisLocale
.On the other hand, when requesting a resource bundle for
Locale
it_CH
, he found the closest one, which would beLocale
it
, but without a specific country.
-
-
-
More specific files inherit keys and values from more generic files if they do not have them.
src/org/j6toj8/localization/resourcebundle/ResourceBundle_Inheritance.javalink:../../../src/org/j6toj8/localization/resourcebundle/ResourceBundle_Inheritance.java[role=include]
../../../resources/Text.propertieslink:../../../resources/Text.properties[role=include]
../../../resources/Text_pt.propertieslink:../../../resources/Text_pt.properties[role=include]
../../../resources/Text_pt_BR.propertieslink:../../../resources/Text_pt_BR.properties[role=include]
console outputLocale: 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 theText_pt_BR.properties
file -
paper
was found in theText_pt.properties
file -
keyboard
was found in theText.properties
file
-
-
Using a Resource Bundle
Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 258). Wiley. Kindle Edition.
-
Class ResourceBundle. Java Documentation.
-
About the ResourceBundle Class. Java Documentation.