Java Learning Note 12--internationalization

Source: Internet
Author: User
Tags string format

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
    1. Info=hello

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import Java.util.ResourceBundle;
  3. Public class T {
  4. public static void Main (string[] args) throws exception{
  5. ResourceBundle RB = Resourcebundle.getbundle ("message");
  6. System.out.println (rb.getstring ("info"));
  7. }
  8. }


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
    1. 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
    1. info=\u4f60\u597d

Message_en_us.properties

[Java]View Plaincopy
    1. Info=hello

Message_fr_fr.properties

[Java]View Plaincopy
    1. Info=bonjour
[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import Java.util.Locale;
  3. Import Java.util.ResourceBundle;
  4. Public class T {
  5. public static void Main (string[] args) throws exception{
  6. Locale Zhlocale = new locale ("zh","CN"); //indicates China region
  7. Locale Enlocale = new Locale ("en","US"); //indicates US region
  8. Locale Frlocale = new Locale ("fr","fr"); //French area
  9. ResourceBundle ZHRB = Resourcebundle.getbundle ("Message", Zhlocale);
  10. ResourceBundle ENRB = Resourcebundle.getbundle ("Message", Enlocale);
  11. ResourceBundle FRRB = Resourcebundle.getbundle ("Message", Frlocale);
  12. System.out.println (zhrb.getstring ("info"));
  13. System.out.println (enrb.getstring ("info"));
  14. System.out.println (frrb.getstring ("info"));
  15. }
  16. }


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
    1. info=\u4f60\u597d{0}
    2. info=hello,{0}
    3. info=bonjour,{0}


[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import Java.text.MessageFormat;
  3. Import Java.util.Locale;
  4. Import Java.util.ResourceBundle;
  5. Public class T {
  6. public static void Main (string[] args) throws exception{
  7. Locale Zhlocale = new locale ("zh","CN"); //indicates China region
  8. Locale Enlocale = new Locale ("en","US"); //indicates US region
  9. Locale Frlocale = new Locale ("fr","fr"); //French area
  10. ResourceBundle ZHRB = Resourcebundle.getbundle ("Message", Zhlocale);
  11. ResourceBundle ENRB = Resourcebundle.getbundle ("Message", Enlocale);
  12. ResourceBundle FRRB = Resourcebundle.getbundle ("Message", Frlocale);
  13. System.out.println (Messageformat.format (zhrb.getstring ("info"), "Itmyhome"));
  14. System.out.println (Messageformat.format (enrb.getstring ("info"), "Itmyhome"));
  15. System.out.println (Messageformat.format (frrb.getstring ("info"), "Itmyhome"));
  16. }
  17. }


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
  1. Package com.itmyhome;
  2. Public class T {
  3. public static void Main (string[] args) throws exception{
  4. Fun ("Java","C + +",". Net");
  5. Fun ("itmyhome");
  6. }
  7. public static void Fun (Object...args) {
  8. For (int i = 0; i < args.length; i++) {
  9. System.err.println (args[i]+",");
  10. }
  11. }
  12. }

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
  1. Package com.itmyhome;
  2. Import Java.util.ListResourceBundle;
  3. Public class MESSAGE_ZH_CN extends ListResourceBundle {
  4. Private final Object date[][] = {
  5. {"info","Hello"}
  6. };
  7. @Override
  8. protected object[][] getcontents () {
  9. //TODO auto-generated method stub
  10. return date;
  11. }
  12. }


Read Resource class

[Java]View Plaincopy
  1. Package com.itmyhome;
  2. Import Java.util.Locale;
  3. Import Java.util.ResourceBundle;
  4. Public class T {
  5. public static void Main (string[] args) throws exception{
  6. Locale Zhlocale = new locale ("zh","CN"); //indicates China region
  7. ResourceBundle ZHRB = Resourcebundle.getbundle ("Com.itmyhome.Message", Zhlocale); //write the full path name
  8. System.out.println (zhrb.getstring ("info"));
  9. }
  10. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.