Quick Start on Internationalization using Java


HTML Demo - http://www.jag-s.org/~wailian/TestI18N/html/TestI18N.html

WML Demo - http://www.jag-s.org/~wailian/TestI18N/wml/TestI18N.wml

Click here to download all the source files and examples.

The I18N.jar file consists of all the relevant classes and methods I've developed for Internationalization using Java. Refer to I18N API documentation for more details.


Steps:

  1. Create the Default Properties File
  2. A properties file is a simple text file. You can create and maintain a properties file with just about any text editor.

    The name of this file begins with the base name of your ResourceBundle and ends with the .properties suffix.

    MessagesBundle.properties file:
    # This is the MessagesBundle.properties file
    greetings = Hello.
    farewell = Goodbye.
    inquiry = How are you?

    Note that in the preceding file the comment lines begin with a pound sign (#). The other lines contain key-value pairs. The key is on the left side of the equal sign and the value is on the right.

  3. Create Additional Properties Files as Needed (To support additional Locales)
  4. MessagesBundle_zh_CN.properties file [Language code for Chinese (zh) and Country code for China (CN)]:
    # This is the MessagesBundle_zh_CN.properties file
    greetings = \u4F60\u597D\u3002
    farewell = \u518D\u89C1\u3002
    inquiry = \u4F60\u597D\u5417?

    Notice that the name of this file, like that of the default file, begins with the base name MessagesBundle and ends with the .properties suffix. However, since this file is intended for a specific Locale, the base name is followed by the language code and country code.

    Note:
    To indicate Unicode characters that cannot be represented in ASCII
    => Use the \uXXXX escape sequence where XXXX is the hex representation (i.e. 2 bytes / 16-bits).

  5. Create Class Files serving the same purpose as the Properties Files (Optional)
  6. ListResourceBundle objects contain an array of key-value pairs. You specify the key, which must be a String, when you want to retrieve the value from the ResourceBundle. The value is the locale-specific object.

    MessagesBundle_zh_CN.java file [Language code for Chinese (zh) and Country code for China (CN)]:
    package com.mot.TestI18N.ResourceBundle ;

    import java.util.ListResourceBundle ;

    /**
    * Class for zh CN ResourceBundle (MessagesBundle_zh_CN.java).
    *
    * @author Chue Wai Lian
    * @version
    *
    * 1.0.0 08 Mar 2001
    * 1st release.
    */
    public class MessagesBundle_zh_CN extends ListResourceBundle
    {
        public Object[][] getContents()
        {
            return( contents ) ;
        }

        static final Object[][] contents =
        {
            {"greetings", "\u4F60\u597D\u3002"},
            {"farewell", "\u518D\u89C1\u3002"},
            {"inquiry", "\u4F60\u597D\u5417?"}
        } ;
    }

  7. CLASSPATH
  8. The MessagesBundle ResourceBundle (backed up by a class and/or by a properties file) must be accessible via the CLASSPATH. The simplest and easiest way is to bundle all the ResourceBundle into a single archive jar file and append the jar file to the CLASSPATH.

    ResourceBundle backed up by only Properties Files:
    jar -cvf TestI18NResourceBundle.jar com\mot\TestI18N\ResourceBundle\*.properties

    ResourceBundle backed up by only Class Files:
    javac com\mot\TestI18N\ResourceBundle\*.java
    jar -cvf TestI18NResourceBundle.jar com\mot\TestI18N\ResourceBundle\*.class

    ResourceBundle backed up by both Class Files and Properties Files:
    javac com\mot\TestI18N\ResourceBundle\*.java
    jar -cvf TestI18NResourceBundle.jar com\mot\TestI18N\ResourceBundle\*.properties com\mot\TestI18N\ResourceBundle\*.class

  9. Create an I18N object
  10. The I18N Class Constructor takes in 3 parameters: language, country, fully qualified ResourceBundle base class.

    String language = "en" ;
    String country = "US" ;
    String strResourceBundlePath = "com.mot.TestI18N.ResourceBundle.MessagesBundle" ;

    I18N i18n = new I18N( language, country, strResourceBundlePath ) ;

    It will set the current Locale and create the ResourceBundle via the getBundle method which first looks for a class file that matches the base name and the Locale. If it can't find a class file, it then checks for properties files.

    When the getBundle method locates the correct class file, it returns a ListResourceBundle object containing the key-value pairs from the class file.

    When the getBundle method locates the correct properties file, it returns a PropertyResourceBundle object containing the key-value pairs from the properties file.

  11. Fetch the Localized Text
  12. To retrieve the translated value from the ResourceBundle, invoke the getString method as follows:

    i18n.getResourceBundle().getString( "greetings" ) ;
    i18n.getResourceBundle().getString( "farewell" ) ;
    i18n.getResourceBundle().getString( "inquiry" ) ;

    The String returned by getString corresponds to the key specified. The String is in the proper language, provided that a class or properties file exists for the specified Locale.

  13. Format Numbers, Currencies, Percentages, Dates, Times, Dates and Times
  14. The I18N Class provides various methods for the above-mentioned in a locale-sensitive manner. Refer to I18N API documentation for more details.

  15. UnicodeFormatter Class
  16. The above-mentioned class provides static methods for formatting Unicode characters. In particular, the getNumericCharacterReferences method converts String of Unicode characters to Numeric Character References suitable for use in markup languages such as HTML or WML.

    Before getNumericCharacterReferences method After getNumericCharacterReferences method
    Hello. Hello.
    你好。 你好。

  17. Examples
  18. // Format Number
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatNumber( new Integer( 123456 ) ) ) ;
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatNumber( new Double( 345987.246 ) ) ) ;

    // Format Currency
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatCurrency( new Double( 9876543.21 ) ) ) ;

    // Format Percent
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatPercent( new Double( 0.75 ) ) ) ;

    // Format Date
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatDate( new Date(), DateFormat.DEFAULT ) ) ;

    // Format Time
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatTime( new Date(), DateFormat.DEFAULT ) ) ;

    // Format Date and Time
    UnicodeFormatter.getNumericCharacterReferences( i18n.formatDateTime( new Date(), DateFormat.DEFAULT, DateFormat.DEFAULT ) ) ;


Copyright© 2002 by Chue Wai Lian

Last updated on Sunday, 06 January 2002

Please send all mails to wailian@singnet.com.sg or wailian@pmail.ntu.edu.sg