Java Web Development: Software Internationalization (Dynamic Element Internationalization)

Posted by Helaman on Mon, 26 Aug 2019 07:57:28 +0200

The second part of software internationalization is dynamic element internationalization.

  • Data such as values, currencies, times, dates and so on can not be separated from the application simply like words because they may be generated dynamically while the program is running, but need special processing. API classes (in the java.util package and the java.text package) are provided in Java to solve these problems.
  • Locale class
    Locale instance objects represent a specific geographical, political, and cultural region.
    A Locale object itself does not verify whether the language and country information it represents are correct. It only provides country information to locally sensitive classes. Formatting and parsing tasks related to internationalization are performed by locally sensitive classes. (If a class in JDK needs to adjust its functionality according to the Locale object at run time, this class is called a local sensitive class).

Next, I will introduce some of the more commonly used API s.

DateFormat class (internationalization date)

The DateFormat class can format a date/time object into a date/time string representing a country area.

In addition to formatting output dates by country, the DateFormat class also defines int like constants for describing date/time display patterns, including FULL, LONG, MEDIUM, DEFAULT, SHORT, which can be used to control the display length of date/time when instantiating DateFormat objects.

There are nine ways to instantiate the DateFormat class. The following three ways are parameterized. The three ways listed below can also be parameterized without or with only the parameters of the display style.

  • getDateInstance(int style, Locale aLocale): Gets the DateFormat instance object with the specified date display mode and local information, which does not process the time value part.
  • getTimeInstance(int style, Locale aLocale): Gets the DateFormat instance object with the specified time display pattern and local information, which does not process the date value part.
  • GetDateTime Instance (int date Style, int time Style, Locale aLocale): Get the DateFormat instance object with the date display mode, time display mode and local information specified separately.

Write test code

    @Test
    public void demo1(){
        // Format the date with its own style
        // Date only
        Date date = new Date();
        // There are four display modes FULL, LONG, MEDIUM, SHORT
        // DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL);
        // DateFormat df1 = DateFormat.getDateInstance(DateFormat.LONG);
        // DateFormat df1 = DateFormat.getDateInstance(DateFormat.MEDIUM);
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
        System.out.println(df1.format(date));

        // As long as time
        DateFormat df2 = DateFormat.getTimeInstance(DateFormat.FULL);
        // DateFormat df2 = DateFormat.getTimeInstance(DateFormat.LONG);
        // DateFormat df2 = DateFormat.getTimeInstance(DateFormat.MEDIUM);
        // DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);
        System.out.println(df2.format(date));
        
        //Date and time
        DateFormat df3 = DateFormat.getDateTimeInstance();
        System.out.println(df3.format(date));
    }

Running results I will not map, you run your own feelings.
It's all about the application of API. There's nothing to say. Just paste some test code to understand the use of API.

    @Test
    public void demo2(){
        //Setting Locale
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CANADA);
        System.out.println(dateFormat.format(date));
    }

NumberFormat class (internationalized data)

  • When instantiating the NumberFormat class, you can use the locale object as a parameter or not, as listed below.
  • getNumberInstance(Locale locale): Get a NumberFormat instance object with multiple uses using the local information identified by the parameter locale object.
  • getIntegerInstance(Locale locale): Gets the NumberFormat instance object that handles integers with the local information identified by the parameter locale object.
  • getCurrencyInstance(Locale locale): Get 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 handles percentage values with the local information identified by the parameter locale object.

Write test code

    @Test
    public void demo3() {
        // Keep the decimal significant digits
        double d = 1.23456789456789;
        // Keep two decimal places
        NumberFormat format = NumberFormat.getNumberInstance();
        // Maximum two decimal digits
        format.setMaximumFractionDigits(2);
        // Minimum two decimal places
        format.setMinimumFractionDigits(2);
        System.out.println(format.format(d));

        // CurrencyFormatter
        int i = 100;
        // Display US Dollars
        NumberFormat format2 = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println(format2.format(i));

        // Percentage formatting
        double d2 = 0.78123;
        NumberFormat format3 = NumberFormat.getPercentInstance();
        System.out.println(format3.format(d2));

        // Want to show two decimal digits
        format3.setMaximumFractionDigits(2);
        format3.setMinimumFractionDigits(2);
        System.out.println(format3.format(d2));
    }

At this point, the internationalization of dynamic elements is introduced. They are all knowledge points that need to be remembered. In fact, there is nothing to say. Next is the last part of software internationalization, dynamic text internationalization.

Topics: Java JDK