Java Learning Notes-internationalization (41)

Source: Internet
Author: User
Tags dateformat locale

Internationalization: Internationalization is i18n.

Example:

The website of the undergraduate university, generally have Chinese and English two kind of page style. Therefore, it is called the internationalization of the page according to different user groups to display different page styles.

Translation VS Internationalization

Translation: Chrome

Internationalization: The main thing is to display the information of the internationalization of the page, as for other resources such as documents can not be internationalized.

1 Principles of internationalization

    1. Get local information for a user

Locale Gets the zone object through a static property.

    1. Resources to prepare for the needs of countries in different regions

All implemented using the properties configuration file. However, the file name needs to be written according to the specified rules.

Specification: Basic Name _ language name. Properties

The basic name is arbitrary, the language name is fixed can refer to the browser language.

    1. Find the specified resource for display based on regional language information

static method of the ResourceBundle resourcebundle getbundle (String baseName,

Locale locale)

The principle of binding resources: Gets the resource base name and the zone language name lock resource name.

String getString (String key)

One experience

1. Write two of the following resource files

Greet_zh.properties

hello=\u4f60\u597d

Greet_en.properties

Hello=hello

2. Implement one of the following Java classes

 Public Static void Main (string[] args) {    //  get local area information    locale locale = locale.us;     // Binding Resources    ResourceBundle bundler = Resourcebundle.getbundle ("cn.itcast.ps.greet", locale);    System. out. println (bundler.getstring ("hello"));  

Summarize:

If you do not have a visual editor, then use Native2ascii.exe to transcode the Chinese language directly.

Internationalization variables

1st period Internationalization

DateFormat This class mainly provides a different date format for displaying different regions.

Format and parse date data based on region information.

static DateFormat getdateinstance (int style, Locale alocale) à get a date instance

static DateFormat gettimeinstance (int style, Locale alocale) à get time instance

static DateFormat getdatetimeinstance (int datestyle, int timestyle, Locale alocale) à get date and time

Date Parse (String source) à parse date

String format (Object obj) à formatted date

Example:

Get Date and time

Long2013 January 21 04:16 P.M. 41 seconds January +, - 4: -: -PM CSTFULL2013 January 21 Monday 04:18 P.M. 19 seconds cstmonday, January +, - 4: -:Panax NotoginsengPM Cstmedium --1- +  -: +: -Jan +, - 4: +: $Pmshort --1- +PM 4: -1/ +/ - 4: -Pm

Exercise: Parse the following string of characters into a Date object.

String date = "January 21, 2013 4:20"; resolves to a Date object.

Long Short

 Public Static void Main (string[] args) throws exception{        //i18ndate ();        " January 21, 2013 PM 4:20 " ;         = Locale.china;         = dateformat.getdatetimeinstance (dateformat.long, Dateformat.short, locale);         = Format.parse (date);        System.  out . println (new_date);    }

Digital internationalization

NumberFormat

Static NumberFormat getinstance (Locale inlocale)              ? International General Digital static  numberformat getcurrencyinstance (Locale inlocale)     ? International currency static NumberFormat getpercentinstance (Locale inlocale)        ? Percentage of internationalization

Internationalization

string format (Object obj) number parse (string source)

Example:

  Public Static voidMain (string[] args) throws exception{//get an internationalized General number of objectsLocale locale =Locale.china; NumberFormat format=numberformat.getinstance (locale); String Str= Format.format (123456.789); System. out. println (str); Locale=Locale.china; Format=numberformat.getcurrencyinstance (locale); STR= Format.format (123456.789); System. out. println (str); Locale=Locale.china; Format=numberformat.getpercentinstance (locale); STR= Format.format (0.789); System. out. println (str); }

Exercise: String str = "¥123.7"; parse to a number.

 Public Static void Main (string[] args) throws exception{        //i18nnumber ();        " ¥123.7 " ;         = Locale.china;          = numberformat.getcurrencyinstance (locale);         = Format.parse (str);        System. out. println (Num.doublevalue () *3);    

Dynamic text Internationalization

 Public Static voidMain (string[] args) throws exception{String str="On {0}, a hurricance destroyed {1} houses and caused {2} of damage."; String Date="Jul 3, 1998"; DateFormat format=dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.short, locale.us); Date new_date=format.parse (date); Double Money=NewDouble (1E7); NumberFormat numfor=numberformat.getcurrencyinstance (Locale.china); String New_str=Numfor.format (Money); object[] OS=NewObject[]{new_date,NewInteger ( the), new_str} messageformat msg_for=NewMessageformat (Str,locale.china); String For_str=Msg_for.format (OS); System. out. println (FOR_STR); }

The above dynamic text internationalization every time need to internationalization of all the data separately to be filled in one after the other. It is therefore cumbersome to use the following pattern strings.

 Public Static voidMain (string[] args) throws exception{String pattern="At {0, time, short} on {0, date}, a hurricance destroyed"+"{1} houses and caused {2, number, currency} of damage."; String datetimestring="Jul 3, 1998"; DateFormat format=dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.short, locale.us); Date new_date=Format.parse (datetimestring); object[] OS=NewObject[]{new_date,NewInteger ( the),NewDouble (1E7)}; Messageformat msg_for=NewMessageformat (pattern,locale.us); String For_str=Msg_for.format (OS); System. out. println (FOR_STR); }

Case one: Realizing the internationalization of landing
<body> <% @taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="inch"%> <inch: SetLocale value="${pagecontext.request.locale}"/> <inch: Setbundle basename="Cn.itcast.login.greet" var="Bund"Scope="page"/> <form action=""> <table align="Center"Border="1"> <tr> <td><inch: Message bundle="${pagescope.bund}"key="name"/></td> <td><input type="text"Name="uname"></td> </tr> <tr> <td><inch: Message bundle="${pagescope.bund}"key="PSW"/></td> <td><input type="text"Name="uname"></td> </tr> <tr> <td colspan="2"> <input type="Submit"Value='<in:message bundle= "${pagescope.bund}" key= "Submit"/>'> <input type="Reset"Value='<in:message bundle= "${pagescope.bund}" key= "Reset"/>'> </td> </tr> </table> </form> </body>

Later in the struts2.x, you need to use the internationalization label of struts.

Java Learning Notes-internationalization (41)

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.