DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps
  • Configuring Anypoint Platform as an Azure AD Service Provider SSO

Trending

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • Deployment Lessons You Only Learn the Hard Way
  • The Trust Problem in Modern SaaS: Why Your Authentication Succeeded, and You Still Got Breached
  • Managing, Updating, and Organizing Agent Skills
  1. DZone
  2. Coding
  3. Frameworks
  4. Internationalization/Localization in Struts2 (i18n) Interceptor

Internationalization/Localization in Struts2 (i18n) Interceptor

By 
Lalit Rao user avatar
Lalit Rao
·
Jan. 31, 15 · Interview
Likes (4)
Comment
Save
Tweet
Share
10.1K Views

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

1) create the action class

 /**
 * @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.

Web application Language code application Locale (computer hardware) Framework Property (programming) Attribute (computing) Web Service

Opinions expressed by DZone contributors are their own.

Related

  • Identifying, Exploiting, and Preventing Host Header Attacks on Web Servers
  • How To Build Web Service Using Spring Boot 2.x
  • Visually Designing Views for Java Web Apps
  • Configuring Anypoint Platform as an Azure AD Service Provider SSO

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook