Java Learning Note 12--internationalization
Internationalized operation refers to a program can adapt to multiple languages at the same time, that is: If the program is Chinese, it will be displayed in Chinese language, if the user is British, the program will be displayed in English text, that is, through international operation, so that a program to adapt to the language requirements of each country.
The program finds different resource files according to different language environment, then extracts the contents from the resource files, and the contents of the resource files are saved in the form of key->value, so the corresponding value is found by its key when reading.
Support classes for internationalization implementations
If you want to implement the internationalization of a Java program, you must complete the following three classes:
Java.util.Locale: Used to represent a national language class.
Java.util.ResourceBundle: Used to access resource files.
Java.text.MessageFormat: A placeholder string that formats the resource file.
Local class
Local is represented locally and is actually using an ISO-encoded encapsulation class. For each country there is a unique code, then this code is called the ISO code, using local can specify a specific country code.
For example: China's code: ZH_CH
English-American code: en-US
Code for French: FR-FR
ResourceBundle
This class is specialized to complete the property file read operations, read the time to specify the name of the file name (which generally does not need to specify a suffix, the suffix is agreed to *.properties), you can automatically select the required resource files based on locale code specified by locale.
public static final ResourceBundle Getbundle (String baseName), which is the specified resource file to be manipulated, and this method finds the default operating system's language locale object
public static final ResourceBundle getbundle (String basename,locale locale), which is also the resource file for the specified operation, and passes in the locale object.
Public final string getString (string key) to obtain the corresponding value from key
The following is a program to observe the use of resource files, and how to use ResourceBundle to read resource files.
Message.properties
[Java]View Plaincopy
- Info=hello
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.util.ResourceBundle;
- Public class T {
- public static void Main (string[] args) throws exception{
- ResourceBundle RB = Resourcebundle.getbundle ("message");
- System.out.println (rb.getstring ("info"));
- }
- }
PS:message.properties The default lookup path is classpath, if not under this path and for the specified full package path name, the following exception will be reported
[Java]View Plaincopy
- Can ' t find bundle for base name message, Locale zh_ch
Java Internationalization Program implementation
You can export different countries according to different countries Hello:
English: Hello!
English: Hello
French: bonjour!
Define the different resource files separately, at this time need to define three resource files, while defining the resource file, you need to specify the language encoding for this resource file:
English: Message_zh_ch.properties
English: Message_en_us.properties
French: Message_fr_fr.properties
Message_zh_cn.properties
[Java]View Plaincopy
- info=\u4f60\u597d
Message_en_us.properties
[Java]View Plaincopy
- Info=hello
Message_fr_fr.properties
[Java]View Plaincopy
- Info=bonjour
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.util.Locale;
- Import Java.util.ResourceBundle;
- Public class T {
- public static void Main (string[] args) throws exception{
- Locale Zhlocale = new locale ("zh","CN"); //indicates China region
- Locale Enlocale = new Locale ("en","US"); //indicates US region
- Locale Frlocale = new Locale ("fr","fr"); //French area
- ResourceBundle ZHRB = Resourcebundle.getbundle ("Message", Zhlocale);
- ResourceBundle ENRB = Resourcebundle.getbundle ("Message", Enlocale);
- ResourceBundle FRRB = Resourcebundle.getbundle ("Message", Frlocale);
- System.out.println (zhrb.getstring ("info"));
- System.out.println (enrb.getstring ("info"));
- System.out.println (frrb.getstring ("info"));
- }
- }
PS: If the above Chinese attribute is in Chinese, it should be Unicode encoded, so as to avoid some of the problems caused by the system garbled
Working with dynamic text
All the content in the previous resource file is actually fixed, and if there is some content now, Hello, XXX. Then you have to do some dynamic text configuration in the resource file, set the placeholder, the contents of these symbols are temporarily not fixed, but when the program is executed by the program to set up, and to achieve such a function, you must use the Messageformat class. This class is defined in the Java.text package.
Placeholders are expressed in the form of (numbers), if the first content "{0}" and the second content "{1}" are now represented in turn.
The format () method is primarily used in the Messageformat class, and this method is defined as follows:
public static string format (String pattern,object...arguments)
[Java]View Plaincopy
- info=\u4f60\u597d{0}
- info=hello,{0}
- info=bonjour,{0}
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.text.MessageFormat;
- Import Java.util.Locale;
- Import Java.util.ResourceBundle;
- Public class T {
- public static void Main (string[] args) throws exception{
- Locale Zhlocale = new locale ("zh","CN"); //indicates China region
- Locale Enlocale = new Locale ("en","US"); //indicates US region
- Locale Frlocale = new Locale ("fr","fr"); //French area
- ResourceBundle ZHRB = Resourcebundle.getbundle ("Message", Zhlocale);
- ResourceBundle ENRB = Resourcebundle.getbundle ("Message", Enlocale);
- ResourceBundle FRRB = Resourcebundle.getbundle ("Message", Frlocale);
- System.out.println (Messageformat.format (zhrb.getstring ("info"), "Itmyhome"));
- System.out.println (Messageformat.format (enrb.getstring ("info"), "Itmyhome"));
- System.out.println (Messageformat.format (frrb.getstring ("info"), "Itmyhome"));
- }
- }
New Java features, variable parameters
After JDK1.5, Java added new features to the operation, you can pass a variable parameter to the method, the previously defined method, in fact, the parameters are fixed number of
[Java]View Plaincopy
- Package com.itmyhome;
- Public class T {
- public static void Main (string[] args) throws exception{
- Fun ("Java","C + +",". Net");
- Fun ("itmyhome");
- }
- public static void Fun (Object...args) {
- For (int i = 0; i < args.length; i++) {
- System.err.println (args[i]+",");
- }
- }
- }
Use a class instead of a resource file
You can also use a class directly to hold all of the resource file contents, but this class must have an obvious point of note when operating, and must inherit ListResourceBundle
Message_zh_cn
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.util.ListResourceBundle;
- Public class MESSAGE_ZH_CN extends ListResourceBundle {
- Private final Object date[][] = {
- {"info","Hello"}
- };
- @Override
- protected object[][] getcontents () {
- //TODO auto-generated method stub
- return date;
- }
- }
Read Resource class
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.util.Locale;
- Import Java.util.ResourceBundle;
- Public class T {
- public static void Main (string[] args) throws exception{
- Locale Zhlocale = new locale ("zh","CN"); //indicates China region
- ResourceBundle ZHRB = Resourcebundle.getbundle ("Com.itmyhome.Message", Zhlocale); //write the full path name
- System.out.println (zhrb.getstring ("info"));
- }
- }
Whether it is a resource class or a resource file, when the search is a message, then if a variety of resource files come out together, then the final look for which one?
In fact, you need to prioritize at this point:
Message_zh_cn.class
Message_zh_cn.properties
Message.properties
Summary: The idea of internationalization program realization: The program is separated from the display, and the different resource files are found according to the locale specified by different locale and the corresponding value is obtained according to their key.
Java Learning Note 12--internationalization