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.

Parameters of i18n interceptor

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

ParameterDescription
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 following pages :

  1. Login.java
  2. Login_en.properties and Login_hi.properties
  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.

Login.java
  1. package com.javacodegeeks;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3.   
  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.     this.name = 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 actionname_languagecode.properties.


3) Create index.jsp for input

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

index.jsp
  1. <%@ taglib prefix="s" uri="/struts-tags" %>  
  2.   
  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

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

4) Create view component

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

login-success.jsp
  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>  






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition