Recipe 15.7 Extracting Strings from Your Code


Problem

You have existing code that contains hardcoded strings.

Solution

Use JILT, Eclipse, or your favorite IDE.

Discussion

Many tools extract Strings into resource bundles. This process is also known as externalization . Nothing to do with jilting your lover, JILT is Sun's Java Internationalization and Localization Toolkit, Version 2.0.[1] JILTing your code means processing it with JILT, which facilitates I18N and L10N'ing the Java classes. JILT has four GUI-based tools, which can be used independently, started from a GUI frontend called JILKIT. Figure 15-1 shows JILT in action.

[1] Ironically, though, Sun appears to be jilting JILT; it's nearing the end of its lifecycle, so you may want to look at the externalization support offered by your IDE instead.

Figure 15-1. JILT in action
figs/jcb2_1501.gif


The tools are listed in Table 15-2.

Table 15-2. JILT programs

Tool

Function

I18N Verifier

Tests program for international use and suggests improvements.

Message Tool

Finds and allows you to edit hardcoded or inconsistent messages.

Translator

Translates messages in a resource bundle file into a given locale/language.

Resource Tool

Merges multiple resource files into a new resource bundle. Can also find differences between resource files.


It's worth noting that the time it takes to learn these tools may overshadow their benefits on small projects, but on large projects they will likely prove worthwhile.

Version 2 of the Translator ships with a Chinese dictionary, but you can provide your own dictionaries as well.

The Java Internationalization and Localization Toolkit is nearing its end-of-life support from Sun but can, as of this writing, still be downloaded for free from Sun's Java page, http://java.sun.com/products/jilkit/.

Many IDEs provide an externalization mechanism. Under Eclipse, for example, select a Java source file, then select Externalize Strings from the Source menu. Eclipse generates a Properties file and a class with static methods to retrieve the values of the Strings and replace the strings in your code with calls to those methods. Other IDEs provide similar mechanisms.



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

Similar book on Amazon

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