Struts2 i18n internationalization (allows users to choose their own language) go
Recently in the study struts2, the study material is Li Gang's "struts2 authoritative guide", this book very good, very has the study value. In my study, I followed up with some examples. The following are the principles and examples of allowing users to choose their own programming language in STRUTS2:
In many mature commercial software, can let the user switch language freely, when the user enters the system, can appear a drop-down list box, lets the user select the language, once the user chooses the language environment which he needs to use, the entire system language environment will always be this language environment.
Struts2 can also allow users to choose their own program language. And, because of the support of STRUTS2, it will be easier to choose your own language environment in the program.
I. Operating mechanism of STRUTS2 internationalization
In Struts 2, we can set the user's default language by using Actioncontext.getcontext (). SetLocale (locale arg). However, this is a completely manual approach and requires programmatic implementation.
To simplify setting up the user's default locale, Struts 2 provides an interceptor (interceptor) named i18n and registers it in the default interceptor stack (Defaultstack). The following is a configuration fragment of the default interceptor stack for STRUTS2, in which the code in bold indicates the i18n interceptor.
<interceptor-stack name= "Defaultstack" >
<interceptor-ref name= "Exception"/>
<interceptor-ref name= "Alias"/>
<interceptor-ref name= "Servlet-config"/>
<interceptor-ref name= "Prepare"/>
<interceptor-ref name= "i18n"/>
<interceptor-ref name= "Chain"/>
<interceptor-ref name= "Debugging"/>
<interceptor-ref name= "Profiling"/>
<interceptor-ref name= "Scoped-model-driven"/>
<interceptor-ref name= "Model-driven"/>
<interceptor-ref name= "FileUpload"/>
<interceptor-ref name= "checkbox"/>
<interceptor-ref name= "Static-params"/>
<interceptor-ref name= "params" >
<param name= "Excludeparams" >dojo\. *</param>
</interceptor-ref>
<interceptor-ref name= "Conversionerror"/>
<interceptor-ref name= "Validation" >
<param name= "Excludemethods" >input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name= "Workflow" >
<param name= "Excludemethods" >input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
I18N Interceptor automatically finds a parameter named Request_locale in the request before executing the action method. If the parameter exists, the interceptor takes it as an argument, converts it to a locale object, and sets it as the user's default locale, which represents the language/national environment.
In addition, the I18N interceptor will save the generated LOCALE object in the user session named "Ww_trans_i18n_locale" in the attribute. Once a property named "Ww_trans_i18n_locale" exists in the user session, the locale specified by the attribute will be the default locale for the browser.
Two. Create a drop-down list box
Based on the previous introduction, in order to enable the user to choose the language of the program, you only need to provide a drop-down list box, the drop-down list box lists the various languages supported by the app, and when the user selects an item in the drop-down list box, the system takes the value of the drop-down as Request_ The locale parameter is presented to the STRUTS2 system,
To do this, we put the language supported by the system into a map, by iterating over the map object in the JSP page, in this way, you can list all the languages supported by the system on the page and let the user choose freely.
The following defines the JavaBean, which holds all the languages supported by the current application, and the JavaBean code is as follows:
The JavaBean stores all the languages supported by the system.
public class Locales extends Actionsupport
{
Because this instance also needs to be internationalized, using current as the user locale
Private Locale current;
Get setter method for user's current locale
public void Setcurrent (Locale cur)
{
this.current = cur;
}
Get all the languages supported by this system
Public map<string, locale> Getlocales ()
{
Keep all languages supported by the current system in the Map object
map<string, locale> locales = new hashtable<string, locale> ();
ResourceBundle bundle = Resourcebundle.getbundle ("Messageresource", current);
Add the language supported by the current system, key is the display name of the system support language, value is the locale instance of the System support language
Locales.put (bundle.getstring ("Usen"), locale.us);
Locales.put (bundle.getstring ("ZHCN"), Locale.china);
return locales;
}
}
In the above JavaBean, we used a map object to hold all user-supported languages, and the map object's key is the display name of the supported language, and the map object's value is the locale instance of the supported language. The display name of the supported language is also generated by the internationalization message.
Once the JavaBean is defined, an instance of the JavaBean can be created in the JSP page and passed a current parameter to determine the locale parameter in the JavaBean. Depending on the locale parameter, you can decide how to display the language display name supported by the system.
To use the JavaBean instance in a JSP page, use the following label to create the JavaBean instance.
<!--using Lee. Locales Create locales instance--
<s:bean id= "Locales" Name= "Lee. Locales ">
<!--is a locales instance of this current parameter value--
<s:param name= "Current"
value= "#SESSION_LOCALE = = null LOCALE: #SESSION_LOCALE"/>
</s:bean>
The label above creates Lee. The locales instance of the locales class, and the current parameter value is passed to the instance, when the parameter value is set, the three-mesh operator is used to determine if the Session_locale is empty, and if the Session_locale is empty, Returns the value of the Locale property in Valuestack (that is, the locale that the user's browser sets), or the value of the Session_locale (that is, the user-selected locale) If Session_locale is not empty.
to make the page contain Session_locale, use the STRUTS2 <s:set .../> tab to set the value of the Ww_trans_i18n_locale property in the user session to Session_ LOCALE.
The following is the label that completes the setting:
<!--set the value of the "Ww_trans_i18n_locale" attribute in the user session to Session_locale. -
<s:set name= "Session_locale" value= "#session [' Ww_trans_i18n_locale ']"/>
Here is the code for the selectlanguage.jsp page:
<% @taglib prefix= "s" uri= "/struts-tags"%>
<script type= "Text/javascript" >
function langselecter_onchanged ()
{
document.getElementById ("Langform"). Submit ();
}
</script>
<!--set the value of the "Ww_trans_i18n_locale" attribute in the user session to Session_locale. -
<s:set name= "Session_locale" value= "#session [' Ww_trans_i18n_locale ']"/>
<!--using Lee. Locales Create locales instance--
<s:bean id= "Locales" Name= "Lee. Locales ">
<!--is a locales instance of this current parameter value--
<s:param name= "Current"
value= "#SESSION_LOCALE = = null LOCALE: #SESSION_LOCALE"/>
</s:bean>
<!--a form that lets users select a language--
<form action= "<s:url/$amp; >quot;$ id=" Langform "
>
<!--output Internationalization tips-
<s:text name= "Languag"/>
<!--use the S:select tag to iterate over the locales map property of the locales instance--
<s:select label= "Language" list= "#locales. Locales" listkey= "value" listvalue= "key"
value= "#SESSION_LOCALE = = null LOCALE: #SESSION_LOCALE"
Name= "Request_locale" id= "Langselecter"
Onchange= "langselecter_onchanged ()" theme= "simple"/>
</form>
Note: The STRUTS2 label is used extensively in the above page, please refer to the tenth chapter of this book for the detailed usage of the Struts2 tag.
The principle of the above page is to instantiate a locales object with the <s:set .../> tag and use the <s:select ..../> to display the locales (map type) property of the Locales object, <s: Select ..../> can use a drop-down list box to display a collection of map types, which will output the map's key as the display name of a drop-down list item, outputting the value of the map as a drop-down list item.
In addition to this, there is a simple JavaScript script on the page that submits the form to action that contains the "Reqeust_locale" variable after the user selects an item in the drop-down list.
The above page and JavaBean used three internationalized keys, so we need to define the internationalized message corresponding to these three keys in the resource file. Because this system only supports Simplified Chinese and American English two environment (if need to add other languages is very simple, only need to add more resources files, and simply modify the locales Class), so you need to add the following three items in the Chinese resource file:
Languag= Select language
usen= American English
zhcn= Simplified Chinese
Add the following three items to the English resource file:
Languag=select Lanuage
Usen=american 中文版
Zhcn=simplified Chinese
Three. Select the program language
This application for better security, all the JSP pages are placed under the web-inf/jsp path, thus avoiding the direct access to the JSP page, in order to make all JSP pages can be Struts2 processing, in the Struts.xml file add the following configuration fragment:
<!--use a wildcard symbol to define the name of the action--
<action name= "*" >
<!--forward the request to the JSP page with the same name as the web-inf/jsp path--
<result>/WEB-INF/jsp/{1}.jsp</result>
</action>
If you request selectlanguage.action in the browser, you will see a page that selects the language of the program.
If the user selects the American English item through the drop-down list box above, they will see a page where the user chooses American English.
Once the above page has been defined, we can include the page in the JSP page via the <s:include .../> tag, which will allow you to choose the program language.
For example, on the landing page, the following tabs are included to include the page in which the program language is selected:
<!--contains pages that let users choose their own program language--
<s:include value= "selectlanguage.jsp"/>
When the above code is added to any page, the page will allow the user to choose the language of their choice. In general, we only need to let the user select the program language on the first page of the app, and the subsequent pages will use that language directly.
If the viewer accesses input.action,struts2 in the browser's address bar and automatically enters the web-inf/jsp/input.jsp page, you will see a page that allows the user to select a program language.
If the user wants to use the American English language, select the "American English" item in the drop-down list box in the page that allows the user to choose the language of the program, and you will see a page that uses the American English interface.
If the user login is successful, you will see a successful landing page in the American English environment. This page automatically uses the American English environment, this is because the user chooses the American English language, the system has set the user choice to the session's Ww_trans_i18n_locale attribute value, the session attribute value directly decides Struts2 system's locale.
---------!!!!! Special reminder: Locales must inherit extends Actionsupport, this is I toss a day of lessons, on-line some have not inherited the example, do not inherit words, cause <s:bean id= "Locales" Name= "Lee." Locales ">
<!--is a locales instance of this current parameter value--
<s:param name= "Current"
value= "#SESSION_LOCALE = = null LOCALE: #SESSION_LOCALE"/>
</s:bean>
Here the value locale is empty, and there will be a problem behind it.
Ww_trans_i18n_locale and Ww_trans_i18n_locale properties