Resource Bundle Caveats
This section details information you need to consider when using resource
bundles in your application.
What to Localize in a Resource Bundle
Certain strings should not be localized and placed into a resource bundle
in any application. Consider the strings that specify the region that a
component is placed in for the BorderLayout class in the
java.awt package. Java allows you to specify five regions on
the border layout, namely, "North," "South," "East," "West," and "Center."
Strings like these are used as program identifiers and must not be
localized. Localizing would create programs that either do not work at all
or behave incorrectly. This situation also holds true for keys used to
retrieve a specific element from a resource bundle; you would not localize
keys used to retrieve values from a PropertyResourceBundle.
Character Encoding Issues for Resource Bundles
Java source files are most commonly written and saved as ASCII text files
with escape sequences for non-ASCII characters. File editors used to create
your source files may also allow you to use a regional character encoding.
The javac compiler supports the –encoding
directive, which allows you to specify the regional encoding of the file
you're trying to compile. Appendix B, Character Encodings Supported by
Java, contains a complete listing of all supported languages and
converters that can be specified for your specific regional encoding.
Chapter 6, Character Sets and Unicode, explains encodings in more
detail.
You need to be aware of the character encoding when you use a
ListResourceBundle. In a ListResourceBundle, your
resources are stored in a class file, which is created by compiling a
.java source file. Say we compile a source file using a Polish
character encoding. We'd compile our resource bundle as follows:
C:\> javac –encoding iso8859_ 2 ProgramResources.java
Using the PropertyResourceBundle class, we must also worry
about the character encoding of the individual properties files. Properties
files must use an ISO 8859-1 encoding or contain Unicode escape sequences
to represent Unicode characters. Unicode escape sequences are specified as
\uxxxx, for which xxxx represents the hexadecimal
value of the specific character.
Java provides a utility called native2ascii, which takes a
file written in a given character encoding and converts non-ASCII
characters to the appropriate Unicode escape sequences.
native2ascii also takes an encoding option like
javac,through which you can specify the character encoding of
the file to be converted. If this option is not specified, it uses the
default character encoding of the platform on which the program is running.
The command-line option –reverse allows you to convert a file
containing Unicode escape sequences to a native character encoding. You
must specify an input file and you may also specify an optional output file
where the converted file will be written. If the output file is not
specified, standard output is used:
C:\> native2ascii –encoding SJIS ProgramResources_ ja. java
ProgramResources_ ja. java. converted
Property Resource Bundle Caveats
We should point out a few things to watch for now that you're aware of how
to use PropertyResourceBundles.
Using the PropertyResourceBundle class is very convenient when
all you have to localize are textual elements. If you have other datatypes
that you'd like to localize in a resource bundle, the
ListResourceBundle class allows you that flexibility. In the
next subsection of this chapter, we'll discuss a problem that could arise
when using both PropertyResourceBundles and
ListResourceBundles.
Unlike the ListResourceBundle class, which must be subclassed,
the PropertyResourceBundle class is a concrete subclass of
ResourceBundle. Therefore, to add more resources using a
PropertyResourceBundle, simply create the appropriate
properties files. You do not have to compile these resource bundles, as you
must with a ListResourceBundle.
Properties files that are found by one of the getBundle
methods must end with a .properties extension. If one
of your properties files is missing this extension, it will not be found;
this situation causes problems when performing a resource bundle lookup.
The .properties extension is added automatically for
you during the lookup process, so you do not need to add one to the name
passed to the getBundlemethod.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.