Internationalization/Localization in Struts2 (i18n) Interceptor
Join the DZone community and get the full member experience.
Join For Free
struts2 framework supports internationalization and we can create resource bundle property files to be used by the framework. struts2 i18n is used a lot in creating labels based on the locale in result pages using ui tags.
internationalization (i18n) is the process of planning and implementing products and services so that they can easily be adapted to specific local languages and cultures, a process called localization. struts2 framework supports i18n through
i18ninterceptor
interceptor and we can pass locale in request with parameter
request_locale
. this interceptor is part of default interceptor stack, so we don’t need to do anything for localization.
the
i18ninterceptor
simply waits for the
request_locale
parameter. once it receives, the
i18ninterceptor
triggers and checks the resource bundles for the corresponding language code e.g. ‘en’ for english, ‘es’ for espanol and change the language code.
now a
resource bundles (properties in java)contain locale-specific objects. when your program needs a locale-specific resource, a
string
or example, your program can load it from the resource bundle that is appropriate for the current user's locale.
this example tries to show in a simple and straight forward way of creating a web application with internationalization or i18n capability.
in this example, we are creating following pages:
1. helloworld.java
2. helloworld_en.properties and helloworld _hi.properties
3. helloworld.jsp
4. struts.xml
/** * @author lee */ package com.nirvanaitedge.lee; import com.opensymphony.xwork2.actionsupport; public class helloworld extends actionsupport { public string execute() throws exception { setmessage(gettext(message)); return success; } /** * provide default value for message property. */ public static final string message = "helloworld.message"; /** * field for message property. */ private string message; /** * return message property. * * @return message property */ public string getmessage() { return message; } /** * set message property. * * @param message text to display on helloworld page. */ public void setmessage(string message) { this.message = message; } }
2) create the properties file
1.helloworld_en.properties helloworld.message=good morning! 2.helloworld_hi.properties helloworld.message=suprabhat!
3) create helloworld.jsp for input
<%-- document : newjsp created on : 14 jan, 2015, 6:46:19 pm author : lee --%> <%@ page contenttype="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title><s:text name="helloworld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <h3>languages</h3> <ul> <li> <s:url id="url" action="helloworld"> <s:param name="request_locale">en</s:param> </s:url> click <s:a href="%{url}">here</s:a> to greet in english. </li> <li> <s:url id="url" action="helloworld"> <s:param name="request_locale">in</s:param> </s:url> click <s:a href="%{url}" >here</s:a> to greet in hindi. </li> </ul> </body> </html>
4) define action in struts.xml
<struts> <package name="com.nirvanaitedge.lee" namespace="/com.nirvanaitedge.lee" extends="struts-default"> <action name="helloworld" class="com.nirvanaitedge.lee.helloworld"> <result>/com.nirvanaitedge.lee/helloworld.jsp</result> </action> </package> </struts>
make a note that we don’t need to specify ‘method = “execute” ’ as action tag attribute because struts by default checks the ‘execute’ method if no method is specified. the ‘method’ attribute of action class is used only when we are using a method name other than ‘execute’ who’s result we need to map in struts.xml file.
running the application:
helloworld.jsp is loaded with two hyperlinks. clicking on any of the links will greet in the specific language.
clicking on 1 st hyperlink greets in english.
clicking on second hyperlink greets in hindi.
explanation:
clicking on any of the hyperlink fires the url with parameter as “request_locale”. struts’ i18n-interceptor triggers itself, takes the value of request_locale, changes the language code finding the corresponding values in the properties file and sets the “helloworld.message” value accordingly.
conclusion:
here we understood how internationalization can be achieved in struts2 using resource bundles with the simple example.
Opinions expressed by DZone contributors are their own.
Comments