Internatinonalization In Java [Tutorial]
Kushal Paudyal August 17th, 2010
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?
Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!
Originally posted 2009-04-06 14:23:01.