Magazine
 
Java Developer’s Internationalization

MessagesBundle.properties:

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

Other properties files:

Write down the following lines in a plaintext 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 plaintext 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:

InternationalizationDemo.java:
(Internationalization Support)

 

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.get
Bundle(“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”));
}
}


A Locale object represents a specific geographical, political, or cultural region. In other words, Locale is an identifier for a particular combination of language and region. One of its constructors is: Locale(String language, String country) First argument specifies the language and the second one specifies the country to support. The language argument is a valid ISO language code (two letter code and in lower case) defined by ISO-639. In the same way, country argument is also a valid ISO country code (two letter code but in upper case) defined by ISO-3166. For example, the list of some language code and country code is given below:

Sept 2007 | Java Jazz Up |11
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF