Javaweb Study Summary (31)--Internationalization (I18N) Overview of international development
Software internationalization: When software is developed, it should be able to respond to different regions and countries in the world, and provide corresponding pages or data that meet the visitors ' reading habits for different regional and national visits.
internationalization (internationalization) is also known as i18n (The Reading method is I, it is said that because internationalization (internationalization) of the word from I to n there are 18 English letters, I18N's name from this)
Second, the qualified international software
The following two features are required to achieve internationalization of the software:
1, for the fixed use of text elements in the program, such as menu bar, navigation bar, such as the use of text elements, or error messages, status information, etc., according to the visitor's region and country, select the text of different languages for the service .
2, for the data generated by the program dynamically , for example (date, currency, etc.), the software should be able to according to the current country or region's cultural habits to display .
Third, the internationalization of fixed text elements
The fixed text information such as menu bar, navigation bar, error message, status information in the software can be written in a properties file, and different properties files are written according to different countries. This set of properties files is called a resource bundle .
3.1. Creating resource bundles and resource files
each resource file in a resource bundle must have a common base name. In addition to the base name, the name of each resource file must also have an additional part that identifies its local information . For example: the base name of a resource bundle is "myproperties", then the resource file name corresponding to the Chinese and English environment is: "Myproperties_zh.properties" "Myproperties_en.properties"
Each resource bundle should have a default resource file that does not have an additional part that identifies the local information. If the ResourceBundle object does not find a resource file in the resource bundle that matches the user, it selects the resource file that is closest to the user in the resource bundle and, if it is not found, uses the default resource file. Example: Myproperties.properties
3.2. Writing format of resource files
The contents of the resource file are usually in the form of "keyword = value", and the software is displayed on the page based on the keyword search value . all resource files in a resource bundle must have the same keyword, and the value is the text of the corresponding country.
and the resource file is in the properties format file, so all the characters in the file must be ASCII loadline, the attribute (properties) file cannot be saved in Chinese, for non-acsii characters such as Chinese, must first encode.
For example:
The properties file of the internationalized Chinese environment
The properties file of the International English environment
Java provides a NATIVE2ASCII tool for encoding Chinese characters, and the Native2ascii usage is as follows:
3.3, programming to achieve the internationalization of fixed text
A ResourceBundle class is provided in Javaapi to describe a resource bundle, and the ResourceBundle class provides the appropriate method Getbundle, which can be displayed automatically based on the resource file corresponding to the visitor's country area.
The ResourceBundle class provides a static method getbundle that is used to mount the resource file and create an ResourceBundle instance:
1 Locale currentlocale = Locale.getdefault (); 2 resourcebundle myresources =resourcebundle.getbundle (basename, Currentlocale);
BaseName is the resource packet name (and must be the full path).
If the resource matches the locale object, the Bun class cannot be found. In general, the default resource file is selected for display.
After the resource file is loaded, the program can invoke the GetString method of the ResourceBundle instance object to get the value corresponding to the specified resource information name.
1 String value = myresources.getstring ("key");
Example: automatically obtain the corresponding resource file according to the national area
1 package me.gacl.i18n; 2 3 Import Java.util.Locale; 4 Import Java.util.ResourceBundle; 5/** 6 * @ClassName: I18ntest 7 * @Description: Programming to achieve internationalization of fixed text 8 * @author: Aloof Wolf 9 * @date: 2014-8-29 PM 9:34:0510 *11 */12 public class I18ntest {$ public static void main (string[] args) {15//Resource Packet name (package name +myproperties)-St Ring basename = "Me.gacl.i18n.resource.myproperties"; 17//Set locale locale CN = locale.china;//Chinese 19 Locale US = locale.us;//English 20//load the corresponding language resource file according to the base name and locale ResourceBundle MYRESOURCESCN = RESOURCEBUNDLE.GETBU Ndle (BASENAME,CN);//load myproperties_zh.properties22 resourcebundle myresourcesus = Resourcebundle.getbundle (Basenam E,us);//Load MYPROPERTIES_EN.PROPERTIES23 24//Load the resource file, the program can call the ResourceBundle instance object's GetString method to get the specified resource information name corresponding to the Value. //string value = myresources.getstring ("key"), and the String USERNAMECN = myresourcescn.getstring ("username "); String PASSWORDCN = Myresourcescn.getstring ("password"); string usernameus = Myresourcesus.getstring ("username"); string Passwordus = myresourcesus.getstring ("password"), System.out.println (usernamecn+ "--" +PASSWORDCN); Tem.out.println (usernameus+ "--" +passwordus); 34}35}
Operation Result:
3.4. Realize the internationalization of fixed text in Web application
As shown below:
1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE html> 3
Operation Result:
The browser language is displayed in the Chinese context:
The browser language is displayed in the English environment:
The same page, under different language environment browser display different language text effect, so that the implementation of fixed text internationalization.
IE browser switch language: Tools →internet options
Iv. Internationalization of Dynamic DataData such as values, currencies, time, dates, and so on, can be generated dynamically when the program is running, so it is not possible to separate them from the application as simple as text, but to require special handling. The API classes that address these issues are available in Java (in the Java.util package and the Java.text package)
4.1. Locale ClassThe locale instance object represents a specific geographic, political, and cultural area.
A locale object itself does not verify that the language and country information it represents is correct, but that it provides national area information to locally sensitive classes, and that the formatting and parsing tasks associated with internationalization are done by locally sensitive classes. ( if a class in the JDK needs to adjust its functionality according to the Locale object at run time, this class is called a local sensitive class )
4.2. DateFormat Class (date format)The DateFormat class can format a date/time object as a date/time string that represents a country region.
The DateFormat class, in addition to the format output date by country, defines some constants for the int type that describes the display mode of the date/time, including full, long, MEDIUM, DEFAULT, short, instantiating DateFormat objects, You can use these constants to control the display length of the date/time.
4.2.1, instantiating DateFormat classThere are nine ways to instantiate the DateFormat class, the following three are in the form of parameters, and the three methods listed below can be either without a parameter, or only with a display style.
getdateinstance (int style, Locale Alocale): Obtains the DateFormat instance object with the specified date display mode and local information, which does not process the time value portion.
gettimeinstance (int style, Locale Alocale): Obtains the DateFormat instance object with the specified time display mode and local information, which does not process the date value portion.
getdatetimeinstance (int datestyle, int timestyle, Locale alocale): Obtains DateFormat instance objects in a separate specified date display mode, time display mode, and local information.
Methods for 4.2.2 and DateFormat objectsFormat: Formats the Date object as a string that conforms to the habits of a local environment.
Parse: Parse a string into a date/time object
Note: The parse and format are completely opposite, one is to convert the date time to the corresponding region and country display style, one is to convert the time date of the corresponding region into a Date object, when the method is used, the time or date of parsing should conform to the specified country and region format, otherwise it will throw an exception.
DateFormat objects are usually not thread-safe, and each thread should create its own DateFormat instance object
4.2.3, DateFormat use example 1 package me.gacl.i18n; 2 3 Import Java.text.DateFormat; 4 Import java.text.ParseException; 5 Import Java.util.Date; 6 Import Java.util.Locale; 7 8/** 9 * @ClassName: DateFormatTest10 * @Description: DateFormat class Test One * DateFormat class to format a date/time object as a date/time string representing a country region 12 * @author: Lonely Wolf * @date: 2014-8-29 PM 10:03:2614 *15 * * * public class Dateformattest {] public static void Mai N (string[] args) throws parseexception {th date date = new Date ();//Current time (date, time) 20 21//Output Date part 22 DateFormat df = dateformat.getdateinstance (Dateformat.full,locale.german), and the String result = Df.format (date); SYSTEM.OUT.PRINTLN (result); 25 26//output time Partial df = dateformat.gettimeinstance (Dateformat.full, L Ocale. (china); result = Df.format (date); System.out.println (result); 30 31//Output date and time df = D Ateformat.getdatetimeinstance (Dateformat.short, dateformat.long,locale.china); result = Df.format (date); 34 SYSTEM.OUT.PRINTLN (result); 35 36//parse string backwards into a Date object PNs string s = "10-9-26 02:49 P.M. 53 seconds"; 38 DF = dateformat.getdatetimeinstance (Dateformat.short, Dateformat.long,locale.china); Date d = df.parse (s); 40 System.out.println (d); 41}42}
4.3. NumberFormat class (digital format)The NumberFormat class can format a numeric value string that conforms to a country's local customs, or it can parse a numeric string that conforms to a country's locale to the corresponding numeric value
Methods of the NumberFormat class:
Format method: Formats A numeric value string that conforms to a country's local custom
Parse method: Resolves a numeric string that conforms to a country's local custom to the corresponding numeric value.
When instantiating a NumberFormat class, you can use the locale object as a parameter, or you can not use it, and the following list uses parameters.
- Getnumberinstance (locale locale): Get NumberFormat instance objects with multiple uses as local information identified by the parameter Locale object
- Getintegerinstance (locale locale): Obtains an NumberFormat instance object that processes integers with local information identified by the parameter Locale object
- Getcurrencyinstance (locale locale): Gets the NumberFormat instance object that handles the currency with the local information identified by the parameter Locale object
- Getpercentinstance (locale locale): Gets the NumberFormat instance object that processes the percent value with the local information identified by the parameter Locale object
Example:
1 package me.gacl.i18n; 2 3 import java.text.NumberFormat; 4 import java.text.ParseException; 5 import Java.util.Locale; 6 7/** 8 * @Clas Sname:numberformattest 9 * @Description: NumberFormat class Test * @author: Aloof Wolf * @date: 2014-8-29 PM 10:25:2912 *13 */PU Blic class Numberformattest {public static void Main (string[] args) throws parseexception {+ int price = 89 ; NumberFormat nf = numberformat.getcurrencyinstance (Locale.china); String result = Nf.format (price); System.out.println (result); a String s = "¥89.00"; nf = numberformat.getcurrencyinstance ( Locale.china); n = nf.parse (s), System.out.println (N.doublevalue () + 1), double num = 0.5 ; NF = numberformat.getpercentinstance (); System.out.println (Nf.format (num)); }32}
Operation Result:
4.4. Messageformat (text formatting)If a string contains more than one internationalization-related data, you can use the Messageformat class to batch the data.
For example: at 12:30pm on Jul 3,1998, a hurricance destroyed houses and caused $1000000 of damage
The above string contains several internationalization-related data, such as time, number, and currency, which can be batched using the Messageformat class for its internationalization-related data.
How does the Messageformat class handle batch processing?
The 1.MessageFormat class allows developers to replace sensitive data in strings (that is, internationalization-related data) with placeholders.
2.MessageFormat class when you format the output with text that contains placeholders, the Messageformat class can receive an array of arguments to replace each placeholder in the text.
4.4.1, pattern strings and placeholdersPattern string:
at {0} on {1},a destroyed {2} houses and caused {3} of damage
{0}, {1}, {2}, {3} in the string are placeholders
4.4.2, Format pattern string1. Instantiate the Messageformat object and load the corresponding pattern string.
2. Format the output pattern string using format (object obj[]), specifying a placeholder corresponding to the replacement object in the parameter array.
Example:
1 package me.gacl.i18n; 2 3 import java.text.MessageFormat; 4 import java.util.Date; 5 import Java.util.Locale; 6 7/** 8 * @ClassName: Me Ssageformattest 9 * @Description: Messageformat class Test * @author: Aloof Wolf * @date: 2014-8-29 PM 10:29:1912 *13 */Public CL messageformattest {All public static void Main (string[] args) { //pattern String * string pattern = "on {0}, a Hurricance destroyed {1} houses and caused {2} of damage. "; The Messageformat object is instantiated, and the corresponding pattern string is loaded messageformat format = new Messageformat (pattern, locale.china); 21 Object arr[] = {new Date (),, 100000000};22 //format pattern string, parameter array specifies the placeholder corresponding to the replacement object, string result = Format.format (arr); System.out.println (result); }26}
Operation Result:
Three ways to write 4.4.3 and placeholders{Argumentindex}: A number between 0-9 that represents the index number of the object data to be formatted in the parameter array
{argumentindex,formattype}: Format type of parameter
{Argumentindex,formattype,formatstyle}: A formatted style whose value must be a legitimate pattern that matches a formatted type, or a string representing a legal pattern.
Example:
1 package me.gacl.i18n; 2 3 import java.text.MessageFormat; 4 import java.util.Date; 5 import Java.util.Locale; 6 7/** 8 * @ClassName: Me Ssageformattest 9 * @Description: Messageformat class Test * @author: Aloof Wolf * @date: 2014-8-29 PM 10:29:1912 *13 */Public CL messageformattest {All public static void Main (string[] args) { //pattern String * string pattern = "at {0, Tim E, short} on {0, date}, a destroyed {1} houses and caused {2, number, currency} of damage. "; The Messageformat object is instantiated, and the corresponding pattern string is loaded messageformat format = new Messageformat (pattern, locale.us); Object arr[] = {new Date (),, 100000000};22 //format pattern string, parameter array specifies the placeholder corresponding to the replacement object, string result = Format.format (arr) ; System.out.println (result); }26}
Operation Result:
The internationalization of fixed text using internationalized Tag library in Web application 1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <%--Import internationalized Tag Library--%> 3 <% @taglib ur I= "http://java.sun.com/jsp/jstl/fmt" prefix= "FMT"%> 4 <! DOCTYPE html> 5 The above is the summary content of internationalization in javaweb development.
Javaweb Study Summary (31)--Internationalization (i18n)