Localizing Java with Resource PackagesA key is obtained by code specifying the message file's prefix.
private ResourceBundle msgs = ResourceBundle.getBundle( msg_file_prefix, locale );
... System.out.println( msgs.getString("searching") ); The project file for an application that has been internationalized contains extra files. It may include different versions of graphics files -- one for each locale. For better performance the msgs can be instantiated from the Java ListResourceBundle class or be be subclassed by a new ResourceBundle type that implements keys as integer offsets into arrays of objects rather than Strings, such as the class provided by the Inprise, Inc. product JBuilder IDE -- borland.jbcl.util.ArrayResourceBundle. These would use a getObject method rather than the getString method. Java Key-Value PairsThe simplest approach for translators is to localized text (messages) stored in external resource bundle files containing key/value pairs.
The separator between key and value can be the equals sign (=) character or the colon (:) character. Keys are case sensitive. Lines starting with an exclamation (!) or a pound (#) character are ignored as comments. A backslash ( \ ) indicates line continuation of a value that span multiple lines. To make keys easier to find keys in the file, arrange them in alphabetical order. But name them so that different forms for the same word are together. Examples: Numbers with curly braces, such as {0}, {1}, etc. in a key's translated value marks where the program dynamically applies other key-value pairs and program variables at run-time.
Java Replacement PatternsBelow is an alternative way of explaining the example presented by Sun Tutorial on the coding used to return a value for key "pattern" after replacing markers {0} and {1}. Marker {0} within a sentence such as "There {0} on {1}" is replaced by defining a ChoiceFormat object that encapsulates the retrieval of additional key values. That processing is based on how many records the application processed, as stored in the numFiles variable. The static value in the fileLimits array is used to compare against the numFiles variable.
double[] fileLimits = {0,1,2};
String [] fileStrings = {
bundle.getString("oneFile"), bundle.getString("multipleFiles") ChoiceFormat choiceForm = new ChoiceFormat( fileLimits, fileStrings );
When no records are processed, numFiles would contain 0 (zero), which
triggers the retrieval of key noFiles.
The value of a key replacing a marker can itself have a marker, such as {2} in the value for the multipleFiles key. Multi-stage replacements are allowed even though they may blow our mind. Marker {2} is replaced with method NumberFormat.getInstance(). This yields a number formatted to the current locale. Methods of the MessageFormat class are used to assemble (format) the substitution of markers in the pattern key. To instantiate a MessageFormat object for a locale:
MessageFormat messageForm = new MessageFormat("");
messageForm.setLocale(currentLocale); String pattern = bundle.getString("pattern"); messageForm.applyPattern(pattern); Marker {1}, does not vary, so it is defined in the code to instantiate the messageForm array used to assemble the result:
Object[] messageArguments = {null, "XDisk", null};
for (int numFiles = 0; numFiles < 4; numFiles++) {
messageArguments[2] = new Integer(numFiles); String result = messageForm.format(messageArguments); System.out.println(result); The messageForm array is populated by methods of the MessageFormat class, which applies classes designated in the format array. object, string returned for the pattern key is first applied to a then formatted with an object specified for each marker position:
Format[] formats = {choiceForm, null, NumberFormat.getInstance()};
The value returned from choiceForm subsitutes for {0}. null means that a processing object is not applied to substitute a marker, The value resulting from NumberFormat.getInstance() is substituted for {2}. |
XML Resource PackagesIn April, 2006 the Internationalization Tag Set Working Group published an updated Working Draft of the Internationalization Tag Set (ITS). Organized by data categories, this set of elements and attributes supports the internationalization and localization of schemas and documents. ITS implementations provides for DTDs, XML Schema and Relax NG, as well as existing vocabularies like XHTML, DocBook, and OpenDocument. XML Internationalization and Localization () by Yves Savourel |
Java Internationalization ResourcesMore on Internationalization and Localization Jeff Tsay's $149 text2gui JAR library makes it easier for Java programmers to internationlize applications by allowing them to specify GUI descriptions instead of coding localization methods. The library parses tex2gui descriptions at runtime, so changes to a GUI do not require recompilation. Use of the library allows specialized syntax, such as Subkeys and chaining of resource bundles that inherit from other resource bundles. The library can also create component hierarchies with a single line of Java code. Because the library allows more properties to be specified in resource bundles, different fonts and layouts can be automatically displayed for each target locale. List of Java Internationalization web pages Java Tutorial: Internationalization Trail by Dale Green describes use of the ResourceBundle class .getBundle method to read properties files for each locale. Sun's Guide to Internationlizing Java Barracuda's open source MVC presentation framework component XMLC Tutorial Javaworld article: Internationalize your software 0-596-00019-7 Samples of the O'Reilly book Java Internationalization by Andy Deitsch and David Czarnecki (March 2001)
|
Next: Internationalization |
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page Thank you! |