Recipe 15.6 Creating a Resource Bundle


Problem

You need to create a resource bundle for use by I18N.

Solution

A resource bundle is just a collection of names and values. You can write a java.util.ResourceBundle subclass, but it is easier to create textual Properties files (see Recipe 7.7) that you then load with ResourceBundle.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 Recipe 15.11):

# 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 probably not have the resources (pardon the pun) to create resource files in-house. If you are shipping 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 ResourceBundle is also a Properties file.

As an alternate 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.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net