Sunday, October 25, 2009

What is Java Internationalization



The term internationalization is abbreviated as i18n,because there are 18 letters between the first "i" and the last"n", with Java Internationalization the Java applications can beadapted to various languages and regions without code changes. Support for newlanguages does not require recompilation or code changes!



Here we see a simple program which explains the Java internationalization.We've got a Frame with two buttons : "Yes" and "No", and wewant this same program to display "Oui" and "Non" in it'sFrench 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 theEnglish 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 codeto 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 andregion:
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 adefault Properties File (You should always create a default properties file) oran Additional one. The name of this file begins with the name that yourResourceBundle uses and ends with the .properties suffix.
For example, a default file will be called here Messages.properties. You canchoose any name of your choice for your file, but remember to use the same namethat your ResourceBundle uses.


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


How to use a properties file with a Local andResourceBundle:


Locale locale = new Locale("fr","FR");


ResourceBundle captions=ResourceBundle.getBundle("Messages",locale);

To retrieve the translated value from the ResourceBundle, invoke the getStringmethod as follows:
String value = captions.getString("yesMessage");


Now in the code above, you decide, to add support for 'deDE'.
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, thedefault Locale corresponds to the locale of the host platform. To determine thedefault Locale of your Java Virtual Machine, invoke the Locale.getDefaultmethod.


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





Stumble
Delicious
Technorati
Twitter

0 Comments:

Post a Comment