Internationalization in Java



General Information

Introduction

Locales

Behavior specific to a population group is controled by the notion of a local. Java adopted this approach from POSIX. The approach has many limitations and problems.

Locale names in Java consist of three parts: language code, country code, and variant code. The country and variant code are optional.

  1. Language code. Two letter code from ISO 639
    Language CodeDescription
    deGerman
    enEnglish
    frFrench
    jaJapanese
    koKorean
    zhChinese

  2. Country code. Two letter codes from ISO 3166-1
    Country CodeDescription
    CNChina
    DEGermany
    FRFrance
    INIndia
    USUnited States

  3. variant code. This part has no predefined structure. Possible variant code examples include: unix, dos, euro and 8859-1

Examples

en_GB
en_US
en_US_unix
en_US_8859-1
de_DE_EURO

Problems: the standard is continually modified --- Bosnia and Bosnian added.

aLocale = new Locale("fr", "CA");
bLocale = new Locale("en", "US");
cLocale = new Locale("en", "GB");
xLocale = new Locale("de", "DE", "UNIX");
yLocale = new Locale("de", "DE", "WINDOWS");
enLocale = new Locale("en", "");
j1Locale = Locale.JAPAN;
j2Locale = new Locale("ja", "JP");
j3Locale = Locale.JAPANESE;
j4Locale = new Locale("ja", "");

Predefined Locales

Predefined final static constants from the java.util.Local (version 1.4.1 and version 1.5)
javap java.util.Locale | grep "public static final" | sort
CANADA
CANADA_FRENCH
CHINA
CHINESE
ENGLISH
FRANCE
FRENCH
GERMAN
GERMANY
ITALIAN
ITALY
JAPAN
JAPANESE
KOREA
KOREAN
PRC
SIMPLIFIED_CHINESE
TAIWAN
TRADITIONAL_CHINESE
UK
US
CANADA       : English (Canada) : en_CA
CANADA_FRENCH: French (Canada) : fr_CA
CHINA        : Chinese (China) : zh_CN
CHINESE      : Chinese : zh
ENGLISH      : English : en
FRANCE       : French (France) : fr_FR
FRENCH       : French : fr
GERMAN       : German : de
GERMANY      : German (Germany) : de_DE
ITALIAN      : Italian : it
ITALY        : Italian (Italy) : it_IT
JAPAN        : Japanese (Japan) : ja_JP
JAPANESE     : Japanese : ja
KOREA        : Korean (South Korea) : ko_KR
KOREAN       : Korean : ko
RPC          : Chinese (China) : zh_CN
SIMPLIFIED_CHINESE: Chinese (China) : zh_CN
TAIWAN       : Chinese (Taiwan) : zh_TW
TRADITIONAL_CHINESE: Chinese (Taiwan) : zh_TW
UK           : English (United Kingdom) : en_GB
US           : English (United States) : en_US

Character Encodings

Properties File and Resource Bundle

API Documentation

Motivation

EnglishDutchFrenchGerman
apply wekvenster validation anwenden
cancel annuleren annulation abbrechen
copy kopiërencopie kopieren
delete knippen suppression löschen
edit bewerken amener editiern
exit
file Bestand fichier Datei
help Help aide Hilfe
new nieuw nouveau neu
ok ok
open openen ouvrir öffen
paste coller enfügen
save opslaan sauvegarde sichern
save asopslaan als sauvegarde ensichern als
view Beeld VisualisationAnzeige

Examples

Property Resource Bundles

Text Files

Label/value pairs:
! lines beginning with ! are comments
# also, lines beginning with # are comments
# pairs seperated by '=' or ':'
# case of labels matters: label != LaBeL
label=value
label:value
# multiple lines can be continuate with a backslash (\)
An example using string tokenizer.
classes=Betriebssysteme, Programmierung, Optimierung,\
   Computationale Logik, Algorithmen
classes=Operating Systems, Programming, Optimization,\
Computational Logic, Algorithms

final ResourceBundle b = ResourceBundle.getBundle ("Text");
final StringTokenizer classes = new StringTokenizer (b.getString ("classes"), ",");
final String c = classes.nextToken().trim();

File Extension

.properties

Packages

package com.acme;

getBundle ("com.acme.Labels");

List Resource Bundle

Not just for strings.

Search

The search for the appropriate property resource bundle file or list resource bundle file is the same. It is based on the current local. First the desired locale, from the most specific to least specific, then the default locale, from most specific to least specific, and then finally the base name.

For example,

         ButtonLabel_fr_CA_UNIX
         ButtonLabel_fr_CA
         ButtonLabel_fr
         ButtonLabel_en_US
         ButtonLabel_en
         ButtonLabel

Class files hide properties files at the same level.

Applets

No Problem! Just put your .class and .properites in the jar file.

Formatting

Numbers

Issues:

The Java NumberFormat class java.text.NumberFormat and . (See table of special pattern characters.)

Number.java -- formating numbers

  7
  37
  537
7
37
537
7
37
537
700%
3,700%
53,700%
¤7.00
¤37.00
¤537.00
      
Example use of DecimalFormat, Decimal.java, output.

Parsing

final String parseMe = "1.234,56";
final NumberFormat numParser = NumberFormat.getInstance(Locale.GERMANY);
try {
  Number result = numParser.parse(parseMe);
  System.out.println(result);
} catch(ParseException e) {
  // handle error condition
}
  
The output is 1234.56
  // Malayalam digits 123456
final String parseMe = "\u0f21\u0f22\u0f23\u0f24\u0f25\u0f26";
final NumberFormat numParser = NumberFormat.getInstance();  // No locale!
try {
  Number result = numParser.parse(parseMe);
  System.out.println(result);
} catch(ParseException e) {
  // handle error condition
}
  
The output is 123456

ArabicDigits.java from Deitsch and Czarnecki. Arabic digits

Java SDK 1.4 does not support ideographic number formatting; however, IBM does. You can download a class called RuleBasedNumberFormat from the ICU4J WWW site at oss.software.ibm.com/icu4j/

Dates and Time

CalendarManipulation.java from Deitsch and Czarnecki.

> java CalendarManipulation
Wednesday, April 2, 2003 4:16:29 PM
Thursday, April 10, 2003 4:16:29 PM
Thursday, April 10, 2003 12:16:29 PM
Friday, April 11, 2003 12:16:29 AM
Friday, April 11, 2003 12:16:29 PM

> java -Duser.language=de CalendarManipulation
Mittwoch, 2. April 2003 16:16:59
Donnerstag, 10. April 2003 16:16:59
Donnerstag, 10. April 2003 12:16:59
Freitag, 11. April 2003 00:16:59
Freitag, 11. April 2003 12:16:59

> java -Duser.language=fr CalendarManipulation
mercredi 2 avril 2003 16:17:16
jeudi 10 avril 2003 16:17:16
jeudi 10 avril 2003 12:17:16
vendredi 11 avril 2003 00:17:16
vendredi 11 avril 2003 12:17:16

> java -Duser.language=es CalendarManipulation
miércoles 2 de abril de 2003 16:17:52
jueves 10 de abril de 2003 16:17:52
jueves 10 de abril de 2003 12:17:52
viernes 11 de abril de 2003 0:17:52
viernes 11 de abril de 2003 12:17:52

> java -Duser.language=zh CalendarManipulation
2003?4?2? 16:19:26
2003?4?10? 16:19:26
2003?4?10? 12:19:26
2003?4?11? 0:19:26
2003?4?11? 12:19:26

> java -Duser.language=ko CalendarManipulation
2003? 4? 2? ??? ?? 4:19:41
2003? 4? 10? ??? ?? 4:19:41
2003? 4? 10? ??? ?? 12:19:41
2003? 4? 11? ??? ?? 12:19:41
2003? 4? 11? ??? ?? 12:19:41

> java -Duser.language=uk CalendarManipulation
??????, 2, ?????? 2003 16:22:11
??????, 10, ?????? 2003 16:22:11
??????, 10, ?????? 2003 12:22:11
?'??????, 11, ?????? 2003 0:22:11
?'??????, 11, ?????? 2003 12:22:11

> java -Duser.language=th CalendarManipulation
????????? 2 ?????? ?.?. 2003, 16:22:19
?????????????? 10 ?????? ?.?. 2003, 16:22:19
?????????????? 10 ?????? ?.?. 2003, 12:22:19
??????????? 11 ?????? ?.?. 2003, 0:22:19
??????????? 11 ?????? ?.?. 2003, 12:22:19

My calendar applet

See date formatting (but this uses the Java class, see below).

MessageFormat

The Java class takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.

MessageFormat and resource bundles

MessageFormat.parse()

ChoiceFormat

The Java class allows you to attach a format to a range of numbers. It is generally used in a for handling plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item.

Example use of ChoiceFormat, Choice.java, output.

Text Processing

German Spelling Reform, German Spelling Reform.

Collation

Issues

Latin1      Es
------    ------
agüero    agüero
argüir    argüir
caro      caro
carro     carro
carta     carta
cellisca  célula
chaleco   cellisca
churo     cuco
cuchara   cuchara
cuco      cuna
cuna      cundir      
cundir    cuña
cuña      chaleco
célula    churo
día       día
güero     güero
güisquil  güisquil
llave     lobo
lobo      llave
maíz      maíz
pero      pero
perro     perro

Collator

Sorting with

Swedish German
banan äpple
orange banan
päron orange
äpple päron

Lexicographic Multilevel comparison
Co-Op of America Coates
CoSelCo Cohen
Coates Cooper
Cohen Cooperatives United
Cooper Co-Op of America
Cooperatives United CoSelCo
Cosell Cosell
MacIntosh Machinists Union
MacPherson Macintosh
Machinists Union MacIntosh
Macintosh Macpherson
Macpherson MacPherson
Van Damme Van Damme
Vandenberg Vandenberg
van den Hul van den Hul
van der Waal van der Waal

Localize This! from Java World

Searching

Search.java from Dietch and Cazrnecki.

Search.java

Regular Expressions

The Java regular expression package does not seem to parallel the class

Text Boundaries

Input Method Editors

NB. Extension classes are classes which extend the Java platform. Every .jar file in the extension directory, jre/lib/ext, is assumed to be an extension and is loaded using the Java Extension Framework. Only jar files can be used; not loose class files. There is no (classpath like) option provided for changing the location of the extension directory.

SWING

Solaris
jar -tf /software/java/jdk1.2.2/jre/lib/rt.jar | grep .prop
com/sun/java/swing/plaf/motif/resources/motif.properties
com/sun/java/swing/plaf/motif/resources/motif_ja.properties
com/sun/java/swing/plaf/windows/resources/windows.properties
com/sun/java/swing/plaf/windows/resources/windows_ja.properties
java/awt/resources/awt.properties
java/awt/resources/awt_ja.properties
javax/swing/plaf/basic/resources/basic.properties
javax/swing/plaf/basic/resources/basic_ja.properties
javax/swing/plaf/metal/resources/metal.properties
javax/swing/plaf/metal/resources/metal_ja.properties
sun/tools/jar/resources/jar.properties
sun/awt/motif/resources/updialog.properties
sun/awt/motif/resources/updialog_ja.properties
sun/awt/motif/resources/printcontrol.properties
sun/awt/motif/resources/printcontrol_ja.properties
sun/rmi/registry/resources/rmiregistry.properties
sun/rmi/rmid/resources/rmid.properties
javax/swing/plaf/basic/resources/basic.properties
# This properties file is used to create a PropertyResourceBundle
# It contains Locale specific strings used in Swing
# Currently, the following components need this for support:
#
#    ColorChooser
#    FileChooser
#    OptionPane
#
# When this file is read in, the strings are put into the 
# defaults table.  This is an implementation detail of the current
# workings of Swing.  DO NOT DEPEND ON THIS.  
# This may change in future versions of Swing as we improve localization 
# support.
#
# %I% %G%
# @author Steve Wilson

############ FILE CHOOSER STRINGS #############
FileChooser.fileDescriptionText=Generic File
FileChooser.directoryDescriptionText=Directory
FileChooser.newFolderErrorText=Error creating new folder
FileChooser.newFolderErrorSeparator= : 
FileChooser.acceptAllFileFilterText=All Files (*.*)
FileChooser.cancelButtonText=Cancel
FileChooser.saveButtonText=Save
FileChooser.openButtonText=Open
FileChooser.updateButtonText=Update
FileChooser.helpButtonText=Help

## file chooser tooltips ###
FileChooser.cancelButtonToolTipText=Abort file chooser dialog
FileChooser.saveButtonToolTipText=Save selected file
FileChooser.openButtonToolTipText=Open selected file
FileChooser.updateButtonToolTipText=Update directory listing
FileChooser.helpButtonToolTipText=FileChooser help

############ COLOR CHOOSER STRINGS #############
ColorChooser.previewText=Preview
ColorChooser.okText=OK
ColorChooser.cancelText=Cancel
ColorChooser.resetText=Reset
ColorChooser.swatchesNameText=Swatches
ColorChooser.swatchesRecentText=Recent:
ColorChooser.hsbNameText=HSB
ColorChooser.hsbHueText=H
ColorChooser.hsbSaturationText=S
ColorChooser.hsbBrightnessText=B
ColorChooser.hsbRedText=R
ColorChooser.hsbGreenText=G
ColorChooser.hsbBlueText=B
ColorChooser.rgbNameText=RGB
ColorChooser.rgbRedText=Red
ColorChooser.rgbGreenText=Green
ColorChooser.rgbBlueText=Blue

############ OPTION PANE STRINGS #############
OptionPane.yesButtonText=Yes
OptionPane.noButtonText=No
OptionPane.okButtonText=OK
OptionPane.cancelButtonText=Cancel

Other thins in javax/swing/plaf/metal/resources/metal.properties

# This properties file is used to create a PropertyResourceBundle
# It contains Locale specific strings used be the Metal Look and Feel.
# Currently, the following components need this for support:
#
#    FileChooser
#
# When this file is read in, the strings are put into the 
# defaults table.  This is an implementation detail of the current
# workings of Swing.  DO NOT DEPEND ON THIS.  
# This may change in future versions of Swing as we improve localization 
# support.
#
# 1.1 08/26/98
# @author Steve Wilson


############ FILE CHOOSER STRINGS #############

FileChooser.lookInLabelText=Look in:
FileChooser.fileNameLabelText=File name:
FileChooser.filesOfTypeLabelText=Files of type:
FileChooser.upFolderToolTipText=Up One Level
FileChooser.upFolderAccessibleName=Up
FileChooser.homeFolderToolTipText=Home
FileChooser.homeFolderAccessibleName=Home
FileChooser.newFolderToolTipText=Create New Folder
FileChooser.newFolderAccessibleNam=New Folder
FileChooser.listViewButtonToolTipText=List
FileChooser.listViewButtonAccessibleName=List
FileChooser.detailsViewButtonToolTipText=Details
FileChooser.detailsViewButtonAccessibleName=Details

To internationalize built your own files basic_tr.properties and metal_tr.properties in the javax/swing/plaf directory structure and get them on the boot of the application.

Notice, this is not guaranteed to work in the future.


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Mon Nov 22 16:50:18 EST 2004