1. Resource file naming
The naming format of international resource files is as follows:
basename_language_country.properties basename_language.properties basename.properties
where basename is the base name of the resource file, language and country must be the language and country supported by java. The language and country codes that get Java support are as follows:
Locale[] locales = Locale.getAvailableLocales(); for(Locale locale:locales){ System.out.println("country:" + locale.getCountry() + ",language:" + locale.getLanguage()); }2. Configuration and acquisition of resource files
Create a new resource file. In a Chinese environment, the value value in the resource file needs to be converted ascii. You can enter native2ascii after running. In addition, this article uses the eclipse attribute file plug-in, so it is displayed directly in Chinese. The properties file is configured as follows:
info_en_US.properties
username=zhangsaninfo_zh_CN.propertiesusername=Zhangsaninfo_zh.propertiesusername=Li Si password=lisiinfo.propertiesusername=Wangwu password=wangwuemail=mailbox
In addition, if you need to read the value in the properties file, you need to configure constants in the struts-config.xml file.
<!-- Use the resource file name, where the value value represents the baseName of the resource file--> <constant name="struts.custom.i18n.resources" value="info"></constant>
The way to get resource files on the page is to use the struts tag <s:text/>, as shown below:
<!-- Resource file gets the key value--> <s:text name="username"/> <s:text name="password"/> <s:text name="email"/>
In addition, if you get the key value in the form form, the method is as follows:
<!-- where the key value is the key value in the resource file, and the function is to add a label name to the input box, which is the same as the label's attribute value. However, in constants, the key value cannot be obtained from the attribute file. --><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/> cannot obtain the corresponding value from the attribute file.
The final page shows the results as follows:
From the above configuration files, we can see the source of the values of these three values. From this, we can see that the search order of the resource file is as follows:
First, look for the relevant key value from basename_language_country.properties. If not, look for the relevant key value in basename_language.properties. If not, look for the relevant key value in basename.properties. If not, look for the relevant key value. If not, it will be displayed as name.
3. Use of placeholders in attribute files
The configuration content of the properties file is as follows:
username=Zhang San,{0},{1}
Use the tag <s:param> value</s:param> to pass parameters in jsp.
<s:text name="username"> <s:param>Hello</s:param> <s:param>Welcome!</s:param></s:text>
4. Application resource files in action
To use resource files in action, you need to inherit the ActionSupport class. The ActionSupport class implements the two interfaces: TextProvider and LocaleProvider. The TextProvider provides a method to get the resource file value getText() method, while the LocaleProvider interface has only one getLocale() method to obtain Locale.
Modify the configuration file info_zh_CN.properties:
username=Zhang San,{0},{1}usernameError=The user name cannot be empty! Please confirm!In action, the method of obtaining the property file is as follows:
this.getText("usernameError");Use placeholders in action to operate, the code is as follows:
this.getText("username", new String[]{"Hello","Welcome"});5. Package-wide resource files
The action under this package and the subpackage can access the resource file. The placement is under a certain package, and the naming format is: package_language_country.properties. Where package is a fixed format. The configuration file package_zh_CN.properties is as follows:
username=username under package scope
In the action below the package, you can use getText("username") to get it.
6. Resource files in the scope of action
You can specify a property file for a separate action, and place it in a directory of the same level as an action. The name format is
ActionName_language_country.properties
.
Configure the properties file of the action scope:
username=username within the range of action
The getText("username") method is also used in the action to get the value.
7. The loading order of resource files
For the use of getText(key) value for action, when there are both property files within the action scope, property files within the package scope and global property files, the loading order is as follows:
First, whether the key value exists in the attribute file in the specified action. If it exists, the value will be taken. If it does not exist, then look for whether the package range attribute file with the specified key value exists in the upper level package of the current action. If it does not exist, continue to search in the previous level package. If it has not been found, then look for whether the specified key value exists in the global attribute file. If the specified key value exists, the value corresponding to the key value is taken out. If it does not exist, the default is the key value.
In addition, when there are multiple resource files in the global attribute file, you need to search in the order of basename_language_country, basename_language, and basename.
8. Get resource files on the page
(1) Use <s:i18n/> to access a resource file.
The code is as follows:
<!-- Use the i18n tag to get the attribute file --> <!-- Get the resource file in the package scope --> <s:i18n name="com/struts/package"> <s:text name="username"></s:text> </s:i18n> <!-- Get the resource file in the action scope --> <s:i18n name="com/struts/service/LoginService"> <s:text name="username"></s:text> </s:i18n>
(2) Use of <fmt/>
JSTL includes internationalization and formatting tags. Using internationalization tags allows a jsp page to output page expressions in different languages according to the area where the visitor is located. Here you need to use Maven to add dependency jar packages. The configuration file is as follows:
<!-- Introducing jstl tag library--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
Use the fmt tag on the jsp page, and the application code is as follows:
<!-- Using jstl tag fmt --> <!-- Setting the locale will overwrite the complaints passed by the client --> <fmt:setLocale value="zh_CN" scope="request"/> <!-- Load a resource set and save it in the EL variable. Basename is the base name. If it is in the package scope, it is com.struts.package, and under the action scope, it is com.struts.service.LoginService--> <fmt:setBundle basename="info" scope="request" var="baseName"/> <fmt:message key="username" bundle="${baseName }"> <!-- Passing parameters for placeholders --> <fmt:param value="Hello"/> <fmt:param value="Welcome"/> </fmt:message><br/> <!--fmt:bundle Set a resource set that is only accessible in the tag body --> <!-- resource file in the package scope --> <fmt:bundle basename="com.struts.package"> <fmt:message key="username"/> </fmt:bundle> <!-- resource file in the action scope --> <fmt:bundle basename="com.struts.service.LoginService"> <fmt:message key="username"/> </fmt:bundle>