Struts 2 – i18n or localization example
A Struts 2 internationalizing(i18n), localization(i10n) or multi-lingua example to show the use of resource bundle to display the message from different languages. In this example, you will create a simple login screen, display the message from resource bundle via the Struts 2 UI components, and change the locale base on the selected language option.
1. Project Structure
The project structure for this example

2. Properties file
Make sure the properties file are named as country specified code.
global.properties
#Global messages
global.username = Username
global.password = Password
global.submit = Submit
global_zh_CN.properties
#Global messages
global.username = \u7528\u6237\u540d
global.password = \u5bc6\u7801
global.submit=\u63d0\u4ea4
global_fr.properties
#Global messages
global.username = Nom d'utilisateur
global.password = Mot de passe
global.submit = Soumettre
global_de.properties
#Global messages
global.username = Benutzername
global.password = Kennwort
global.submit = Einreichen
3. Action
Two action classes, the LocaleAction is basically do nothing, and the LoginAction will do a simple validation and display the error message from resource bundle via getText().
LocaleAction.java
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class LocaleAction extends ActionSupport{
//business logic
public String execute() {
return "SUCCESS";
}
}
LoginAction.java
package com.mkyong.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
//...getter and setter methods
//business logic
public String execute() {
return "SUCCESS";
}
//simple validation
public void validate(){
if("".equals(getUsername())){
addFieldError("username", getText("username.required"));
}
if("".equals(getPassword())){
addFieldError("password", getText("password.required"));
}
}
}
4. View page
A login page with a textbox, password and submit UI components.
login.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 localization example</h1>
<s:form action="validateUser" namespace="/user">
<s:textfield key="global.username" name="username" />
<s:password key="global.password" name="password"/>
<s:submit key="global.submit" name="submit" />
</s:form>
<s:url id="localeEN" namespace="/" action="locale" >
<s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="localezhCN" namespace="/" action="locale" >
<s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url id="localeDE" namespace="/" action="locale" >
<s:param name="request_locale" >de</s:param>
</s:url>
<s:url id="localeFR" namespace="/" action="locale" >
<s:param name="request_locale" >fr</s:param>
</s:url>
<s:a href="%{localeEN}" >English</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeDE}" >German</s:a>
<s:a href="%{localeFR}" >France</s:a>
</body>
</html>
5. Display the resource bundle message?
In Struts 2 , there are many ways to display the resource bundle message base on the selected language or locale. For examples,
<s:textfield key="global.username" name="username" />
<s:text name="global.username" />
<s:property value="getText('global.username')" />
<s:text name="global.password" />
6. struts.xml
Struts 2 configuration file, link it all together.
<?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.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="login">
<result>pages/login.jsp</result>
</action>
<action name="validateUser" class="com.mkyong.user.action.LoginAction">
<result name="SUCCESS">pages/welcome.jsp</result>
<result name="input">pages/login.jsp</result>
</action>
</package>
<package name="default" namespace="/" extends="struts-default">
<action name="locale" class="com.mkyong.common.action.LocaleAction">
<result name="SUCCESS">user/pages/login.jsp</result>
</action>
</package>
</struts>
7. Demo
http://localhost:8080/Struts2Example/user/login.action
http://localhost:8080/Struts2Example/locale.action?request_locale=en

http://localhost:8080/Struts2Example/locale.action?request_locale=zh_CN

http://localhost:8080/Struts2Example/locale.action?request_locale=de

http://localhost:8080/Struts2Example/locale.action?request_locale=fr

hello
how can i send messages n upload file using struts n hibernate without using java mail api
mkyong,
I am working on a project where we started in struts 1 and are now transferring to struts 2. In struts 2 how do you do i18n on html pages?
How to keep empty value for some of the languages for example if i dont want any value for english but would like to see value for JP?
You can localize .po files, .properties files as well as .xml files by using the online tool https://poeditor.com/
Once the language is changed, will this work for the complete application ? and other pages/actions ?
Is it automatically setted/stored in the session or something ?
Regards
Nitin
Howdy great blog! Does running a blog such as this require a great deal of work?
I’ve very little understanding of coding but I had been hoping to start my own blog in the near future. Anyway, if you have any ideas or techniques for new blog owners please share. I know this is off subject but I simply had to ask. Kudos!
easy to understand
Dear Mkyong,
Thanks for your tutorial, sorry to say but its little urgent for me, will you please give a little abstract for me like, I don’t have Uni-Code text, I have only plain Arabic text, I’ve using same struts, but in browser all characters are showing in strange values. I can’t use Uni-Code in any ways because my higher authorities are not listing to my words.
Grate tutorials thanks …….
Hello,
Thank to you for the quality of your tutorials, it?s really helpful.
I want ask the same question as Visitor
Do you have any examples of radio buttons or dropdown lists …..
Thanks
Do you have any examples of radio buttons or dropdown lists that populated from resources properties files to support bilingual application?
After checking out a few of the blog articles
on your site, I really appreciate your technique of writing
a blog. I saved as a favorite it to my bookmark webpage list and will be checking back soon.
Please visit my web site too and tell me what you think.
Hey there just wanted to give you a quick heads up.
The text in your post seem to be running off the screen
in Chrome. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let
you know. The design look great though! Hope you get the issue fixed soon.
Kudos
i get changed language on buttons but not on labels.on labels it is giving like this ???.??
plz help………..
what to do run this example
i am new in this field
error on my page using this example
type Status report
message /Struts2Example/user/pages/login.jsp
description The requested resource is not available.
what to do ?
what about error messages..??
hi, Thanks for the tutorial…. I’m a beginner so why no clue whether is silly one, any ways. When i tried the above it is working absolutely fine for me. but this locale will be set only for that particular page, (say for login page in mine project). once user login again it will be set global.properties. I’m just trying to know how can i set it to app level than just for a page or request level.
Thanks in Advance.
Hi, after reading this awesome piece of writing i am also happy
to share my know-how here with friends.
I appreciate your insight on this interesting topic. I am happy that I have found your blog because it will surely help in my
subsequent investing decisions. I will most definitely be back
again in the future. By the way, is there a subscription
for your site? I did not notice where to sign up on your blog.
Hi mates, nice piece of writing and good urging
commented here, I am really enjoying by these.
What’s up to every one, the contents present at this web site are actually remarkable for people knowledge, well, keep up the good work fellows.
great tutorial!! but i still got a different Problem.
I am using the following tags. But i got Symbols instead of characters in my view page.
Great job…..!!!
It is a great example for dandling text display in i18n. I am wondering how to handle images with text of different languanges in them in i18n? How to switch them according to locale selections?
how to maintain as it is “global_zh_CN.properties ” like english and other but not like unicode as above example
how to maintain chinese as it is in “global_zh_CN.properties” file like english and other…
but not like unicode(as above example).
thanks
How to implement localization for current rather than redirect to index.jsp like KWSP website?
Please help asap.
What if I want to put the global.properties file inside some package in the classpath? How should I write struts.custom.i18n.resources property in this case?
wen i selecting languages getting error dis way
You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:
struts.devMode=false
in your WEB-INF/classes/struts.properties file.
can u plzz help me
help me