Java program Internationalization

Source: Internet
Author: User

The purpose of introducing internationalization is to provide adaptive and user-friendly user interfaces without any changes.Program. The idea of internationalizing Java programs is to put tags, prompts, and other information in the program into the resource file. The corresponding resource files need to be provided for the countries and languages that the program needs to support. The resource file is a key-value pair. The key of each resource file remains unchanged, but the value changes with the country and language. Java program internationalization is mainly achieved through the following three classes:

Java. util. resourcebundle: used to load a country or language resource package.

Java. util. locale: Used to encapsulate a specific country/region or language environment.

Java. Text. messageformat: used to format strings with placeholders.

Note: resource files must be stored inClasspathUnder the root directory(SRCLower).

Resource file names can be in the following three forms:

Basename_language_country.properties

Basename_language.properties

Basename. Properties

Basename is the basic name of the resource file. It is user-defined, and language and country cannot be changed at will. The language and country supported by Java must be used.

The following program obtains the countries and languages supported by Java:

 
Package COM. lovo; import Java. util. locale; public class localelist {public static void main (string [] ARGs) {// returns the array Locale [] localelist = locale. getavailablelocales (); // traverses each element of the array and obtains the supported countries and languages in sequence (INT I = 0; I <localelist. length; I ++) {// print the supported country and language system. out. println (localelist [I]. getdisplaycountry () + "=" + localelist [I]. getcountry () + "" + localelist [I]. getdisplaylanguage () + "=" + localelist [I]. getlanguage ());}}}

The following two resource files are provided:
The first resource file: mess_zh_cn.properties

Hello = Hello

The second resource file: mess_en_us.properties

Hello = welcome you

Note:

Java provides a tool for processing resource files that contain non-Western European characters: native2ascii.

% Java_home %/bin path. The syntax format of this tool is as follows:

Native2ascii source resource file destination resource file

Example:Native2ascii mess_zh_cn.properties AA.Properties

Generated AA. PropertiesFile is the resource file we need, and then rename it mess_zh_cn.properties.

 

The following simple program is internationalized:

 
Import Java. util. locale; import Java. util. resourcebundle; public class Hello {public static void main (string [] ARGs) {// obtain the default country/language environment locale mylocale = locale. getdefault (); // load the resource file resourcebundle bundle1 = resourcebundle according to the default country/language environment of the system. getbundle ("mess", mylocale); // You can also directly specify the country/language environment to load the resource file resourcebundle bundle2 = resourcebundle. getbundle ("mess", locale. US); // print the message system extracted from the resource file. out. println (bundle1.getstring ("hello"); system. out. println (bundle2.getstring ("hello "));}}

 

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.