Internationalization and formatting (1): internationalization of the program

Source: Internet
Author: User


I18n_welcome.java:

public class I18N_Welcome {public static void main(String[] args) {Locale myLocale=Locale.getDefault();ResourceBundle bundle=ResourceBundle.getBundle("mess",myLocale);System.out.println(bundle.getString("welcome"));}}

Mess_en_us.properties:

welcome=Hello World !

Mess_zh_cn.properties:

welcome=\u4e16\u754c\u4f60\u597d\uff01

Mess_zh_cn.properties: the string after the medium number is "Hello World !" . For resource files that contain non-Western European characters, Java provides a tool for transcoding: native2ascii.

We can see that the basename of the two resource files is the same: mess. From the above process, we can see that if you want the program to be internationalized, you only need to store the prompts of different countries/languages (locale) in different files. For example, the language resource file for simplified Chinese is the xxx_zh_cn.properties file, and the language resource file for American English is the xxx_en_us.properties file.

The key category of Java program internationalization isResourcebundleIt has a static method:Getbundle (string basename, locale)This method loads resource files according to locale, while locale encapsulates a country and language. For example, the simplified Chinese environment can be represented by locale in simplified Chinese, the American English environment can be represented by locale in American English.

For example, use the following code to load the resource file:

ResourceBundle bundle=ResourceBundle.getBundle("mess",myLocale);

The above code will load one of the series of resource files whose basename is mess. Which resource file is loaded depends on mylocale, the second parameter. For locale in simplified Chinese, the mess_zh_cn.properties file will be loaded. For locale in American English, load the mess_en_us.properties file.

The output of the above program depends on the language environment of the operating system. If it is a simplified Chinese environment, the output will be "Hello World !"; If the English environment is in the United States, "Hello World!" Is output !".

Summary: The key classes for Java program internationalization are resourcebundle and locale,Resourcebundle loads the language resource file based on different locale, and then obtains the strings in the language resource file based on the specified key.

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.