1 The idea of Java internationalization
The idea of the internationalization of Java programs is to put the tags, prompts and other information in the program in the resource file, the program needs to support which country, language environment, corresponding to provide the corresponding resource files. The resource file is a Key-value pair, and the key in each resource file is constant, but value changes with different countries and languages.
the internationalization of Java programs is mainly accomplished by the following three classes:
? Java.util.ResourceBundle: Used to load a country, language resource pack.
? Java.util.Locale: Used to encapsulate a specific country/region, locale.
? Java.text.MessageFormat: Used to format a string with placeholders.
In order to achieve the internationalization of the program, you must first provide the resource files required by the program. The contents of the resource file are many key-value pairs. Where key is the part used by the program, and value is the display string of the program interface.
The name of the resource file can be in the following three ways:
? BaseName _ Language _country.properties
? BaseName _language.properties
? Basename.properties
Where basename is the basic name of the resource file, which the user can freely define. and language and country are not arbitrary changes, must be the language and country supported by Java.
2 languages and countries supported by Java
in fact, Java cannot support all countries and languages, such as the need to get the languages and countries supported by Java, which can be obtained by invoking the Getavailablelocale method of the Locale class, which returns a locale array that contains the languages and countries supported by Java.
The following program simply demonstrates how to get the countries and languages supported by Java:
public class Localelist
{
public static void Main (string[] args)
{
returns an array of all the countries and languages supported by Java
locale[] localelist = Locale.getavailablelocales ();
iterate through each element of the array, getting the supported countries and languages in turn
for (int i = 0; i < localelist.length; i++)
{
print out the supported countries and languages
System.out.println (Localelist[i].getdisplaycountry () + "=" + locale
List[i].getcountry () + "" + localelist[i].getdisplaylanguage ()
+ "=" + Localelist[i].getlanguage ());
}
}
}
With this program, we can get the country/locale supported by the Java program.
3 Complete Program internationalization
For the simplest of these programs:
public class Rawhello
{
public static void Main (string[] args)
{
System.out.println ("Hello World");
}
}
The implementation of this program is also simple: it must be a simple "Hello world" string, no matter where it is executed, there will be no change! In order for the program to support internationalization, it is certainly not possible for the program to output the "Hello World" string, which directly outputs a string constant, never any change. In order for the program to output different strings, the string constant must never be used here.
In order for the string constants above to be changed, we will need to export the various strings (different countries/locales corresponding to different strings) defined in the resource bundle.
We provide the following two files for the above program:
First file: Mess_zh_cn.properties, the contents of the file are:
# The contents of the resource file are key-value pairs.
hello= Hello!
The second file: Mess_en_us.properties, the contents of the file are:
# The contents of the resource file are key-value pairs.
Hello=welcome you!
For resource files that contain non-Western European characters, JAVA provides a tool to process the file: Native2ascii, which can be found under the%java_home%/bin path. The syntax format for using the tool is as follows:
Native2ascii Source Resource File Destination resource file
If we enter the following instruction in the command window:
# Use the NATIVE2ASCII command to process mess_zh_cn.properties files, generate aa.properties files
Native2ascii mess_zh_cn.properties aa.properties
The above command will generate a aa.properties file, which is the resource file we need, it seems to contain a lot of garbled characters, in fact, non-western character Unicode encoding method, this is completely normal. Rename the file to Mess_zh_cn.properties.
We see that the basename of the two file names are the same: mess. The three naming methods for resource files have been described earlier, where the countries and languages behind basename must be the country and language combinations supported by Java.
put the above Java program modified to the following form:
public class Hello
{
public static void Main (string[] args)
{
get the system default country/locale
Locale Mylocale = Locale.getdefault ();
load a resource file according to a specified country/locale
ResourceBundle bundle = Resourcebundle.getbundle ("mess", Mylocale);
print a message from a resource file
System.out.println (bundle.getstring ("Hello"));
}
}
the printed statement in the above program is no longer a direct print of the "Hello World" string, but instead prints the information read from the resource bundle. If you run the program in a Chinese environment, it will print "Hello!" "If we set the locale of the machine to United States in the Control Panel and then run the program again, the" Welcome you! "will be printed string.
Through the simple procedure above, we can realize how simple the internationalization of Java program is!
From the above program can be seen: if we want to complete the internationalization of the program, we only need to separate the different countries/languages (locale) information in different files. For example, the language resource file for Simplified Chinese is the xxx_zh_cn.properties file, and the U.S. English language resource file is the Xxx_en_us.properties file.
the key class for the internationalization of Java programs is ResourceBundle, which has a static method:Getbundle (String baseName, locale locale); the method will load the resource file according to locale, while locale encapsulates a country, language, such as Simplified Chinese environment can be represented in Simplified Chinese locale, the U.S. English environment can be represented in the U.S. English locale.
From the name of the resource file above, it can be seen that the basename of the resource files of different languages and national environment is the same, that is, there are many resources files for mess, and different countries and languages have different resource files.
For example, use the following code to load a resource file:
load a resource file according to a specified country/locale
ResourceBundle bundle = Resourcebundle.getbundle ("mess", Mylocale);
The above code will load one of the series resource files that basename to mess , which is loaded, depending on mylocale, and themess_zh_cn.properties file is loaded for the Simplified Chinese locale.
Once the file is loaded, the contents of the resource file are multiple Key-value pairs, and the program obtains the specified information based on key, such as getting a message with a key of Hello, the message is "Hello!" "-This is the process of internationalization of Java programs.
If the locale for US English is loaded with mess_en_us.properties, the message that the key in the file is Hello is "Welcome you!".
the key class for the internationalization of Java programs is resourcebundle and locale,resourcebundle to load language resource files according to different loacle, and then obtain the strings in the loaded language resource file according to the specified key.
Internationalization for Java