Java international support (I18N problem)

Source: Internet
Author: User

Java international support (I18N problem)

1. Read the locale information of the PC.

Locale information is composed of the language and the Country Code. In Java, it provides the Locale class corresponding language information.

 

 

/** // Obtain the local information: Locale defaultLocale = Locale. getDefault (); * System. out. println ("country:" + defalocallocale. getCountry (); * System. out. println ("language:" + defaultLocale. getLanguage ());*/
2. How to support internationalization

1. Add international resource files

The international resource file consists of baseName + Locale, for example, MessagesBundle_zh_CN.properties. The default international resource file name is baseName. properties. The value of a Chinese international resource file must be converted to unicode encoding. For example, k1 = \ u4F60 \ u597D, {0 }.

2. Read international resource files

// Define locale class // Locale currentLocale = new Locale ("zh", "CN"); Locale defaultLocale = Locale. getDefault (); ResourceBundle rb = ResourceBundle. getBundle ("MessagesBundle", defaultLocale); String k1 = rb. getString ("k1"); String k2 = rb. getString ("k2"); System. out. println (k1); System. out. println (k2); MessageFormat mf = new MessageFormat (rb. getString ("k1"); System. out. println (mf. format (new Object [] {"James "}));

For default placeholders, you can use MessageFormat to assign values dynamically.

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.