Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text.
In this tutorial, I will demonstrate the following:
- How to use Resource Bundles in Java.
- How locales are defined using the language code and country code.
- How to retrieve a proper resource bundle using the correct locale.
- How to write a simple internationalized document.
Please note that the following files are required to run this program.
- MessagesBundle.properties
- MessagesBundle_de_DE.properties
- MessagesBundle_en_US.properties
- MessagesBundle_fr_FR.properties
- MessagesBundle_np_NP.properties
You can also download the Source Code of this program.
package com.kushal.utils;
import java.util.*;
public class MyInternationalization {
static public void main(String[] args) {
/**
* Printing Messages in English Language
**/
printMessages("en", "US");
/**
* Printing Messages in German Language
**/
printMessages("de", "DE");
/**
* Printing Messages in French Language
**/
printMessages("fr", "FR");
/**
* Printing messages in Nepali Language
**/
printMessages("np", "NP");
}
public static void printMessages(String language, String country)
{
Locale currentLocale;
ResourceBundle messages;
/*Creating Locale Object Using language code & country code**/
currentLocale = new Locale(language, country);
/* Getting the correct resource bundle for the current locale.
* The Resource Bundles are .PROPERTIES files with certain naming
* conventions and contain string messages.
**/
messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
System.out.println();
System.out.println(messages.getString("greetings"));
System.out.println(messages.getString("inquiry"));
System.out.println(messages.getString("farewell"));
}
}
—————————————————–
Here is the output of this program:
—————————————————-
Hello.
How are you?
Goodbye.
Hallo.
Wie geht’s?
Tschüß.
Bonjour.
Comment allez-vous?
Au revoir.
Namaste.
Sanchai Hunu Hunchha.
Baai?
Originally posted 2009-04-06 14:23:01.