Struts 2 with i18n example Tutorial

Struts 2 with i18n example tutorial. We can support the internationalization and localization with i18n interceptor.

Struts 2 with i18n example Tutorial

Struts 2 with i18n example Tutorial

The i18n interceptor provides multi-lingual support for your application.

It handles setting locale for the action. It can be used if user wants to set his/her locale and get data according to the locale provided.

It is found in the defaultStack bydefault, so you don't have to specify it explicitly.

Parameters of i18n interceptor

There are 2 parameters defined for i18n interceptor. Both are optional.

Parameter Description
parameterName It specifies the name of the HTTP request parameter. It is set to request_locale bydefault.
attributeName specifies the name of the session key to store the locale. It is WW_TRANS_I18N_LOCALE bydefault.

Example of i18n interceptor

In this example, we are creating the following pages :

  2. and
  3. struts.xml
  4. index.jsp
  5. login-success.jsp

1) Create the action class

To use the i18n interceptor, you need to extend the ActionSupport class that implements TextProvider.

  1. package com.hpnmaratt;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  4. public class Login extends ActionSupport{  
  5. private String name;  
  6. public String getName() {  
  7.     return name;  
  8. }  
  9. public void setName(String name) {  
  10. = name;  
  11. }  
  12. public String execute(){  
  13.         return SUCCESS;  
  14. }  
  15. }  

2) Create properties files

Now create 2 properties file inside the package. Its name should be

  1. login.msg=Good Morning!

  1. login.msg=Suprabhat!  

3) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name from the user.


  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <!--  
  6. <s:textfield name="request_locale" label="Language Code"></s:textfield> 
  7.  -->  
  8. <s:submit></s:submit>  
  9. </s:form>  

Define action in struts.xml


  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">  
  3. <struts>  
  4. <package name="abc" extends="struts-default" >  
  6. <action name="login" class="com.hpnmaratt.Login" method="execute">  
  7. <result name="success">/login-success.jsp</result>  
  8. </action>  
  10. </package>  
  11. </struts>      

4) Create view component

Now use text tag to get the data. It is used in i18n.


  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. Welcome, <s:property value="name"/>  
  3. <br>  
  4. Message is: <s:text name="login.msg"></s:text>  

Directory Structure in Eclipse IDE

Now let's change the language code. Here, we are using chrome browser

Click on the settings.

Now, click on the show advanced settings.
Now, click on the language and input settings.
Now, add hindi language and move it to the top side and then click on done.
If again, you run the application, it will show hindi message.
If you use request_locale property name in index.jsp file, message will be shown according to the given language code

  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <s:textfield name="request_locale" label="Language Code"></s:textfield>  
  6. <s:submit></s:submit>  
  7. </s:form>  

Let's see the output.