I18n_welcome.java:
public class I18N_Welcome {public static void main(String[] args) {Locale myLocale=Locale.getDefault();ResourceBundle bundle=ResourceBundle.getBundle("mess",myLocale);System.out.println(bundle.getString("welcome"));}}
Mess_en_us.properties:
welcome=Hello World !
Mess_zh_cn.properties:
welcome=\u4e16\u754c\u4f60\u597d\uff01
Mess_zh_cn.properties: the string after the medium number is "Hello World !" . For resource files that contain non-Western European characters, Java provides a tool for transcoding: native2ascii.
We can see that the basename of the two resource files is the same: mess. From the above process, we can see that if you want the program to be internationalized, you only need to store the prompts of different countries/languages (locale) in different files. For example, the language resource file for simplified Chinese is the xxx_zh_cn.properties file, and the language resource file for American English is the xxx_en_us.properties file.
The key category of Java program internationalization isResourcebundleIt has a static method:Getbundle (string basename, locale)This method loads resource files according to locale, while locale encapsulates a country and language. For example, the simplified Chinese environment can be represented by locale in simplified Chinese, the American English environment can be represented by locale in American English.
For example, use the following code to load the resource file:
ResourceBundle bundle=ResourceBundle.getBundle("mess",myLocale);
The above code will load one of the series of resource files whose basename is mess. Which resource file is loaded depends on mylocale, the second parameter. For locale in simplified Chinese, the mess_zh_cn.properties file will be loaded. For locale in American English, load the mess_en_us.properties file.
The output of the above program depends on the language environment of the operating system. If it is a simplified Chinese environment, the output will be "Hello World !"; If the English environment is in the United States, "Hello World!" Is output !".
Summary: The key classes for Java program internationalization are resourcebundle and locale,Resourcebundle loads the language resource file based on different locale, and then obtains the strings in the language resource file based on the specified key.