1. Locale introduction
Locale means region. Each Locale object represents a specific geographical, political and cultural region.
It is often used when operating objects representing dates/times such as Date, Calendar, etc.; because the time representation method is different in different regions.
Let’s talk about three common ways to create Locale objects.
(1) Get the default Locale
How to use:
Locale locale = Locale.getDefault()
(2) Directly use Locale's static object
The following static objects are provided in Locale.java
public static final Locale CANADApublic static final Locale CANADA_FRENCHpublic static final Locale CHINApublic static final Locale CHINESEpublic static final Locale ENGLISHpublic static final Locale FRANCEpublic static final Locale FRENCHpublic static final Locale GERMANpublic static final Locale GERMANYpublic static final Locale ITALIANpublic static final final Locale ITALYpublic static final Locale JAPANpublic static final Locale JAPANESEpublic static final Locale KOREApublic static final Locale KOREANpublic static final Locale PRCpublic static final Locale ROOTpublic static final Locale SIMPLIFIED_CHINESEpublic static final Locale TAIWANpublic static final Locale TRADITIONAL_CHINESEpublic static final Locale UKpublic static final Locale US
How to use: The following Locale object corresponds to "China (Mainland)"
Locale locale = Locale.SIMPLIFIED_CHINESE
(3) Create Locale object through Locale constructor
There are 3 constructors in 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("zh", "CN");The Locale class supports a lot of countries and regions. We can view all areas supported by Locale through the following methods:
Locale[] ls = Locale.getAvailableLocales();for (Locale locale:ls) { System.out.println("locale :"+locale);} The input results are 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_IQ, 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, zh, 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, es_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
Select two of them to illustrate how to use them to create Locale objects:
For example, the first output is "ja_JP".
Among them, ja represents "language", which refers to Japanese; "JP" represents country, which refers to Japan.
We can create "the language is Japanese and the country is Japan's Locale object" through the following method.
Locale locale = new Locale("ja", "JP"); For example, the third output is "en".
Among them, en represents "language", which refers to English here.
We can create "Locale object whose language is English" through the following method.
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 getDisplayCountry(Locale locale)final String getDisplayCountry()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 getISO3Country()String getISO3Language()static String[] getISOCountries()static String[] getISOLanguages()String getLanguage()String getVariant()synchronized int hashCode()synchronized static void setDefault(Locale locale)final String toString()
2. Locale example
The following is an example to demonstrate using Locale in Date.
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 Date date = new Date(113, 8, 19, 14, 22, 30); // Create Locale Locale localeCN = Locale.SIMPLIFIED_CHINESE; // Create Locale LocaleUS = new Locale("en", "US"); // Get the date string corresponding to "Simplified Chinese" String cn = DateFormat.getDateInstance(DateFormat.MEDIUM, localeCN).format(date); // Get the date string corresponding to "English/US" String us = DateFormat.getDateInstance(DateFormat.MEDIUM, localeUS).format(date); System.out.printf("cn=%s/nus=%s/n", cn, us); } /** * Show 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 ResourceBundle to read international resource files
The ResourceBundle resource package contains objects for a specific locale. Use it to load and read locale resources.
1. Easily localize or translate into different languages
2. Handle multiple locale environments at once
3. You can easily modify it in the future and support more locale environments once.
When a program needs locale-specific objects, it uses the getBundle() method to load the ResourceBundle class
ResourceBundle rb = ResourceBundle.getBundle("res.MessageBundle", currentLocale);Among them, res.MessageBundle represents that we have created a res folder under src, and there is a resource file named MessageBundle.properteis under the res folder. We allow multiple international resource files to be created here:
MessageBundle_zh_CN.properties;MessageBundle_en_US.properteis;
Among them, MessageBundle is a string to be used when constructing a ResourceBundle. This name is legal and does not require specific constraints. The following names must follow the rules.
resourceName_language_country.properteis;
Then we use rb.getString(key); to get the value corresponding to the Key in the resource file.
Use Struts1.x to achieve international automatic switching
By checking the Struts source code, you can find that the locale information is stored in the session, through this.setLocale(request, locale);
Therefore, to achieve internationalization, you need to change the Locale value in the session when you click the hyperlink on the page and pass the Action. In Action:
//Accept the language request information sent by the client String language = request.getParameter("myLanguage"); //Define the language region information Locale currentLocale = null; //Create different locale information according to different requests if ("zh".equals(language)) { currentLocale = new Locale("zh", "CN"); } else if ("en".equals(language)) { currentLocale = new Locale("en", "US"); } //... //Change the information in the session this.setLocale(request, currentLocale); //Of course, when displaying resource file information on the page, use the following method: //<bean:message key=”key” />