Java internationalization Mechanism

Source: Internet
Author: User

/// Link: http://tech.ccidnet.com/art/3737/20040616/449729_1.html

Internationalization is the process of designing an application that is applicable to multiple languages and regions. It is applicable to multiple languages and regions. When users in different languages and regions use this application, applications must use the language they can understand and conform to their cultural habits to display information. Internationalization is sometimes referred to as i18n because 18 letters are in the range of [I, n.

An international program generally has the following features:

  • There is an additional localized data and it has the ability to execute in various regions around the world.
  • Text elements, such as status information or GUI section lables, are not directly written (hardcoded) in a program, but stored in localized data, and can be used dynamically by the program correctly.
  • When new languages are supported, you do not need to modify the program or re-compile the program.
  • Data on cultural differences, such as dates and currencies, must display different formats based on Supported languages and habits.
  • Can be quickly localized.

Localization refers to the process of adapting applications to different languages and regions by adding locale-specific components and Text Translation. Localization is sometimes referred to as l10n. It should contain 10 letters between the letters l and N of the localized English word. Generally, the most time-consuming job of localization is text translation. Local chemical authors need to create new formats for data such as dates, numbers, and commodities based on specific regional needs. Other types of data, such as sound and image, also need to be determined based on specific needs.

The following is a simple example to illustrate how to provide an international feature for a program. In this example, different text information is displayed in different language environments.

A simple example

Let's take a look at the following code:
Public class noti18n {
Static public void main (string [] ARGs ){
System. Out. println ("hello .");
System. Out. println ("how are you? ");
System. Out. println ("goodbye .");
}
}
If you decide to display the same information to different users in Germany and France in the above program. But your programmer is not a language expert. He does not know German or French. So you need to translate it into German and French, but your translators do not understand the program, therefore, you can store the information in a text or other format for translation personnel. Then, the program must be able to display information in different languages, and you do not know what the next language you want to provide for the program is, maybe Japanese or other languages.
The following code is an international code example:
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 "));
}
}
Note: In the above Code, the information is not included in the Code by hardcoded!
To run the above Code, download the following file:
1. i18nsample. Java
2. messagebundle. Properties
3. messagebundle_de_de.properties
4. messagebundle_en_us.properties
5. messagebundle_fr_fr.properties
Let's take a look at the running result of this program:
Display French information:
Java i18nsample fr
Bonjour.
Comment allez-vous?
Au revoir.
Show English information:
Java i18nsample en us
Hello.
How are you?
Goodbye.

From the code above, we can see that in code that implements internationalization, the information to be displayed is not directly included in the code, but in a file for use by the program. The program obtains different information in the file based on different languages and countries (local. We try to step by step to analyze how the code has been internationalized:

1. Create a properties file ---- local data

The properties file is used to store information related to the program and environment. It must end with the suffix of. properties. The properties file is in plain text format. In the preceding example, there are four properties files. Defines the language used for greeting, goodbye, and greeting in different languages and countries. The properties file uses the key-value format as follows:
Greetings = bonjour.
Farewell = au revoir.
Inquiry = Comment allez-vous?
The name of the properties file is very important. The format is basename_ll_cc.properties.ll, which indicates the language code and CC indicates the country code. The language code and country code are the parameters for local class initialization, And the basename is the parameter for creating the resourcebundle object.

2. Create a local object-IDs of different languages and countries

Java. util. Local is the standard API provided by JDK. This class is used to identify the country and language.
We can create a local object:
Alocale = new locale ("en", "us ");
This object marks a place in the United States where the language is English.
What do the following two objects represent?
Calocale = new locale ("FR", "ca ");
Frlocale = new locale ("FR", "FR ");
The calocale object marks Canada and uses French, while the frlocal indicates France and the language is French.
Note: The local object is only an identifier class. The object created for this class does not indicate that your program has been internationalized. This class is used for classes that require internationalization. It is used to identify the local information that we will implement internationalization.

3. Create a resourcebundle object-the main role for internationalization in this example

Java. util. resourcebundle is a standard API provided by JDK. It is actually an abstract class and provides a static factory method for creating its subclass. In the example program, we actually use the propertyresourcebundle object, a subclass of resourcebundle. Through this, we can read different properties files based on the local object to obtain different information.
If local = new locale ("en", "us"); then the messagesbundle_en_us.properties file is read.
In the example, use the following statement to create this object:
Message = resourcebundle. getbundle ("messagesbundle", currentlocale );

4. Use resourcebundle to read information ---- implement Localization

After reading the propeties file, we can use the resourcebundle instance to obtain information through the getstring (string key) method. The statement is as follows:
String msg1 = messages. getstring ("greetings ");

Conclusion:

As shown in the preceding example, internationalization is not difficult. He needs you to make some plans and add a small amount of extra code to the program. This chapter is just an introduction. In the subsequent sections, we will learn more about Java's advanced features of internationalization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.