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.
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.
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).
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?"} } ; } |
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 |
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.
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.
Before getNumericCharacterReferences method | After getNumericCharacterReferences method |
Hello. | Hello. |
你好。 | 你好。 |
// Format Number UnicodeFormatter.getNumericCharacterReferences( i18n.formatNumber( new Integer( 123456 ) ) ) ; UnicodeFormatter.getNumericCharacterReferences( i18n.formatNumber( new Double( 345987.246 ) ) ) ;
// Format Currency
// Format Percent
// Format Date
// Format Time
// Format Date and Time |
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