Internationalization of java development

Posted by Magena Cadmar on Thu, 11 Jul 2019 01:53:19 +0200

1 The internationalization of static text, for example, the Chinese display of user name is user name, which is used to display user name.

Among them, static file naming follows: basic name language abbreviation country abbreviation. properties

The classes to be used are

1) import java.util.Locale; //localization
2) import java.util.ResourceBundle; //resource loading

For how to find language abbreviations and national abbreviations, we can find language through the internate option. As shown in the following figure

 

Examples are as follows:

Create two new properties files to store the corresponding internationalized values

The msg_zh_CN.properties file reads as follows

 

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"));

 

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

The main class objects are NumberFormat, SimpleDateFormat

2.1 Currency internationalization, code as follows

2.1.1) Format digital money into strings

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

2.1.2) Format strings into numbers, coded as follows

        Locale locale = Locale.CHINA;
        String strNumber = "¥200.00";
        //Monetary 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 digital money into strings

2.2.1) Number formatted into strings

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

Results: 2,000,000.15

2.2.2) Format strings into numbers

        Locale locale = Locale.CHINA;
        //Digital 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();
        }

Results: 2000 000.15

 

There are two ways to internationalize the 2.3-day period.

2.3.1) Using DateFormat, 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);

RESULTS: CST at 08:51:28 a.m. from April to November 2017

 

2.3.2) SimpleDateFormat is used for formatting, which can produce a specified format and is more autonomous.

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

Results: 2017-04-11 08:55:20

The formats available are 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

Topics: Java