Notes: International, notes International
Internationalization (I18N) indicates the first letter of the word, 18 indicates the length of the word, and N indicates the last letter of the word. Internationalization is also called Localization (L10N ).
Java internationalization is mainly achieved through the following three classes:
- Java. util. ResourceBundle: used to load a resource package
- Java. util. Locale: corresponds to a specific country/region and language environment.
- Java. text. MessageFormat: used to format messages
To internationalize a program, you must provide the resource files required by the program. The content of a resource file is composed of key-value pairs. The resource file can be named in three formats:
- Basename_language_country.properties
- Basename_language.properties
- Basename_properties
If the resource file contains non-Western characters, you must use the native2ascii tool that comes with JDK. The syntax format of this tool is as follows:
Native2ascii resource file name target resource file name
For example:
Native2ascii mess_zh_XXX.proerties mess_zh_CN.proerties
The Locale class can be used to obtain the regional environment (for example, Locale. ENGLISH, Locale. CHINESE, these constants return a Locale instance). You can also obtain the regional language environment used by the current system, or use the language and region to create a Locale object, the local language of Java is used when the International Standard Organization (ISO) defines the encoding, the local language is represented by the code of two lower-case letters, following the ISO-639-1; country code consists of two uppercase letters of code, followed by ISO-3166-1, the following table is commonly used code:
Language |
Code |
Country |
Code |
Chinese |
Zh |
China |
CN |
English |
En |
UnitedStates |
US |
Japan |
Ja |
Japan |
JP |
You can use the static method getAvailableLocales of Locale to obtain the Supported languages and countries. This method returns a Locale array containing the languages and countries supported by java. The Code is as follows:
Locale [] availableLocales = Locale. getAvailableLocales ();
For (Locale l: availableLocales ){
System. out. println ("Locale DisplayName =" + l. getDisplayName () + "Country =" + l. getCountry () + "Language ="
+ L. getLanguage ());
}
- Get Locale object
- Use the factory method to obtain the formatter object. When using the factory method, the NumberFormat class static method accepts a Locale type parameter. There are three factory methods: getNumberInstance (number) and getCurrencyInstance (currency) and getPercentInstance (percentage)
- Use this formatter object to complete formatting and parsing
Sample Code for formatting:
Locale deLocale = newLocale ("de", "DE ");
NumberFormat currFmt = NumberFormat. getCurrencyInstance (deLocale );
Double amt =123878.34;
String formatResult = currFmt. format (amt );
System. out. println ("amt =" + amt + "Format =" + formatResult );
If you want to read a number that is input or stored according to a Locale usage, you need to use the parse method.
Sample Code for parsing:
Localede Locale = newLocale ("de", "DE ");
NumberFormat currFmt = NumberFormat. getCurrencyInstance (deLocale );
Number input = currFmt. parse (formatResult );
Double parseAmt = input. doubleValue ();
System. out. println ("FormatAmt =" + formatResult + "ParseAmt =" + parseAmt );
Once the getBundle method locates a package, for example, "package name _ zh_CN", it will continue to search for the "package name _ zh" and "package name" packages, if these packages also exist, they are called "package name _ zh_CN" parent package in the resource level. If the package does not exist in the current package when searching for resources in the future, the parent package will be searched.