Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt

Related items

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #4

Java Applets #3

Java Applets #2

Java Applets #1

Steps to Java Internationalization (i18n)

You are here: irt.org | Articles | Java | Steps to Java Internationalization (i18n) [ previous next ]

Published on: Sunday 15th August 1999 By: Azzam Alnijres, Kaesar Alnijres

With Internationalization your Java applications can be adapted to various languages and regions without code changes. The term internationalization is abbreviated as i18n, because there are 18 letters between the first "i" and the last "n". Support for new languages does not require recompilation or code changes! Written by Azzam and Kaesar Alnijres.

Let's see how Internationalization works with a very simple program. You've got a Frame with two buttons : "Yes" and "No", and you want this same program to display "Oui" and "Non" in it's French version (Remember no code changes are rquired)

Write down the following lines in a plain-text file (for the English version):

yesMessage=Yes
noMessage=No 

Save your file as : Messages_en_US.properties.

Write another plain-text file for the French version:

yesMessage=Oui
noMessage=Non 

Save this file as : Messages_fr_FR.properties.

Notice that yesMessage and noMessage are the same in the English and French files, these words are keys. These keys must not change.

We'll talk about properties files later, but first let's write some Java code to use these files.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class I18N extends JFrame{

String yesCaption;
String noCaption;

static String language;
static String country;

JButton yesButton,noButton;

static public void main(String[] args) {


if (args.length != 2) {//Use I18N fr FR (French)
  	                //or
                      //I18N en US (US English)

  System.out.println("Use :java I18N Language country");
  System.exit(1);
    }

 language = new String(args[0]);
 country  = new String(args[1]);

 I18N frame=new I18N();

 frame.addWindowListener(new WindowAdapter() {
                           public void windowClosing(WindowEvent e) {
                               System.exit(0);
                           }
                       });

 frame.setBounds(0,0,200,100);
 frame.setVisible(true);
  }//main

public I18N(){
 
 Locale locale = new Locale(language, country);
 ResourceBundle  captions= ResourceBundle.getBundle("Messages",locale);

 yesCaption =captions.getString("yesMessage");
 noCaption  = captions.getString("noMessage");

 yesButton = new JButton(yesCaption);
 noButton =  new JButton(noCaption);

 getContentPane().add(yesButton,BorderLayout.WEST);
 getContentPane().add(noButton,BorderLayout.EAST);

  }//I18N construct

}//I18N

Has your code worked fine? Great.

Notes:

A Locale object is an identifier for a particular combination of language and region:

en US (English & USA)
en GB (English & Great Britain)
fr FR (French & France)
fr CA (French & Canada)
de DE (German & Germany)

Properties Files Naming Convention:

A properties file is a simple text file. This file can be a default Properties File (You should always create a default properties file) or an Additional one. The name of this file begins with the name that your ResourceBundle uses and ends with the .properties suffix.

For example, a default file will be called here Messages.properties. You can choose any name of your choice for your file, but remember to use the same name that your ResourceBundle uses.

For an an Additional properties file, the name of this file begins with the name that your ResourceBundle uses (as above), add codes for the language and the country, end the name with the .properties suffix.

How to use a properties file with a Local and ResourceBundle:

    Locale locale = new Locale("fr","FR");
    ResourceBundle  captions= ResourceBundle.getBundle("Messages",locale);

To retrieve the translated value from the ResourceBundle, invoke the getString method as follows:

    String value = captions.getString("yesMessage");

Now in the code above, you decide, to add support for 'de DE'.

  1. You must create a properties file for this language: Messages_de_DE.properties
  2. Translate your keys to German
  3. To use German, the end-user must enter I18N de DE
  4. That's it

Can Your Program Find Your Locale?

Yes. Set by the Java Virtual Machine when it starts up, the default Locale corresponds to the locale of the host platform. To determine the default Locale of your Java Virtual Machine, invoke the Locale.getDefault method.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class I18NDefault extends JFrame{

String yesCaption;
String noCaption;

JButton yesButton,noButton;

static public void main(String[] args) {


 I18NDefault frame=new I18NDefault();

 frame.addWindowListener(new WindowAdapter() {
                           public void windowClosing(WindowEvent e) {
                               System.exit(0);
                           }
                       });

 frame.setBounds(0,0,200,100);
 frame.setVisible(true); 
  }//main

public I18NDefault(){
 
 ResourceBundle captions=ResourceBundle.getBundle("Messages",Locale.getDefault());                                
 
 yesCaption =captions.getString("yesMessage");
 noCaption  = captions.getString("noMessage");

 yesButton = new JButton(yesCaption);
 noButton =  new JButton(noCaption);

 getContentPane().add(yesButton,BorderLayout.WEST);
 getContentPane().add(noButton,BorderLayout.EAST);

  }//I18NDefault construct

}//I18NDefault

You can download a zip file of all the .java and .class files for a fully working example.

You can also download our Free WYSIWYG Java development tool JVCoding2 from http://www.chez.com/alnijres/jvc2e/index.html

It allows you to write -visually- both applets and applications for jdk 1.0, jdk1.1, and jdk 1.2.

100% Java code (no native calls).

Related items

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #4

Java Applets #3

Java Applets #2

Java Applets #1

Feedback on 'Steps to Java Internationalization (i18n)'

View the profiles on Azzam Alnijres, Kaesar Alnijres.

©2013 Martin Webb