Over a million developers have joined DZone.

Internationalization/Localization in Struts2 (i18n) Interceptor

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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 parameterrequest_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 {
        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

    HelloWorld.message=Good Morning!  


 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" %>

        <title><s:text name="HelloWorld.message"/></title>

        <h2><s:property value="message"/></h2>

                <s:url id="url" action="HelloWorld">
                    <s:param name="request_locale">en</s:param>
               Click <s:a href="%{url}">here</s:a> to greet in English.

                <s:url id="url" action="HelloWorld">
                    <s:param name="request_locale">in</s:param>

            Click <s:a href="%{url}" >here</s:a> to greet in Hindi.


 4) Define action in struts.xml


    <package name="com.nirvanaitedge.lee" namespace="/com.nirvanaitedge.lee"
      <action name="HelloWorld" class="com.nirvanaitedge.lee.HelloWorld">

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 1st hyperlink greets in English.

Clicking on second hyperlink greets in Hindi.


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.


Here we understood how internationalization can be achieved in Struts2 using resource bundles with the simple example.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}