Struts2 notes--6. Internationalization

Source: Internet
Author: User
Tags i18n

The internationalization of STRUTS2 is based on the internationalization of Java. By providing a message resource for a different country or locale, and then loading the resource file for the specified locale through ResourceBundle, the corresponding message for the specified key in the resource file is obtained. The STRUTS2 framework further encapsulates this process, simplifying the internationalization of applications. It can provide different international resources for jsp,action, global scope, so maintenance can be maintained separately. The internationalization of Struts2 can be done in the following steps: Loading internationalized resource files and exporting internationalization.

The action scope, package scope of internationalized resources will be automatically loaded by the system, JSP scope, global scope needs to use the tag library, configure the constant way to manually load. The internationalization message for the View page requires the STRUTS2 tag library output, and the message in the action needs to be done actionsupport the GetText () method.

The resource file needs to be properties and is placed under SRC. Naming rules must follow basename_language_country.properties strictly. Let's say we want to achieve the internationalization associated with login, we need to build login_zh_cn.properties and login_en_us.properties these two files or more. Then two files for the same key field, to give the Chinese and English value. Where value values can use placeholders, such as: Hello {0}, as to how to pass parameters, explained later.


Internationalization of view pages

Specifying an internationalized resource in a JSP requires the s:i18n/label. The reason is called i18n, because internationalization altogether 18 letters. When the text tag is inside the il8n, text loads the internationalized resource file specified il8n. When the form is inside the i18n, the key in the form (the value in the normal JSP, which is displayed on the page) will extract the information from the internationalization. The resource file is a properties. Placed in SRC or its subdirectories. The name attribute inside the i18n tag in the JSP specifies the properties file location. The following code can load a series of internationalized resource files under the PackageName package that basename is myinternational.

<s:i18n name= "Packagename.myinternational" >


Internationalization of action and packages

When you internationalize the input checksum for an action or action, you can specify an internationalized resource file for the action individually. Create multiple files named Actionname_language_country.properties under the path of the action. These internationalized files are automatically loaded by the system. The GetText method of the parent class is used directly in the action to get the value of the specified key. PS: Using internationalized value in a checksum file, you can use the properties of the message child element. Creating an internationalized file for each action is too cumbersome to create a Package_language_country.properties series of files under the package that will be automatically loaded by the system.


Global internationalization

You can load a global internationalized resource file by configuring the Struts.custom.i18n.resource constant in Struts.xml or struts.properties. The value of the constant is the basename of a series of global internationalized files. If basename is example, add the following code to the Struts.properties:

Struts.custom.i18n.resource=example

It should be noted that the internationalization file should be the same path as the configuration file. Global internationalized Messaging resources can be used by all components of the entire application.


Internationalized messages with placeholders

These placeholders must be replaced by some parameters when the Internationalized resource contains placeholders for shapes such as {0},{1}. Placeholders in JSPs are replaced by the values in the S:param element. The first few param are substituted for the first few placeholders.

<s:text name= "username" ></s:text>

<s:text name= "WELCOMG" >
<s:param><s:property value= "username"/></s:param>
<s:param>S{d}</s:param>

</s:text>

The value in the corresponding WELCOMG resource file should be

Welcome={0}, Hello, now the time is: {1}
-----------------------------------------
Welcome={0}, Hello, now is {1}

In addition, expressions can also be used in internationalized messages. In this way, you do not need to assign a value to the placeholder when calling internationalized messages. Like what:

Current Username= currently users are: ${username}

This approach works by removing the value of the username in Valuestack and then populating it with internationalized messages.


Load resource File Order

Suppose that an action wants to load the action file it needs, the order is as follows:


  1. The first to find its own exclusive, that is, basename and action with the same name.

  2. If not, look for the exclusive exclusive of its parent class.

  3. If not, find the exclusive access to the action's interface.

  4. If not, find the exclusive file for the package where the action is located.

  5. If not, follow the bag up to find, and finally not yet, to find the overall.

  6. If not, output the value of the key property directly.



Assuming that you want to load internationalized files for JSPs, if you have s:i18n as the parent tag, the order is as follows:

    1. Find the NameBase specified by s:i18n.

    2. If not, go to the big picture.

    3. If not, there is no way to find out the string value of key directly.


If you do not use s:i18n as the parent tag, the global resource file is loaded directly. The value of the key property is directly output if it is not found.

This article from "Fingertip Light Fly" blog, declined reprint!

Struts2 notes--6. Internationalization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.