The locale class supports a great number of countries and regions. We can view all the areas supported by locale in the following ways:
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
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.
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"/>