1, resource file naming
The internationalized resource file naming format is as follows:
Basename_language_country.properties
basename_language.properties
basename.properties
Where basename is the basic name of the resource file, language and country must be Java-supported languages and countries. Get the language and country code for Java support as follows:
locale[] locales = Locale.getavailablelocales ();
for (Locale locale:locales) {
System.out.println ("Country:" + locale.getcountry () + ", Language:" + Locale.getlanguage ());
}
2, the allocation of resource files and access
To create a new resource file, the value value in the resource file needs to be converted in ASCII in the Chinese context, and can be converted by running the input native2ascii, and the Eclipse's property file plug-in is used in this article, so it is displayed directly as Chinese. The properties file is configured as follows:
Info_en_us.properties
Username=zhangsan
info_zh_cn.properties
username= John
info_zh.properties
username= Password=lisi
info.properties
username= Harry
Password=wangwu
email= Mailbox
In addition, if you need to read the values in the properties file, you need to configure the constants in the Struts-config.xml file.
<!--Use the name of the resource file, where the value value represents the basename-->
<constant name= "struts.custom.i18n.resources" value= "info" for the resource file ></constant>
The page gets the resource file by <s:text/> the struts tag, as follows:
<!--resource file gets the key value-->
<s:text name= "username"/> <s:text name=
"password"
/> Name= "Email"/>
Also, if you get the key value in the form form, the following is the way:
<!--where the key value is the key value in the resource file, the function is to add the label name to the input box, as well as the property value of the label. However, in a constant, a key value cannot be obtained from the property file to obtain the corresponding value-->
<s:textfield name= "login.username" key= "username" ></s: Textfield>
Also note that when the constant <constant name= "Struts.ui.theme" value= "simple"/> is used, the key value in <s:textfield/> is not able to obtain the corresponding value from the property file.
The final page displays the results as follows:
From the above configuration file can see the value of these three values from the source, you can see that the resource file search order is as follows:
First look for the relevant key value from the Basename_language_country.properties, no search for basename_ Language.properties to find the relevant key value, no to the basename.properties to find the relevant key value, no is displayed as the value of name.
3. Use of placeholder in property file
The properties file configuration reads as follows:
Username= John, {0},{1}
Use label <s:param> value </s:param> to pass parameters in JSP.
<s:text name= "username" >
<s:param> Hello </s:param>
<s:param> welcome you!</s:param>
</s:text>
4, action in the application of resource files
Using resource files in action, you need to inherit the Actionsupport class, the Actionsupport class implements Textprovider, Localeprovider these two interfaces, Where Textprovider provides a method to obtain the value of a resource file GetText () method, and the Localeprovider interface has only one GetLocale () method for obtaining locale.
Modify configuration file Info_zh_cn.properties:
Username= John, {0},{1}
usernameerror= user name cannot be empty! Please confirm!
In the action, get the property file method as follows:
This.gettext ("Usernameerror");
Use placeholders for actions in action with the following code:
This.gettext ("username", new string[]{"Hello", "Welcome You"});
5, package scope of resource files
The action under the package and the child package can access the resource file, placed under a package named format: Package_language_country.properties. Where package is a fixed format. The configuration file package_zh_cn.properties is as follows:
Username= Package range of username
The action in the following package can be obtained using GetText ("username").
6, action scope of the resource file
You can specify a property file for a separate action, and the drop location is under an action sibling directory. Named format is
Actionname_language_country.properties
。
To configure an action-scoped property file:
Username=action within the range of username
The action also uses the GetText ("username") method to get the value.
7, the loading sequence of resource files
For action using the GetText (key) value, when the property file in the action range, the package-scoped property file, and the global properties file are present, the loading order is as follows:
First, whether there is a key value in the property file in the specified action, and if so, the value, if it does not exist, to find out whether the package scope property file with the specified key value exists in the first-level package of the current action, if it does not exist, continue to the upper-level package, The global property file is searched for the existence of the specified key value. If a specified key value is present, the value corresponding to the key value is fetched, and if it does not exist, the default is the key value.
In addition, when a global property file has multiple resource files, it needs to be searched in the order of Basename_language_country, Basename_language, and basename.
8, page access to resource files
(1) Use <s:i18n/> access to a resource file.
The code is as follows:
<!--use the i18n tag to get the property file-->
<!--get the resource file in the package scope-->
<s:i18n name= "Com/struts/package" >
<s : Text name= "username" ></s:text>
</s:i18n>
<!--get resource files under Action scope-->
<s:i18n Name= "Com/struts/service/loginservice" >
<s:text name= "username" ></s:text>
</s:i18n >
(2) The use of <fmt/>
The JSTL includes an internationalized and formatted label, which enables a JSP page to produce a page representation of a different language based on the region in which the visitor is located. Here you need to add a dependency jar package using Maven. The configuration file is as follows:
<!--introduced jstl tag library-->
<dependency>
<groupId>jstl</groupId>
<artifactId> jstl</artifactid>
<version>1.2</version>
</dependency>
Using the FMT tag on the JSP page, apply the following code:
<!--use the JSTL tag fmt-->
<!--to set up a locale that overrides the complaint passed by the client-->
<fmt:setlocale value= "ZH_CN" scope= "Request" />
<!--load a resource set and save it in an El variable, basename is the base name. If the package scope is the Com.struts.package,action range for the com.struts.service.loginservice-->
<fmt:setbundle basename= " Info "scope=" request "var=" BaseName "/>
<fmt:message key=" username "bundle=" ${basename} ">
<!-- Placeholder Transfer parameters-->
<fmt:param value= "Hello"/>
<fmt:param value= "Welcome"/>
</fmt:message>< Br/>
<!--fmt:bundle set a resource set that is accessed only within the label-->
<!--package resource file-->
Basename= "Com.struts.package" >
<fmt:message key= "username"/>
</fmt:bundle>
<!- -The resource file under Action scope-->
<fmt:bundle basename= "Com.struts.service.LoginService" >
<fmt:message key= "username"/>
</fmt:bundle>