Internationalization

Swing in Java also supports the feature of Internationalization. The developers can build applications by which the users can interact worldwide in different languages.

Internationalization

Swing in Java also supports the feature of Internationalization. The developers can build applications by which the users can interact worldwide in different languages.

Internationalization

Internationalization

     

Swing in Java also supports the feature of Internationalization. The developers can build applications by which the users can interact worldwide in different languages. They can also create applications that can accept input in languages having different characters such as French, Spanish, Japanese, etc.  

An automatic support is provided by the Swing's layout managers required by the UI. That is Swing's layout manager let the UI to appear from right to left in a locale. Hence to let the UI work for left to right and right to left and also to take care of the size of components that change on localization, you need to code the UI only once.

Lets see how to internationalize the program given below.

import java.util.*;

public class InternationalizationDemo { 
  public static void main(String[] args) { 
  System.out.println("The text displayed is specific to locale"+" ("+Locale.getDefault().getDisplayLanguage() +", "+ Locale.getDefault()
.getDisplayCountry () + ").\n"); 
  System.out.println("Hello, how are you?"); 
  System.out.println("Thanks to visit."); }
}

Now you want this program to get internationalized so that it may response according to the specific region and country i.e. locale (Remember no code changes are required for different locale). Just follow these steps:

1. Create Properties Files:

Create ?. properties? file containing a set of key and value pair. Remember to keep the keys same in all the files and provide values according to the locale in different files. 

Properties Files Naming Convention:

Creating a default properties file is a good practice, which is available to the program by default. You can give any name to this file but remember to use the same name that your ResourceBundle uses (MessageBundle.properties file in our example). While naming other properties files follow the syntax:

PropertiesFileNameUsedInResourceBundle_languageCode_countryCode.properties

Let?s create these files. 

Default file:
Write down the following lines in a plain-text file (Default version) and save it as MessagesBundle.properties:

localeInfo = The text displayed is specific to locale
welcome = Hello, how are you?
sayThanks = Thanks to visit.


Other properties files:
Write down the following lines in a plain-text file (for the French version) and save it as MessagesBundle_fr_FR.properties:

localeInfo = Le texte affiché est spécifique à la scène
welcome = Bonjour, comment allez-vous ?
sayThanks = Merci pour visiter.

Write down the following lines in a plain-text file (for the Spanish version) and save it as MessagesBundle_es_ES.properties:

localeInfo = El texto mostrado es específico al lugar
welcome = ¿Hola, ¿Cómo está usted?.
sayThanks = Gracias a visita.

Notice that ?localeInfo?, ?welcome?, ?sayThanks? keys are same in English, French, Spanish files and values are replaced with the converted value of the particular language.

2. Remove hard coded text from the source file:

Our next step is to remove the hard coded text from our source file and use the keys of properties file. These values are picked up at the run time from the properties file according to the locale provided to the program. So this source file doesn?t require to be compiled while dealing with the different locales. You can be sure of this fact viewing the code modified below:

import java.util.*;

public class InternationalizationDemo {
  public static void main(String[] args) {
  String language;
  String country;
  Locale locale;
  ResourceBundle rb;

  if (args.length != 2) {
  language = new String("en");
  country = new String("US");
  }
  else {
  language = new String(args[0]);
  country = new String(args[1]);
  }
 locale = new Locale(language, country);
 rb = ResourceBundle.getBundle("MessagesBundle", locale);
 System.out.println(rb.getString("localeInfo"" ( " 
locale.getDisplayLanguage
() "," + locale.getDisplayCountry() ").\n");
 System.out.println(rb.getString("welcome"));
 System.out.println(rb.getString("sayThanks"));
  }
}

When the set of language code and country code is provided at run time, the output is shown according to that particular locale. For example, in the output below, for ?es? and ?ES? the output is displayed in Spanish language. The text displayed is fetched from MessageBundle_es_ES.properties file. If  ?fr? and ?FR? codes are provided at run time then values are displayed in French language. Now this text is fetched from MessageBundle_fr_FR.properties file. Here we are not changing and compiling the source code to work for different locale because the program references the keys, not the values.

Output of the Program:

C:\Program Files\Java\jdk1.6.0_01\bin>javac InternationalizationDemo.java

C:\Program Files\Java\jdk1.6.0_01\bin>java InternationalizationDemo
The text displayed is specific to locale (English,United States).

Hello, how are you?
Thanks to visit.

C:\Program Files\Java\jdk1.6.0_01\bin>java InternationalizationDemo fr FR
Le texte affichΘ est spΘcifique α la scΦne (French,France).

Bonjour, comment allez-vous ?
Merci pour visiter.

C:\Program Files\Java\jdk1.6.0_01\bin>java InternationalizationDemo es ES
El texto mostrado es especφfico al lugar. (Spanish,Spain).

┐Hola, ┐C≤mo estß usted?.
Gracias a visita.

Download this example.