Java. util. resourcebundle 1. Understand international resource filesThis category provides shortcuts to internationalization of software. With this feature, you can easily localize your programs or translate them into different languages to process multiple language Environments at a time, the function of this class is to read the properties file of the resource, and then according. properties File Name Information (localization information), match the country language information of the current system (you can also specify the Program), and then obtain the corresponding properties file content. When using this class, note that the name of this properties file is standardized: The general naming convention is: Custom name_language code _ country code. properties. If it is the default one, enter the custom name directly. properties: myres_en_us.properties
If the myres_zh_cn.properties and Myres. properties files exist in the Chinese operating system, the myres_zh_cn.properties file is used first. If the myres_zh_cn.properties file does not exist, the default Myres. properties file is used. Resource files that do not provide languages and regions are default resource files of the system. Resource files must be ISO-8859-1 encoded, so all non-Western processing must first convert it to Java Unicode escape format. The conversion method is through the built-in JDK tool native2ascii.
Ii. InstancesDefine three resource files and put them under the SRC root directory (this is required, or you can put them under your configured calsspath. Myres. Properties
aaa=good bbb=thanks
Myres_en_us.properties
aaa=good bbb=thanks
Myres_zh_cn.properties
aaa=\u597d bbb=\u591a\u8c22
Import Java. util. locale; import Java. util. resourcebundle;/*** international resource binding test ***/*** Note: to convert Chinese characters to Unicode codes on my computer, you must open C in the JDK installation directory: \ Program Files \ Java \ jdk1.6.0 _ 06 \ bin * under the native2ascii.exe file, then write the Chinese characters and press enter to generate a unicode Code * do not know why, the Unicode code generated in the properties file directory on my computer is incorrect and garbled. You can only generate the Unicode code in the first way. ** note that since it is international, for example, in Chinese and English, prepare two files, * myres_zh_cn.properties and myres_en_us.properties. * @ Param ARGs */public class testresourcebundle {
public static void main(String[] args) { Locale locale1 = new Locale("zh", "CN"); ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1); System.out.println(resb1.getString("aaa")); ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault()); System.out.println(resb1.getString("aaa")); Locale locale3 = new Locale("en", "US"); ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3); System.out.println(resb3.getString("aaa")); } }
Running result:
Thank you. Thank you. Good thanks.
If the default locale is used, the myres_en_us.properties or Myres. properties resource file is selected in the English operating system.3. Understand localeThe Locale object represents a specific geographic, political, and cultural region. The operations that require locale to execute their tasks are called language-environment-sensitive operations. It uses locale to customize information for users. For example, displaying a value is a language-sensitive operation. The value should be formatted based on the user's country, region, or cultural customs/traditions. Use the constructor in this class to create locale: locale (string LANGUAGE)
Locale (string language, string country)
After locale (string language, string country, string variant) is created, you can query its own information. You can use getcountry to obtain the ISO country code, and use getlanguage to obtain the ISO language code. You can use getdisplaycountry to obtain the country name suitable for display to users. Similarly, you can use getdisplaylanguage to obtain the language name suitable for display to users. Interestingly, the getdisplayxxx method itself is language-sensitive. It has two versions: one uses the default language environment as the parameter, and the other uses the specified language environment as the parameter.
The language parameter is a valid ISO language code. The Code is a lowercase two-letter code defined by the ISO-639. You can find a complete list of these codes on many websites, such as http://www.loc.gov/standards/iso639-2/engangn.html. The country parameter is a valid ISO country code. The code is an uppercase two letter code defined by the ISO-3166. You can find a complete list of these codes on many websites, such as http://www.iso.ch/iso/en/prods-services/iso%6ma/02iso-%6-code-lists/list-en1.html.
Iv. Transcoding of Chinese resource files native2asciiThe usage of this tool is as follows: if you are in trouble, you can directly paste Chinese characters into it, and press enter to see the transcoded results. I don't know what's going on my computer. The Unicode code generated in the first method is incorrect or garbled. The second method can generate the correct Unicode code to understand this, it is not uncommon for struts and other Web frameworks to become international.