Java Development is internationalized, java Development is internationalized

Source: Internet
Author: User
Tags dateformat iso 8601

Java Development is internationalized, java Development is internationalized

 

1. The static text is internationalized. For example, the user name displayed in Chinese on the page is the user name, and the user name is used for display.

The static file naming rules are as follows: Basic name_language abbreviation _ country abbreviation. properties

The class to be used is

1) import java. util. Locale; // localized
2) import java. util. ResourceBundle; // resource Loading

You can use the internate option to find the language Abbreviation and country abbreviation. As shown in

 

Example:

Create two properties file storage corresponding to the value to be internationalized

The content of the msg_zh_CN.properties file is as follows:

 

The content of the msg_en_US.properties file is as follows:

The test code is as follows:

String baseName = "com.huitong.test.bundle.msg";            ResourceBundle bundle = ResourceBundle.getBundle(baseName, Locale.CHINA);        System.out.println(bundle.getString("username"));

 

2. Internationalization of dynamic content: mainly including digital internationalization, currency internationalization, and date Internationalization

The main class objects are NumberFormat and SimpleDateFormat.

The code for internationalization of 2.1 currencies is as follows:

2.1.1) format the digital currency into a string

Locale locale = Locale. CHINA; double number = 200; // currency internationalization NumberFormat currencyInstance = NumberFormat. getCurrencyInstance (locale); String result = currencyInstance. format (number); System. out. println (result );

2.1.2) format the string into a number. The Code is as follows:

Locale locale = Locale. CHINA; String strNumber = "¥200.00"; // currency internationalization NumberFormat currencyInstance = NumberFormat. getCurrencyInstance (locale); Number result; // String result = currencyInstance. format (dnumber); try {result = currencyInstance. parse (strNumber); System. out. println (result. doubleValue ();} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();}

 

2.2 digital Internationalization

Format the digital currency into a string

2.2.1) format the number into a string

Locale locale = Locale. CHINA; // number internationalization NumberFormat numberInstance = NumberFormat. getNumberInstance (locale); double num = 2000000.15; String strnum = numberInstance. format (num); System. out. println (strnum );

Result: 2,000,000.15

2.2.2) format the string into a number

Locale locale = Locale. CHINA; // number internationalization NumberFormat numberInstance = NumberFormat. getNumberInstance (locale); // double num = 2000000.15; String strnum = "2,000,000.15"; try {Number result = numberInstance. parse (strnum); System. out. println (result. doubleValue ();} catch (ParseException e) {// TODO Auto-generated catch block e. printStackTrace ();}

Result: 2000000.15

 

2.3 There are two ways to internationalize Date:

2.3.1) When DateFormat is used, the generated formats are fixed, including DateFormat. FULL, DateFormat. LONG, DateFormat. MEDIUM, DateFormat. SHORT.

        int dateStyle = DateFormat.MEDIUM;                int timeStyle = DateFormat.FULL;                Locale aLocale = Locale.CHINA;                DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(dateStyle , timeStyle , aLocale );                String result = dateTimeInstance.format(new Date());                System.out.println(result);

Result: 08:51:28 A.M. CST

 

2.3.2) The SimpleDateFormat format can be used to produce the format specified, which is more autonomous.

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println(simpleDateFormat.format(new Date()));

Result: 08:55:20

The format is as follows:

  •    
       
    Letter Date or Time Component Presentation Examples
    G Era designator Text AD
    y Year Year 1996; 96
    Y Week year Year 2009; 09
    M Month in year (context sensitive) Month July; Jul; 07
    L Month in year (standalone form) Month July; Jul; 07
    w Week in year Number 27
    W Week in month Number 2
    D Day in year Number 189
    d Day in month Number 10
    F Day of week in month Number 2
    E Day name in week Text Tuesday; Tue
    u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
    a Am/pm marker Text PM
    H Hour in day (0-23) Number 0
    k Hour in day (1-24) Number 24
    K Hour in am/pm (0-11) Number 0
    h Hour in am/pm (1-12) Number 12
    m Minute in hour Number 30
    s Second in minute Number 55
    S Millisecond Number 978
    z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
    Z Time zone RFC 822 time zone -0800
    X Time zone ISO 8601 time zone -08; -0800; -08:00

 

3. International tag

Introduce internationalization labels: <% @ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>

5 main labels

// Set the localization object as the locale requested by the browser

<Fmt: setLocale value = "$ {pageContext. request. locale}"/>

// Set the tool class

<Fmt: setBundle basename = "com. huitong. i18n. msg" var = "bund"/>

// Obtain the data in the tool class. The key is the key in the properties file.

<Fmt: message bundle = "$ {bund}" key = "title"> </fmt: message>

<Fmt: formatNumber pattern = "0.00" value = "100"> </fmt: formatNumber>

<Fmt: formatDate pattern = "yyyyMMdd" value = "<% = new Date () %>"/>

 

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.