List all pages on this site [Alt+M] For all roles... another page on this site Software Internationalization and Localization:   List all pages on this site 
About this site 
Go to first topic
Go to Bottom of this page
 

Reload this page Java Internationalization

Search

Next: Internationalization  Next Topic

  Email me!  How I may help  Your Shopping Cart

Set screen Localizing Java with Resource Packages

A 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.

Set screen Java Key-Value Pairs

The simplest approach for translators is to localized text (messages) stored in external resource bundle files containing key/value pairs.


    # Sample key-value pairs:
    search = Rechercher
    searching = Chercher
    search_menu = Rechercher...
    search_next = Rechercher \
    suivant
    search_stopped = Chercher a arrêté

    pattern = There {0} on {1}.
    noFiles = are no files
    oneFile = is one file
    multipleFiles = are {2} files

 

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.

Idea 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.

 

Set screen Java Replacement Patterns

Below is an alternative way of explaining the example presented by webpage article 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.

choiceForm object
numFiles &
fileLimits
fileStrings
[key]
en_US
value
0 noFiles are no files
1 oneFile is one file
2 or more multipleFiles are {2} files
double[] fileLimits = {0,1,2};
String [] fileStrings = {
    bundle.getString("noFiles"),
    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.
When only one record is processed, numFiles would contain 1 (one), which triggers the retrieval of key oneFile.
When two or more records are processed, numFiles would contain 2 or more, which triggers the retrieval of key multipleFiles.

Reminder 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[0] = new Integer(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()};
messageForm.setFormats(formats);

Format array position 0 1 2
Marker {0} {1} {2}
format Object 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}.

CodeNotes


Go to Top of this page.

    Set screen Java External Resource Bundle Files

    Resource Bundle files are automatically suffixed with the .properties file extension.

    The ResourceBundle class looks for files based on a hierarchy. For msg_file_prefix class1 and locale en_US_UNIX:

    1. class1_en_US_UNIX.properties
    2. class1_en_US.properties
    3. class1_en.properties
    4. class1.properties (the default)
    5. the ResourceBundle class overrriden in the source code.

    Only add a key if it has a unique translation. Only national variations need be in files named with a country's id.

    If a key cannot be found in any resourcebundle, a MissingResourceException is thrown by the compiler or the JVM at run-time.


Go to Top of this page.

    Set screen XML Resource Packages

    In 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

    webpage article XML in localisation: A practical analysis IBM Aug 2004


Go to Top of this page.

Next: Internationalization  Next Topic

How I may help

Send a message with your email client program


Your rating of this page:
Low High





Your first name:

Your family name:

Your location (city, country):

Your Email address:



  Top of Page Go to top of page

Thank you!