Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Internationalization, called too long a word for internationalization, also known as I18n (with 18 letters in the middle) without Shouting it up! Brainwave, I can also be referred to as more than 6 road! Ok, moving on to internationalization, which is the ability of a product or program to adapt to the needs of different languages and regions without having to change. The same greeting in China you say “Hello”, in America you say “Hello”, you see, you are i18N.

Internationalization in Java is achieved mainly through a utility class, ResourceBundle. The core idea is to provide a different resource file for different locales. So let’s look at how resource files are defined.

To define the corresponding resource files, we put the resource files in a resource bundle (just a regular bundle), and each resource file in a resource bundle must have a common base name. In addition to the base name, each file name must have an additional part that identifies its local information. For example, if the base name of a resource package is “message”, the file names corresponding to the Chinese (China) and English (United States) environment are:

message_zh_CN.properties

message_en_US.properties

It should be noted here that the resource file usually adopts the form of key-value pairs, and the resource file adopts the format of Properties file, so all characters in the file must be ASCLL code and cannot be saved in Chinese. The Native2ASCLL tool is provided in Java to convert Chinese into ASCLL code. So when you write the properties file, you write it in Chinese, and the car is automatically coded.

The Java API provides a ResourceBundle class that describes a ResourceBundle, and the ResourceBundle class provides the corresponding static method getBundle. This method automatically binds the corresponding resource files based on the country and region of the visitor.

import java.util.Locale;
import java.util.ResourceBundle;

public class I18n{

    public static void test(a) {
        // Set the custom language country code
        Locale locale1 = new Locale("en_US");
        Locale locale2 = Locale.getDefault();
        // Get the resource file
        ResourceBundle rb = ResourceBundle.getBundle("message.i18n.message", locale2);
        // Get the corresponding key value
        String greeting = rb.getString("greeting");
        String userInfo = rb.getString("name");

        System.out.println(greeting);
        System.out.println(userInfo); 
    }    

    public static void main(String[] args) { test(); }}/ / how are you
/ / 6 road

Copy the code

The above is the internationalization of some fixed text, including menu name, navigation bar, system prompt, etc., which are manually configured in the Properties file. However, some data, such as value, currency, time, date, etc., may be generated dynamically when the program is running. So they can’t be separated out of an application as easily as text, and require special processing. Java provides apis to address these issues.

Just a quick word about the Locale class, which looks like this in English

A Locale object represents a specific geographical , political , or cultural region . An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user .

This means that a Locate object represents a specific geographic, political, or cultural area. Operations that need to Locate to set up tasks are called sensitive areas (classes that contain Locate are sensitive areas). You can use Locate to customize information for the user.

Take a look at the structure diagram of the DateFormat utility class used for internationalization of dates.

DateFormat has many different constructors, and you can specify different time display modes in the constructors. I won’t go through them all, but the sample code is shown below.

public class DateFormatTest {

    public static void main(String[] args) throws ParseException {

        Date date = new Date();

        DateFormat df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINA);
        String str = df.format(date);
        System.out.println(str); / / 2018-8-6

        df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.CHINA);
        System.out.println(df.format(date)); // 7:58:26 p.m

        df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, Locale.CHINA);
        System.out.println(df.format(date)); // Monday, August 6, 2018 7:58 PM

        // Get the default DateFormat
        df = DateFormat.getInstance();
        System.out.println(df.format(date)); // 18-8-6 7:58 p.m

        // Format a string into a date object using DateFormat in reverse
        String dateString = "Monday, August 6, 2018 7:58 PM";
        DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, Locale.CHINA);
        Date date2 = df2.parse(dateString); // There may be an exception, if the format of the string is inconsistent with the format set by DateFormat.
        System.out.println(date2); // Mon Aug 06 19:58:00 CST 2018}}Copy the code

Dynamic text internationalization uses the utility class MessageFormat, which allows developers to replace sensitive data in strings (that is, internationalization-related data) with placeholders such as {0} {1} {2} {3} in strings.

public class MessageFormatTest {
    public static void main(String[] args) {
        Pattern ={0},{1},your age is {2}. */
        ResourceBundle rb = ResourceBundle.getBundle("message.i18n.message_en_US",Locale.US);
        String greeting = rb.getString("greeting");
        String name = rb.getString("name");
        String age = rb.getString("age");

        // Get the pattern string
        String pattern = rb.getString("message");

        // Instantiate the MessageFormat object and load the corresponding schema string
        MessageFormat format = new MessageFormat(pattern);
        The format method requires an array of objects.
        Object[] params = {greeting, name, age};
        // Format pattern string, parameter array to specify placeholder corresponding replacement objectString string = format.format(params); System.out.println(string); }}//Welcome,YJK,your age is 18.

Copy the code

The NumberFormat class can format a numeric value into a numeric string that conforms to the conventions of a country or region, or parse a numeric string that conforms to the conventions of a country or region into corresponding values. The corresponding methods are Format and parse.

public class NumberFormatTest {
    public static void main(String[] args) throws ParseException{

        int price = 18;
        // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA); RMB 18.00 / /
        // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); / / $18.00
        // NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.JAPAN); / / RMB 18
        // Get the NumberFormat instance object that handles the currency
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE); / / 18, 00 euro
        System.out.println(nf.format(price));

        String s = "18, 00 euro";
        nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);

        Parsing 18,00 €in French currency yields 18.0
        System.out.println(nf.parse(s).doubleValue()); 

        double d = 0.1;
        // Get the NumberFormat instance object that handles integers.
        nf = NumberFormat.getIntegerInstance(); / / 0

        // Get the NumberFormat instance object that handles percentage values
        nf = NumberFormat.getPercentInstance();

        System.out.println(nf.format(d)); / / 10%}}Copy the code

Finally: Welcome to like the comments, we study together to discuss.