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 

  • Textual elements such as a status message and the GUI  component labels are not hardcoded in the program.  Instead, they are stored outside of the source code and retrieved dynamically.
  • Internationalization supports a 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 an 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 "None", without 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.  Here, you must give any key's name on the left side 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.gif
country.gif
 
Step-4 code
  1. import java.util.*;  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5.    
  6. public class Internationalization_Demo extends JFrame  
  7. {  
  8.    
  9. String Cap_yes;  
  10. String Cap_no;  
  11.    
  12. static String language;  
  13. static String country;  
  14.    
  15. JButton Button_yes,Button_no;  
  16.    
  17. static public void main(String[] args)  
  18.  {  
  19.  if (args.length != 2)  
  20.     {//Use Internationalization_Demo fr FR (French)  
  21.           //or  
  22.      //Internationalization_Demo en US (US English)  
  23.      System.out.println("Use :java Internationalization_Demo Language(Ex-fr,en) country(FR,US)");  
  24.      System.exit(1);  
  25.     }  
  26.    
  27.  language = new String(args[0]);  
  28.  country  = new String(args[1]);  
  29.    
  30.  Internationalization_Demo frame=new Internationalization_Demo();  
  31.  // Event handle for click on close button with the help of anonymous class   
  32.  frame.addWindowListener(new WindowAdapter() {  
  33.                            public void windowClosing(WindowEvent e) {  
  34.                                System.exit(0);  
  35.                            }  
  36.                        });  
  37.  frame.setBounds(0,0,400,200);  
  38.  frame.setVisible(true);  
  39.   }//main close  
  40.    
  41. public Internationalization_Demo()  
  42.   {  
  43.  Locale locale = new Locale(language, country);  
  44.  ResourceBundle  captions= ResourceBundle.getBundle("Msg",locale);  
  45.  Cap_yes =captions.getString("yesMsg");  
  46.  Cap_no  = captions.getString("noMsg");  
  47.  Button_yes = new JButton(Cap_yes);  
  48.  Button_no =  new JButton(Cap_no);  
  49.  getContentPane().add(Button_yes,BorderLayout.WEST);  
  50.  getContentPane().add(Button_no,BorderLayout.EAST);  
  51.  }//Internationalization_Demo construct close  
  52.  }//Internationalization_Demo class close 
     
Output
 
In this cmd, we give the cmd argument as en-US here en means, English language code and the US for the United States.
 

Summary 

 
In this article, we learned about Internationalization in Java and how to use Internationalization in Java Program. 


Similar Articles