On the internationalization----i18n of struts2

Source: Internet
Author: User
Tags i18n

You may occasionally see this word when you are using the Struts framework: i18n. That is internationalization I start, n end, a total of 18 letters, the main content of today is around these four letters.

Struts2 internationalization is built on the basis of Java internationalization, through the provision of different national, language environment of the message resources, and then through the ResourceBundle load the corresponding resource file of the specified locale, obtain the information specified in the file corresponding to the key.

LocaleThe object represents a specific geographical, political, and cultural area. Localeactions that need to perform their tasks, called locale-sensitive operations, use Locale information tailored to the user. For example, displaying a numeric value is a locale-sensitive operation that should be formatted according to the custom/tradition of the user's country, region, or culture.

Resourcebundle:public abstract class ResourceBundle extends Object
Resource bundles contain locale-specific objects. When a program requires a locale-specific resource, such as a String, the program can load it from a resource bundle that is appropriate for the current user locale. In this way, you can write program code that is largely independent of the user's locale, isolating most, if not all, of the language-specific information in the resource bundle.

Perhaps we are not familiar with ResourceBundle, but its subclasses, we can not be used to the familiar.

public class propertyResourceBundle extends ResourceBundle
Reads the Xxx.properties content class loader reads the resource file as a stream
PropertyResourceBundle.class.getClassLoader (). getResourceAsStream ("Db.properties")

Connection database, log4j configuration, we have used the Xxx.properties configuration file, note that these configuration files are placed under the classpath.


The internationalization of struts2 is divided into three categories: class-level, package-level, and global. The priority is reduced in descending order from left to right.

1, the global configuration method is: A, in Struts.xml <constant name= "struts.custom.i18n.resources" value= "message" ></constant> Specify BaseName Internationalization file name: Basename_ language Name _ Country name. properties (e.g., message_zh_cn.properties)
B, global resource files are placed under SRC

2, Package level: A, the package level of internationalization resources files placed under the package

b, the naming rules are: Package_ language name _. properties (e.g. Package_zh_cn.properties) where the package is not the same, not the name of the packages, the internationalized file name for each package is like this

3, Class-level: A, with the class in the same directory

B, naming rules: Class Name _ language name _ Country name. properties (e.g. Registeraction_zh_cn.properties)


Three ways to access internationalized messages:

1. JSP page output internationalization message: Use Struts's label <s:text.../> Name property to specify the key for the internationalized resource file
2, the action class to access the internationalized message, the action inherits Actionsupport implementation GetText (), receives a String type parameter, the parameter specifies the internationalized resource file key

3, table cell label output internationalized message, specify a key property for the form label, specify the key of the internationalized resource file


Know that people think the theory is very boring, the following examples to explain: Configuration steps (Global configuration JSP output internationalization message as an example)

1, in the SRC directory to add resource files, naming: basename_language_country.properties, resource file writing: In the form of key-value pairs, note the name must match


2, in the Struts.xml file for internationalization configuration, configuration constants: <constant name= "struts.custom.i18n.resources" value= "baseName"/> Take care not to write wrong, If you come to question me why I have no effect, ....
Struts 2 provides a i18n-named Interceptor (Interceptor) and registers it in the default interceptor stack (Defaultstack)
<interceptor-ref name= "i18n"/>
So we don't need to configure an international interceptor again.

3, JSP page form elements Remember to add a key attribute, the value of the key is the resource file is configured in the name


4, you can see there are two hyperlinks on the page, passed the parameters of Request_locale, parameter values and we have written a resource file named Language_country match. The role here is to switch the language environment. Struts2 's i18ninterceptor interceptors intercept all actions, and the main thing to do is to find out if there is a parameter named Request_locale in the request parameter sent from the client. If so, the value of Request_locale is converted to a locale, which is stored in a session named Ww_trans_i18n_locale. If we do not make any configuration, the client will not send the Request_locale parameter. It defaults to the GetLocale () method of the request to get the default locale, which is stored in the session. Let's take a look at the effect

This is the content of today.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

On the internationalization----i18n of struts2

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.