Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

9.2 I18n and Res


May 14, 2021 JFinal manual



The I18n object can be obtained from the baseName and locale parameters of the resource file to the corresponding Res object, which provides the API to obtain internationalized data.

Here are some steps to use:

  • l Create i18n_en_US.properties, i18n_zh_CN.properties resource files, i18n is the baseName of the resource file, can be any name, in this example, use "i18n" as baseName
  • i18n_en_US add the following to the .properties file: msg s Hello {0}, today is {1}
  • i18n_zh_CN added the following to the .properties file: msg s {0}, today is {1}.
  • Use me.setI18nDefaultBaseName ("i18n") in YourJFinalConfig to configure the default baseName for resource files
  • In particular, the java internationalization specification requires that the editing of a properties file require a dedicated editor, otherwise it will be garbled, often with Properties Editor, which can be downloaded here: http://www.oschina.net/p/properties+editor

Here's an example of code based on the steps above:

The corresponding Res object is en_US the locale parameter is used
Res resEn = I18n. use ("en_US");
Get the data directly
String msgEn = resEn.get("msg");
Get the data and format it with parameters
String msgEnFormat = resEn.format("msg", "james", new Date());

The corresponding Res object is zh_CN the locale parameter is used
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 s resZh.format ("msg", "Jambo", new Date());

In addition, I18n can load resource files that have not been configured using me.setI18nDefaultBaseName(), the only difference
You need to specify the baseName parameter, and the following example otherRes_en_US create a new .properties file Res otherRes . . . I18n. use ("otherRes", "en_US"); otherRes.get("msg");