IBM: DeveloperWorks China website: Java: All articles
Internationalization and localization of Java programs
Shao Rong (Youcenter@yahoo.com)
Software Engineer, CA Co., Ltd. R & D Center May 2001
This article trys to use a simple example to demonstrate how easy it is to comply with the international standard, and then how simple it is for localization.
When a software product needs to apply globally, it is necessary to consider the usage of different territories and locations. The simplest requirement is to display the information on the UI to display, of course an excellent globalization. The requirements of software products about internationalization and localization are far more than this, and this paper only involves an international and localized consideration of a small part of the interface display language. Java Language Kets provide internal support for different countries and different language characters, due to congenital reasons, Java supports more than C / C than C / C due to congenital reasons.
Before you start specific introduction, you need to introduce several terms:
I18N: It is Internationalization, internationalization, and 18 characters between the first letters "I" and the end of the letter "N", referred to as I18n. INTERNATIONALIZATION means that the application can adapt to changes in different languages and regions without systematic Design measures taken by changes. L60N: Localization, localization, 10 letters "L" and end letters "N", referred to as L10n. localization referred to as Localization to enable application software to join local specialty in a particular locale or region The process of subjected to the post-translational text. Locale: Simply refers to a sign of a special combination of language and region. Our example, display a Dialog window with a button and a menu, making the application to display the interface of English or Chinese text without changing the program. Note: Because it is used as a demonstration, our menu bar also joins the dialog as a component, as if it is a Button, not the menu bar in the usual sense. Usually our program is like this:
/ **
* File Name: I18nDemo.java
* Author ID: jack, at http://bbs.Whnet.edu.cn, Java Discuss Board.
* Description: shows a dialog with a "ok" Button and "file" / "exit" menu.
* /
Import javax.swing. *;
Import java.awt. *;
Import java.util. *;
Public class i18ndemo {
Public static void main (string args []) throws exception {
JDIALOG DIALOG = New JDIALOG ();
JButton BTOK = New jbutton ();
JMenuBar MenuBar = new jmenubar ();
JMENU MENUFILE = New Jmenu ();
JMenuItem MenueXit = new jmenuitem ();
BTok.setText ("OK");
Menufile.Settext ("file");
Menuexit.Settext ("exit");
Dialog.Settitle ("I18n Demo");
MenuBar.Add (Menufile);
Menufile.Add (Menuexit);
Dialog.getContentPane (). setLayout (new flowLayout ());
Dialog.getContentPane (). add (btok); Dialog.getContentPane (). add (menubar);
Dialog.setsize (200,100);
Dialog.SetModal (TRUE);
Dialog.show ();
System.exit (0);
}
}
We reach the requirements of I18n by modifying procedures: Separate those resources that have a relationship of those and interfaces, separately below the resource file. The resource file inside. The resource file in Java is called ResourceBundle, which is divided into two kinds, one is ListResourceBundle, the other is PropertyResourceBundle We use ListResourceBundle here, please refer to the recommended reading after the text. When a program requires some locale-related resources, such as string resources, it can be loaded from the resource bundle. It should be noted that in order to correctly display the Chinese characters, the components in the program need to select the specified font, such as using "MS Song" when displaying Chinese, otherwise there is no meaning when there is Chinese characters. Please see the modified program and resource files.
/ **
* File Name: I18nDemo.java
* Author: jack, at http://bbs.Whnet.edu.cn, Java Discuss Board.
* Email: Greatjava@sina.com
* Description: shows a dialog with a "ok" Button and "file" / "exit" menu.
* This Program Made Some Modifications to Fit The Requestion of i18n
* /
Import javax.swing. *;
Import java.awt. *;
Import java.util. *;
Public class i18ndemo {
Public static void main (string args []) throws exception {
Resourcebundle res;
IF (args.length <1) {
// Use Current Locale At Machine TO GET The Resource Bundle.
Res = ResourceBundle.getBundle ("res");
} else {
Res = ResourceBundle.getBundle ("res", new local (args [0], args [1]));
}
JDIALOG DIALOG = New JDIALOG ();
JButton BTOK = New jbutton ();
JMenuBar MenuBar = new jmenubar ();
JMENU MENUFILE = New Jmenu ();
JMenuItem MenueXit = new jmenuitem ();
Font font = new font (Res. maxString ("fontname"), Font.Plain, 12);
Btok.Settext (Res. GetString ("OkText");
Menufile.Settext (Res. GetString ("FileMenutext"));
Menuexit.Settext (Res. GetString ("FileExit GeneNutext"));
Dialog.SetTitle (Res. GetString ("Dialogtitle"); BTOK.SETFONT (FONT);
Menufile.SetFont (font);
Menuexit.SetFont (font);
MenuBar.Add (Menufile);
Menufile.Add (Menuexit);
Dialog.getContentPane (). setLayout (new flowLayout ());
Dialog.getContentPane (). Add (btok);
Dialog.getContentPane (). add (menubar);
Dialog.setsize (200,100);
Dialog.SetModal (TRUE);
Dialog.show ();
System.exit (0);
}
}
/ **
* File name: res_en_us.java
* Author: jack, at http://bbs.Whnet.edu.cn, Java Discuss Board.
* Email: Greatjava@sina.com
* Description: Resource File for i18ndemo.java
* /
Import java.util. *;
PUBLIC CLASS RES_EN_US EXTENDS JAVA.UTIL.LISTRESOURCEBUNDLE {
Static firm Object [] [] contents = new string [] [] {
{"Oktext", "ok"},
{"Fontname", "dialoginput"},
{"FileMenutext", "file"},
{"FileExit GeneNutext", "exit"},
{"Dialogtitle", "Demo Dialog"}};
Public object [] [] getContents () {
Return Contents;
}
}
Before running, we will make a resource file containing Chinese resources.
/ **
* File name: res_zh_cn.java
* Author: jack, at http://bbs.Whnet.edu.cn, Java Discuss Board.
* Email: Greatjava@sina.com
* Description: Resource File for i18ndemo.java
* /
Import java.util. *;
Public class res_zh_cn extends java.util.ListResourceBundle {
Static firm Object [] [] contents = new string [] [] {
{"Oktext", "OK"},
{"Fontname", "MS Song"},
{"FileMenutext", "File"},
{"FileExit GeneNutext", "Exit"},
{"Dialogtitle", "Demo dialog"}}
Public object [] [] getContents () {
Return Contents;
}
}
First run to see the results:
// Run next command step by step
Javac * .java
Java I18nDemo
Java i18ndemo zh CN
Java i18ndemo en us
You can see a dialog box that appears in the Chinese interface and an English interface. Take a specific look: Main program I18nDemo.java, more than a resourcebundle object, which contains information about a resource file. The value of the corresponding LOCALE is then obtained when using localized resources. Through the command line, we pass the code on the language and the country, so when generating the ResourceBundle object, you can point to the corresponding resource file. If you don't specify local, Locale used by generating resourceBundle is the default area and language on the current machine. In my machine (English WindowsNT 4), when opening the Richwin Chinese platform, the default Locale is China. When Richwin is turned off, local is US. Resource files res_en_us.java contains English interface resources, file name "En" refers to English language, "US" refers to the country is the United States. Res_zh_cn.java is Chinese resource file, "EN" instruction language is Chinese, "CN" shows that the country is China. A complete comparison table for language and country can be found in the reference resource. The res_xx_xx class is inherited from ListResourceBundle, and the ListResourceBundle subclass must provide an overloaded getContents method and an object array, and the array contains the keys of the resource and the corresponding VALUES. For example, the value of key "oktext" is "OK" and "OK" in two files, respectively. Under normal circumstances, it is generally not necessary to change Locale after the application is installed so that when we publish an application, multiple Resource files are not available in different areas, as long as the used resource files are replaced with corresponding local content The file is OK. For example, in this example, as long as a res. Java resource file, when the content is replaced with res_en_us.java, in the Chinese environment, it is possible to replace it in the Chinese environment. Further discussion of I18N and L10N This article is only the simplest internationalization of interface display languages. In the actual application process, the situation is more complicated, and some factors that need to be considered include: prompt information, labels on the GUI assembly. , Online help, sound, color, graphics, icon, date, currency symbol ..... In addition to these, specific processing in some processes will be different, such as sorting methods, input processing, etc. For more technical content, see the recommended references later. Reference language code comparison table http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt country code control table http://www.chemie.fu-berlin.de/diverse/doc /ISO_3166.html Sun About INTERNATIONLIZATION In JDK instructions http://java.sun.com/j2se/1.3/docs/guide/intl/intltoc.doc.html Sun About the Internationalization Tutorial Http: //java.sun .com / DOCS / BOOKS / TUTORIAL / I18N / INDEX.HTML
About the author Shao Rong, software technical expert. Software engineering and software technology, especially Java technology, in-depth research. At present, he is a software engineer of the CA Limited R & D center while hosing the Java discussion group of BBS white clouds. You can contact him through YOUCENTER@yahoo.com.