Struts 2 “i18n” tag is used to get the message from any declared resource bundle, not just the resource bundle that associated with the current action. See a complete “i18n” tag example below :

1. Action

Action class to forward the request.

I18nTagAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class I18nTagAction extends ActionSupport{
 
	public String execute() throws Exception {
 
		return SUCCESS;
	}
}

2. Properties files

Two properties files for the demonstration.

I18nTagAction.properties

i18n.msg = "This is a message from I18nTagAction.properties"

Custom.properties

i18n.msg = "This is a message from Custom.properties"

3. i18n tag example

It shows the use of “i18n” tag.

i18n.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 i18n tag example</h1>
 
<h3>1.Get message from I18nTagAction.properties</h3> 
Output : 
<s:text name="i18n.msg" />
 
<h3>2.Get message from Custom.properties</h3> 
Output : 
<s:i18n name="com/mkyong/common/action/Custom">
	<s:text name="i18n.msg" />
</s:i18n>
 
</body>
</html>

How it work?

1. In example 1, it will get the message from the resource bundle (I18nTagAction.properties) that’s associate with the current action class (I18nTagAction.java).

2. In example 2, it will get the message from the “Custom.properties” properties file, which located in the com/mkyong/common/action/ folder.

DO NOT PUT .properties suffix
A very common mistake in the i18n tag, if you declared the properties file with a .properties suffix, Struts 2 will failed to get the message from the declared resource bundle.
WRONG WAY :

<s:i18n name="com/mkyong/common/action/Custom.properties">
	<s:text name="i18n.msg" />
</s:i18n>

CORRECT WAY :
Declared the properties file without a .properties suffix.

<s:i18n name="com/mkyong/common/action/Custom">
	<s:text name="i18n.msg" />
</s:i18n>

4. struts.xml

Link it ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 	<constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
 
		<action name="i18nTagAction" 
			class="com.mkyong.common.action.I18nTagAction" >
			<result name="success">pages/i18n.jsp</result>
		</action>
 
	</package>
</struts>

5. Demo

http://localhost:8080/Struts2Example/i18nTagAction.action

Output

Struts 2 i18n tag example

Reference

  1. Struts 2 i18n tag documentation
Note : You can find more similar articles at - Struts 2.x Tutorials