Detailed Java locale class _java for internationalization

Source: Internet
Author: User
Tags dateformat locale object object

1. Locale Introduction
Locale says the region. Each of the locale objects represents a specific geographical, political and cultural area.
This is often used when manipulating dates, calendar, and other objects that represent date/time, because different regions have different time representations.
Here are 3 common ways to create locale objects.
(1) Get the default locale
How to use:

Locale Locale = Locale.getdefault ()

(2) Direct use of locale static objects
The following static objects are available in the Locale.java

public static final Locale CANADA public static final
Locale Canada_french public
static final Locale
Blic static final Locale Chinese public
static final Locale 中文版 public
static final Locale FRANCE public
s Tatic final Locale FRENCH public static
final Locale GERMAN public
static final Locale GERMANY public
static F Inal Locale Italian public static
final Locale ITALY public
static final Locale JAPAN public
static final L Ocale Japanese public static
final Locale KOREA public static
final Locale Korean public
static final local e PRC public
static final Locale ROOT public
static final Locale Simplified_chinese public
static final Loc Ale TAIWAN public static
final Locale Traditional_chinese public static
final Locale UK public
static FINA L Locale US

How to use: The following locale object corresponds to "China (mainland)"

Locale Locale = Locale.simplified_chinese

(3) Creating locale objects through locale constructors
There are 3 constructors for locale. As follows:

Locale (String language)
Locale (String language, String country)
Locale (String language, String country, string Variant

How to use:

Locale local = new Locale ("en", "CN");

The locale class supports a great number of countries and regions. We can view all the areas supported by locale in the following ways:

locale[] ls = locale.getavailablelocales ();

for (Locale locale:ls) {
  System.out.println ("Locale:" +locale);
}

Enter the results as follows:

 all locales:ja_jp, Es_pe, en, JA_JP_JP, Es_pa, Sr_ba, MK, ES_GT, Ar_ae, No_no, Sq_al, BG, Ar_I Q, Ar_ye, Hu, Pt_pt, El_cy, Ar_qa, MK_MK, SV, De_ch, en_US, Fi_fi, is, CS, EN_MT, Sl_si, Sk_sk, it, tr_tr, en, th, Ar_sa, No, EN_GB, Sr_cs, lt, Ro, En_nz, No_no_ny, Lt_lt, Es_ni, NL, Ga_ie, Fr_be, Es_es, ar_lb, Ko, Fr_ca, Et_ee, ar_kw, Sr_rs, E S_us, Es_mx, AR_SD, in_id, Ru, LV, Es_uy, LV_LV, IW, Pt_br, Ar_sy, HR, ET, ES_DO, Fr_ch, hi_in, Es_ve, Ar_bh, En_ph, Ar_tn  , FI, De_at, es, Nl_nl, Es_ec, ZH_TW, Ar_jo, be, is_is, Es_co, ES_CR, ES_CL, Ar_eg, En_za, th_th, El_gr, IT_IT, CA, Hu_hu, FR, En_ie, Uk_ua, PL_PL, Fr_lu, Nl_be, en_in, Ca_es, Ar_ma, Es_bo, En_au, Sr, Zh_sg, PT, UK, ES_SV, Ru_ru, Ko_kr, VI, AR_ DZ, Vi_vn, Sr_me, sq, ar_ly, AR, ZH_CN, be_by, Zh_hk, JA, Iw_il, BG_BG, in, MT_MT, Es_py, SL, fr_fr, Cs_cz, It_ch, Ro_ro,  ES_PR, En_ca, De_de, GA, De_lu, DE, Es_ar, SK, Ms_my, hr_hr, En_sg, DA, MT, PL, Ar_om, tr, th_th_th, El, MS, Sv_se, DA_DK, Es_hn 

Here are two instructions for how to use them to create a locale object:
For example, the first output is "JA_JP".
Among them, JA stands for "language", this refers to Japanese, "JP" on behalf of the country, this refers to Japan.
We can create "language is Japanese, country is Japanese locale object" through the following methods.

Locale Locale = new Locale ("Ja", "JP");

For example, the third output is "en".
Where en stands for "language", this refers to English.
We can create "locale objects with language in English" by using the following methods.

Locale Locale = new Locale ("en");

Locale Function List

Locale constructor
Locale (String language)
Locale (String language, String country)
Locale (String language, String country, String variant)
 object Clone () Boolean Equals (Object object) static locale[] Getavailablelocales () string getcountry () static Locale Getdefault () string Getdisplayco             Untry (Locale Locale) Final String getdisplaycountry () final string getdisplaylanguage () string
Getdisplaylanguage (Locale Locale) string GetDisplayName (Locale Locale) Final String GetDisplayName () Final string getdisplayvariant () string getdisplayvariant (Locale Locale) string Getiso3cou Ntry () String getiso3language () static string[] Getisocountries () static string[] Getisolanguages ( String GetLanguage () string getvariant () synchronized int hashcode () synchronized static V OID SetDefault (Locale Locale) Final String toString () 

The

2. Locale Example
shows the use of locale in date using the example below. The
Reference code is as follows (Localetest.java):

Import Java.util.Locale;
Import Java.util.Date;
Import Java.util.Calendar;
Import Java.text.SimpleDateFormat;

Import Java.text.DateFormat;
    /** * Locale Test program/public class Localetest {public static void main (string[] args) {//2 different Locale creation methods

    Testdiffdatelocales ();
  Show all locales testalllocales (); /** * 2 different locale creation methods * * private static void Testdiffdatelocales () {//date is 2013-09-19 14:22:30 Da

    Te date = new Date (113, 8, 19, 14, 22, 30);
    Create "Simplified Chinese" Locale Locale LOCALECN = Locale.simplified_chinese;

    Create "English/American" Locale Locale localeus = new Locale ("en", "us");
    Gets the date string corresponding to "Simplified Chinese" string cn = Dateformat.getdateinstance (Dateformat.medium, LOCALECN). Format (date);

    Gets the "English/United States" corresponding date string, string us = Dateformat.getdateinstance (Dateformat.medium, Localeus). Format (date);
  System.out.printf ("cn=%s\nus=%s\n", CN, US); /** * Shows all locales * * private static void Testalllocales () {Locale[] ls = locale.getavailablelocales ();
    System.out.print ("All Locales:");
    for (Locale locale:ls) {System.out.printf (locale+ ",");
  } System.out.println ();
 }
}

3. Use Resoucebundle to read internationalized resource files

The ResourceBundle resource bundle contains objects for a specific locale. Use it to load and read language environment resources.
1. Easy localization or translation into different languages
2. Handle multiple locales at a time
3. Can be easily modified in the future, and support more of the language environment.

When a program needs a locale-specific object, it uses the Getbundle () method to load the ResourceBundle class

ResourceBundle RB = Resourcebundle.getbundle ("Res.") Messagebundle ", Currentlocale);

of which, Res. What Messagebundle represents is that we built a res folder under SRC, and there is a resource file named Messagebundle.properteis under the Res folder. We allow the creation of multiple internationalized resource files here:

Messagebundle_zh_cn.properties; Messagebundle_en_us.properteis;

Where Messagebundle is the string to be used when constructing resourcebundle, the name is valid and does not require a specific constraint. The name behind must follow the rules,

Resourcename_language_country.properteis;

Then we use Rb.getstring (key), we can get the value of the key in the resource file.
Using struts1.x to implement international automatic switching

See struts source can be found, language environment information is stored in the session, through This.setlocale (request, locale);
So to achieve internationalization, click the hyperlink on the page to change the value of the locale (locale) in the session when the action is clicked. In action:

Accept language request information from client 
String language = Request.getparameter ("Mylanguage"); 
 
Define language Area information 
Locale currentlocale = null; 
 
Create different locale information based on different requests 
if ("en". Equals (language)) { 
  Currentlocale = new Locale ("en", "CN"); 
} else if ("en". Equals (language)) { 
  Currentlocale = new Locale ("en", "US"); 
} 
..... Change the information in session to  
This.setlocale (request, Currentlocale); 
 
Of course, when the page displays resource file information, the following method is used: 
 
//<bean:message key= "key"/> 
Related Article

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.