Introduction to the concept and use of Java internationalization

Internationalization is a basic requirement of commercial software system, because today's software system needs to face global browsers. The purpose of internationalization is to output corresponding pages to users according to their different language environments to show friendliness.

Program internationalization has become the basic requirement of Web applications. With the development of the network, most Web sites are no longer facing local or domestic visitors, but visitors from all over the world. Therefore, internationalization has become an indispensable part of Web applications.

The idea of Java internationalization is to put the information in the program in the resource file, and the program reads the corresponding resource file according to the supported country and language environment. Resource files are key value pairs. The key in each resource file is unchanged, but the value varies with different countries / languages.

The internationalization of Java programs is mainly completed through two classes.

1)java.util.Locale

It is used to provide local information, which is often called Locale. Different languages, countries and regions are represented by different Locale objects.

2)java.util.ResourceBundle

This class is called a resource bundle and contains locale specific resource objects. When a program needs a locale specific resource (such as a string resource), the program can load it from a resource package suitable for the current user's locale. In this way, the program code can be written independently of the user's language environment, and the information related to the specific language environment is provided through the resource package.

In order to realize the internationalization of Java programs, the resource files required by the program must be provided in advance. The content of the resource file consists of many key value pairs, in which key is the part used by the program and value is the display of the program interface.

Resource files can be named in the following three forms:

  • baseName.properties.
  • baseName_language.properties.
  • baseName_language_country.properties.

baseName is the basic name of the resource file, which is freely defined by the user, but the language and country must be the language and country code supported by Java. For example:

  • Chinese mainland: baseName_zh_CN.properties.
  • USA: baseName_en_US.properties.

The resource file in Java only supports characters in ISO-8859-1 encoding format, and there will be garbled code when writing Chinese directly. Users can use the Java command native2ascii Exe solves the problem of Chinese garbled code of resource files. MyEclipse is used to write the resource attribute file. When saving the resource file, MyEclipse automatically executes native2ascii Exe command, so there will be no Chinese garbled code problem in the resource file in MyEclipse.

Languages and countries supported by Java

java. util. The common construction methods of locale class are as follows:

public Locale(String language);
public Locale(String language, String country). 

Among them, language represents language, and its value is a language code composed of two lowercase letters. Country refers to the country or region. Its value is the country or region code composed of two uppercase letters.

In fact, Java cannot support all countries and languages. If you need to obtain the languages and countries supported by Java, developers can obtain them by calling the getAvailableLocales method of the Locale class, which returns a Locale array containing the languages and countries supported by Java.

The following Java program simply demonstrates how to obtain the countries and languages supported by Java:

import java.util.Locale;
public class Test {
    public static void main(String[] args) {
        // Returns an array of languages and countries supported by Java
        Locale locales[] = Locale.getAvailableLocales();
        // Traverse the array elements to get the supported countries and languages in turn
        for (int i = 0; i < locales.length; i++) {
            // Print out the supported countries and languages
            System.out.println(locales[i].getDisplayCountry() + "="
                    + locales[i].getCountry() + ""
                    + locales[i].getDisplayLanguage() + "="
                    + locales[i].getLanguage());
        }
    }
}

Internationalization of Java programs

Suppose you have the following simple Java program:

public class TestI18N {
    public static void main(String[] args) {
        System.out.println("I want to say hello to the people of different countries: Hello!");
    }
}

In order for the program to support internationalization, it needs to say "I want to say hello to the people of different countries: Hello!" Strings corresponding to different locales are defined in different resource files.

Create a new file messageresource in the src directory of the Web application_ zh_ CN. Properties and messageResource_ en_US.properties. Then give the resource file messageresource_ zh_ CN. Add "Hello = I want to say hello to the people of different countries: Hello!" After saving the content, you can see the effect shown in Figure 1.


The content shown in Figure 1 seems to be a lot of garbled code, but it is actually the content of Unicode encoded file. At this point, the resource file messageResource_zh_CN.properties creation completed.

Finally, give the resource file messageResource_en_US.properties add "hello=I want to say hello to all world!" Content.

Now test I18N The Java program is modified as follows:

import java.util.Locale;
import java.util.ResourceBundle;
public class TestI18N {
    public static void main(String[] args) {
        // Get the default national language environment of the system
        Locale lc = Locale.getDefault();
        // Load the resource file according to the national locale
        ResourceBundle rb = ResourceBundle.getBundle("messageResource", lc);
        // Print out the information obtained from the resource file
        System.out.println(rb.getString("hello"));
    }
}

The print statement in the above program prints the information read from the resource file. If you run the program in a Chinese environment, it will print "I want to say hello to the people of different countries: Hello!".

If you set the computer's locale to the United States in the control panel, and then run the program again, you will print "I want to say hello to all world!". It should be noted that if the program cannot find the resource file of the corresponding country / language, what should the system do?

Suppose you take the simplified Chinese environment as an example, first search the following files:

messageResource_zh_CN.properties

If no resource file matching the country / language is found, then search the language matching file, that is, search the following files:

messageResource_zh.properties

If the above file has not been found, search the file matching baseName, that is, search the following file:

messageResource.properties

If none of the above three files can be found, an exception will appear.

Internationalization information with placeholder

In the resource file, the message text can have parameters, such as:

welcome={0},Welcome to study Spring MVC. 

The number in curly braces is a placeholder that can be replaced by dynamic data. In the message text, the placeholder can use numbers from 0 to 9, that is, the maximum number of parameters of the message text can be 10. For example:

welcome={0},Welcome to study Spring MVC,Today is Sunday{1}. 

If you want to replace the placeholder in the message text, you can use Java text. Messageformat class, which provides a static method format for formatting text with parameters. The format method is defined as follows:

public static String format(String pattern,Object ...arguments)

Among them, the pattern string is a string with placeholders, and the number placeholders in the message text will be replaced in the order of the method parameters (starting from the second parameter).

Example code for replacing placeholders is as follows:

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class TestFormat {
    public static void main(String[] args) {
        // Get the default national language environment of the system
        Locale lc = Locale.getDefault();
        // Load the resource file according to the national locale
        ResourceBundle rb = ResourceBundle.getBundle("messageResource", lc);
        // Information obtained from resource files
        String msg = rb.getString("welcome");
        // Replace the placeholders in the message text, and the number placeholders in the message text will be in the order of the parameters
        // (starting from the second parameter) is replaced, that is, "I" replaces {0}, "5" replaces {1}
        String msgFor = MessageFormat.format(msg, "I", "5");
        System.out.println(msgFor);
    }
}

Added by l053r on Sat, 19 Feb 2022 22:14:08 +0200