Internationalization in JAVA


Introduction

Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Sometimes the term Internationalization is abbreviated as i18n, because there are 18 letters between the first "i" and the last "n"
Localization- Localization is the process of adapting software for a specific region or language by adding local specific components and translating text; its short name is l10n.

Characteristics of Internationalization:

  • A textual element such as a status message and the GUI  component lavels are not hard coded in the program. Instead, they are stored outside of the source code and retrieved dynamically.
  • Internationalization supports new language without recompilation.
  • Other culturally dependent data, such as dates and currencies, appear in formats that conform to the end user's  region and language.

Following step follow to create Internationalized application.

For example you have a frame with the two buttons "yes" and "no" and we want to display them in the French language so the button for "yes" changes to "Oui" and "No" changes to "Non" with out recompiling the program.
 

Step-1 First you create a properties file that stores the information; for creating it we save a simple text file with a .properties extension. For example xyz.properties. Write all the keys and their values using a Text Editor and here you must give any key's name on the left side and and key values on the right side.

Step-2
And save both files named as Msg_en_US.properties and Msg_fr_FR.properties.

internation1.gif

 Step-3 Defining the Locale Object identifies a particular language and country. The following language defines country.

standard.gifcountry.gif

 

Step-3 code

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

public
class Internationalization_Demo extends JFrame
{
 
String Cap_yes;
String Cap_no;
 

static
String language;
static
String country;
 
JButton Button_yes,Button_no;
 

static
public void main(String[] args)
 {
 
if (args.length != 2)
    {//Use Internationalization_Demo fr FR (French)
          //or
     //Internationalization_Demo en US (US English)
     System.out.println("Use :java Internationalization_Demo Language(Ex-fr,en) country(FR,US)");
     System.exit(1);
    }
 
 language = new String(args[0]);
 country  = new String(args[1]);
 
 Internationalization_Demo frame=new Internationalization_Demo();

 // Event handle for click on close button
with the help of anonymous class 
 frame.addWindowListener(new WindowAdapter() {
                           public void windowClosing(WindowEvent e) {
                               System.exit(0);
                           }
                       });
 frame.setBounds(0,0,400,200);
 frame.setVisible(true);
  }//main close
 

public
Internationalization_Demo()
  {
 Locale locale = new Locale(language, country);
 ResourceBundle  captions= ResourceBundle.getBundle("Msg",locale);
 Cap_yes =captions.getString("yesMsg");
 Cap_no  = captions.getString("noMsg");
 Button_yes = new JButton(Cap_yes);
 Button_no =  new JButton(Cap_no);
 getContentPane().add(Button_yes,BorderLayout.WEST);
 getContentPane().add(Button_no,BorderLayout.EAST);
 }//Internationalization_Demo construct close
 }
//Internationalization_Demo class close

OUTPUT:

In this cmd we give the cmd argument as en US here en means  English language code and US for United state.

cmdInternationalizatio.gif

inter.. output.gif

In this cmd we give the cmd argument as fr FR here en means  french language and FR for French country code.

cmdInternationalizatio2.gif

inter.. output2.gif


Resources

Code Internationalization

Use of ByteStreams and CharacterStreams in JAVA

Learning JDBC (Java Database Connectivity)

Working with Hibernate - Display , Insert, Update and Delete in JAVA

Up Next
    Ebook Download
    View all
    Learn
    View all