I18n and Res
The I18n object can obtain the corresponding Res object through the baseName and locale parameters of the resource file. The Res object provides an API to obtain internationalized data.
The specific usage steps are given below:
- lCreate i18n_en_US.properties, i18n_zh_CN.properties resource files, i18n is the resource The baseName of the file can be any name. In this example, use "i18n" as the baseName
- Add the following content to the i18n_en_US.properties file: msg=Hello {0}, today is{1}.
- i18n_zh_CN.properties Add the following content to the file: msg=Hello{ 0}, Today is {1}.
- Use me.setI18nDefaultBaseName("i18n") in YourJFinalConfig to configure the resource file default baseName
- Special attention, Java internationalization specifications require that a special editor be used to edit properties files, otherwise garbled characters will appear. The commonly used ones are Properties Editor, which can be downloaded here: http://www. oschina.net/p/properties+editor
The following is a code example based on the above steps:
// Obtained through the locale parameter en_US Corresponding Res object
Res resEn = I18n.use("en_US");
// Get data directly
String msgEn = resEn.get("msg");
// Get data and Use parameter formatting
String msgEnFormat = resEn.format("msg", "james", new Date());
// Get the corresponding Res object through the locale parameter zh_CN
Res resZh = I18n.use("zh_CN");
//Get the data directly
String msgZh = resZh.get("msg");
//Get the data and format it with parameters
String msgZhFormat = resZh.format("msg", "Zhanbo", new Date());
// In addition, I18n can also load resource files that have not been configured using me.setI18nDefaultBaseName(), the only difference Yes
// You need to specify the baseName parameter. In the following example, you need to create the otherRes_en_US.properties file first. Res otherRes = I18n.use("otherRes", "en_US"); otherRes.get("msg");
Res resEn = I18n.use("en_US");
// Get data directly
String msgEn = resEn.get("msg");
// Get data and Use parameter formatting
String msgEnFormat = resEn.format("msg", "james", new Date());
// Get the corresponding Res object through the locale parameter zh_CN
Res resZh = I18n.use("zh_CN");
//Get the data directly
String msgZh = resZh.get("msg");
//Get the data and format it with parameters
String msgZhFormat = resZh.format("msg", "Zhanbo", new Date());
// In addition, I18n can also load resource files that have not been configured using me.setI18nDefaultBaseName(), the only difference Yes
// You need to specify the baseName parameter. In the following example, you need to create the otherRes_en_US.properties file first. Res otherRes = I18n.use("otherRes", "en_US"); otherRes.get("msg");