Java Reference
In-Depth Information
Solution
A resource bundle is simply a collection of names and values. You can write a
java.util.ResourceBundle subclass, but it is easier to create textual Properties files (see
Storing Strings in Properties and Preferences ) that you then load with Re-
sourceBundle.getBundle( ) . The files can be created using any text editor. Leaving it in a
text file format also allows user customization; a user whose language is not provided for, or
who wishes to change the wording somewhat due to local variations in dialect, will have no
trouble editing the file.
Note that the resource bundle text file should not have the same name as any of your Java
classes. The reason is that the ResourceBundle constructs a class dynamically with the same
name as the resource files. You can confirm this by running java -verbose on any of the
programs that use the I18N class from this chapter.
Discussion
Here is a sample properties file for a simple browser (see the MenuIntl program in Program:
MenuIntl ) :
# Default Menu properties
# The File Menu
file.label=File Menu
file.new.label=New File
file.new.key=N
Creating the default properties file is usually not a problem, but creating properties files for
other languages might be. Unless you are a large multinational corporation, you will prob-
ably not have the resources (pardon the pun) to create resource files in-house. If you are ship-
ping commercial software, you need to identify your target markets and understand which of
these are most sensitive to wanting menus and the like in their own languages. Then, hire a
professional translation service that has expertise in the required languages to prepare the
files. Test them well before you ship, as you would any other part of your software.
If you need special characters, multiline text, or other complex entry, remember that a Re-
sourceBundle is also a Properties file.
As an alternative approach, the next recipe describes a program that automates some of the
work of isolating strings, creating resource files, and translating them to other languages.
Search WWH ::




Custom Search