Over a million developers have joined DZone.

Internationalization/Localization in Struts2 (i18n) Interceptor

DZone's Guide to

Internationalization/Localization in Struts2 (i18n) Interceptor

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}