java-- i18n internationalization

1. Java internationalization Introduction

Since they are bound to use the Java language as a cross-platform environment in a variety of languages, in order to solve this problem Java provides us with a tool class ResourceBundle, help us achieve the internationalization of Java, the core idea is that different the language provides a different resource file.

2. Java internationalization of step

(1) course is to write to the internationalization of the main program.

(2) define a resource file that defines resource files attention to meet certain specifications, specifications are as follows: If we default file name of resource file named message.properties, then the resource file name corresponding to other languages ​​is message_ language Code _ country Code .properties For example: our language Chinese language code is zh, the country code is CN, so the Simplified Chinese resource file name is: message_zh_CN.properties, English language code is en, the United States is the country code US, so the US English resource file is: message_en_US.properties.

(3) to obtain the appropriate resource files based software environment

(4) to obtain the corresponding value in the Key resource file acquired.

3. Examples (Note that the resource file must be placed in the root directory of the classpath)

(1) The main program ParamFormat.java

import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Internationalization of Java demo
 *
 * @Author Abu
  * / 
public  class I18nUtil {
     public  static String getI18nMessage (Language String, String Key) {
         // set a custom language country code 
        Locale locale = null ;
         IF (StringUtils.isEmpty (Language)) {
            Local = Locale.getDefault ();
        } else {
            locale = new Locale(language);
        }
        // get the resource file 
        ResourceBundle the ResourceBundle.getBundle RB = ( "Message" , the locale);
         // get the corresponding key value 
        return rb.getString (key);
    }

    public  static String getI18nMessage (Language String, String Key, Object [] the params) {
         // set a custom language country code 
        Locale the locale = null ;
         IF (StringUtils.isEmpty (Language)) {
            Local = Locale.getDefault ();
        } else {
            locale = new Locale(language);
        }
        // get the resource file 
        ResourceBundle the ResourceBundle.getBundle RB = ( "Message" , the locale);
         // get the corresponding key value 
        String value = rb.getString (key);
         // formatting parameters, return to the format string 
        return the MessageFormat .format (value, params);
    }
}

2) The default resource file message.properties (used here native2ascii the Chinese converted into ISO-8859-1 encoding.)

Transcoding ago:

greeting = Hello, {0}.
accountRegesitFailure = Account registration failed!

After transcoding (native2ascii -encoding gb2312 message_zh_pre.properties message_zh_CN.properties):

greeting = \u4f60\u597d,{0}.
accountRegesitFailure = \u8d26\u53f7\u6ce8\u518c\u5931\u8d25\uff01

(3) resource file American English message_en_US.properties

greeting = Welcome,{0}.
accountRegesitFailure = Account regesit failure.

4. Summary

In fact, this is the essence of Java i18n and some other international frameworks such as the core struts, webwork, etc. are true.

Guess you like

Origin www.cnblogs.com/jvStarBlog/p/12509961.html