First Easy Application For Internationalization
- By
- On 28/12/2015
- Comments (0)
- In Internationalization
If you're new to internationalizing software, then, this chapter is for you.
By a simple example you'll learn how Locale
and ResourceBundle
objects work together and how to use properties files:
- Before Internationalization:
Simply, lets consider the following program: (which is NOT internationalized): and analyze the underlined information
public class NotI18N
{
static public void main(String[] args)
{
System.out.println("Hello.");
System.out.println("How are you?");
System.out.println("Goodbye.");
}
}
- After Internationalization:
Let's suppose that we want to Internationalize our code into three language French, English and German:
In order to insure that we need to create four property files: (each one contains the content below)
- MessagesBundle.properties:
greetings = Hello.
farewell = Goodbye.
inquiry = How are you?
- MessagesBundle_de_DE.properties:
greetings = Hallo.
farewell = Tschüß.
inquiry = Wie geht's?
- MessagesBundle_en_US.properties:
greetings = Hello.
farewell = Goodbye.
inquiry = How are you?
- MessagesBundle_fr_FR.properties:
greetings = Bonjour.
farewell = Au revoir.
inquiry = Comment allez-vous?
import java.util.*;
public class I18NSample {
static public void main(String[] args) {
String language;
String country;
if (args.length != 2) {
language = new String("en");
country = new String("US");
} else {
language = new String(args[0]);
country = new String(args[1]);
}
Locale currentLocale;
ResourceBundle messages;
currentLocale = new Locale(language, country);
messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
System.out.println(messages.getString("greetings"));
System.out.println(messages.getString("inquiry"));
System.out.println(messages.getString("farewell"));
}
}