[Excerpt] internationalization/localization in Java)

Source: Internet
Author: User

I. Glossary

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.

Ii. Features of International Programs

  • 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.

Iii. Internationalization steps

To internationalize a program, follow these steps:

1. Create a properties file-local data

The properties file name must follow a fixed format, such as "basename_ll_cc.properties" or "basename_ll.properties ". Ll indicates the language code (such as "ZH") and CC indicates the Country Code (such as "cn "). For example, localized Chinese data can be stored in the file "message_zh.properties.

2. Create a local object-the identifier 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:
Local locale = new locale ("en", "us ");
This object marks a place in the United States where the language is English.

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.
You can use the following statement to create this object:
Resourcebundle RB = resourcebundle. getbundle ("messagesbundle", locale );

4. Use resourcebundle to read information-implemented by Localization

After reading the propeties file, we can use the resourcebundle instance to obtain information through the getstring (string key) method.

Iv. program example

1. Create a project and create the following three files under the application root path:

  • Classmain. Java
  • Message_zh.properties
  • Message_en.properties

For simplicity, only one row of data is written to each file:
Message_zh.properties:
Msg0 = "Chinese locale"
Message_en.properties:
Msg0 = "English locale"

2. Enter the following code in classmain. Java:

Import java. util .*;

Public class classmain {
Public static void main (string ARGs [])
{
Locale local = new locale ("en", "us ");
Resourcebundle RB = resourcebundle. getbundle ("message", local );
System. Out. println (RB. getstring ("msg0 "));

Local = new locale ("ZH", "cn ");
RB = resourcebundle. getbundle ("message", local );
System. Out. println (RB. getstring ("msg0 "));
}
}

Compile and run the following results:

"English locale"
"Chinese locale"

Reference: http://wenku.baidu.com/view/2ef69bc52cc58bd63186bda5.html

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.