<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p><b>Internationalization (i18n)</b> 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. The internationalization process is sometimes called translation or localization enablement. Internationalization is abbreviated <b>i18n</b> because the word starts with an i and ends with an n, and there are 18 characters between the first i and the last n.</p>
<p>Will create a simple <b>employee form</b> screen, display the message from resource bundle via the Struts 2 UI components, and change the locale base on the selected language option.</p>
<h2><b>Resource Bundles:</b></h2>
<p>uses resource bundles to provide multiple language and locale options to the users of the web application. You don&#8217;t need to worry about writing pages in different languages. All you have to do is to create a resource bundle for each language that you want. The resource bundles will contain titles, messages, and other text in the language of your user. Resource bundles are the file that contains the key/value pairs for the default language of your application.</p>
<p>When you reference a message element by its key, Struts framework searches for a corresponding message bundle in the following order:</p>
<ul style="text-align: left;">
<li>ActionClass.properties</li>
<li>Interface.properties</li>
<li>SuperClass.properties</li>
<li>model.properties</li>
<li>package.properties</li>
<li>struts.properties</li>
<li>global.properties</li>
</ul>
<p>To develop your application in multiple languages, you would have to maintain multiple property files corresponding to those languages/locale and define all the content in terms of key/value pairs.</p>
<p>For example if you are going to develop your application for<b> US English (Default), Hindi,Spanish, Chinese, German and Franch </b>the you would have to create three properties files. Here I will use global.properties file only, you can make use of different property files to segregate different type of messages.</p>
<ul style="text-align: left;">
<li><b>global.properties:</b> By default English (United States) will be applied</li>
<li><b>global_fr.properties:</b> This will be used for Franch locale.</li>
<li><b>global_es.properties:</b> This will be used for Spanish locale.</li>
<li> <b>global_de.properties:</b> This will be used for German locale.</li>
<li><b>global_hn.properties:</b> This will be used for Hindi locale.</li>
<li><b>global_zh_CN.properties:</b> This will be used for China locale.</li>
</ul>
</div>
<div id="ads-id" align="center"></div>
<h2><b>global.properties:</b></h2>
<pre class="highlight">global.name = Name 
global.age = Age 
global.telephone = Telephone 
global.email = Email 
global.submit = Submit 
global.success = Employee Successfully Added 
global.form = Employee Form 
</pre>
<h2><b>global_hn.properties:</b></h2>
<pre class="highlight">global.name = u0928u093Eu092E 
global.age = u0909u092Eu094Du0930 
global.telephone = u091Fu0947u0932u0940u092Bu094Bu0928 
global.email = u0908u092Eu0947u0932 
global.submit = u092Au094Du0930u0938u094Du0924u0941u0924 u0915u0930u0928u093E 
global.success = u0915u0930u094Du092Eu091Au093Eu0930u0940 u0938u092Bu0932u0924u093Eu092Au0942u0930u094Du0935u0915 u091Cu094Bu0921u093Cu093E u0917u092Fu093E 
global.form = u0915u0930u094Du092Eu091Au093Eu0930u0940 u092Au0930u094Du091Au093E 
</pre>
<h2><b>global_de.properties:</b></h2>
<pre class="highlight">global.name = Nennen 
global.age = Alter 
global.submit = Einreichen 
global.form = Angestellter Formular 
global.telephone = Telefonieren 
global.email = Emaeiln 
global.success = Mitarbeiter erfolgreich am 
</pre>
<h2><b>global_es.properties:</b></h2>
<pre class="highlight">global.name = Nombre de usuario 
global.age = Edad 
global.telephone = Teléfono 
global.email = Emaile 
global.submit = Presentar 
global.form = Formulario Empleado 
global.success = Empleado con éxito Agregado 
</pre>
<h2><b>global_fr.properties:</b></h2>
<pre class="highlight">global.name = Nom d'utilisateur 
global.age = l'âge 
global.telephone = téléphone 
global.email = Emailé 
global.submit = Soumettre des 
global.form = Formulaire d'employés 
global.success = Employé ajouté avec succès 
</pre>
<h2><b>global_zh_CN.properties:</b></h2>
<pre class="highlight">global.name = u540D 
global.age =u5E74u9F84 
global.telephone = u7535u8BDD 
global.email = u7535u5B50u90AEu4EF6 
global.submit=u63d0u4ea4 
global.form = u5458u5DE5u8868u5355 
global.success = u5458u5DE5u6210u529Fu6DFBu52A0 
</pre>
<h2><b>Access the messages:</b></h2>
<p>There are several ways to access the message resources, including <i><b>getText</b></i>, the <i><b>text tag</b></i>, <i><b>key </b></i>attribute of UI <i><b>tags</b></i>, and the<i><b> i18n</b></i> tag. Let us see them in brief:</p>
<p>To display i18n text, use a call to <b>getText </b>in the property tag, or any other tag, such as the UI tags as follows:</p>
<pre class="highlight"><;s:property value="getText('some.key')" />; 
</pre>
<p>The text tag retrieves a message from the default resource bundle ie. <i><b>struts.properties</b></i></p>
<pre class="highlight"><;s:text name="some.key" />; 
</pre>
<p>The <i><b>i18n</b></i> tag pushes an arbitrary resource bundle on to the value stack. Other tags within the scope of the <i><b>i18n</b></i> tag can display messages from that resource bundle:</p>
<pre class="highlight"><;s:i18n name="some.package.bundle">; 
 <;s:text name="some.key" />; 
<;/s:i18n>; 
</pre>
<p>The key attribute of most UI tags can be used to retrieve a message from a resource bundle:</p>
<pre class="highlight"><;s:textfield key="some.key" name="textfieldName"/>; 
</pre>
<h2><b>Localization Example:</b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/9999.png" border="0" /></div>
<p><b> </b></p>
<p>Let us target to create employee.jsp from the previous chapter in multiple languages. Same file would be written as follows:</p>
</div>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;html>; 
<;head>; 
<;title>;<;s:property value="getText('global.form')" />; - Struts2 Demo | dineshonjava.com<;/title>; 
<;s:head />; 
<;/head>; 
 
<;body>; 
<;h2>;<;s:property value="getText('global.form')" />;<;/h2>; 
 
<;s:form action="employee" method="post" validate="true">; 
 <;s:textfield name="name" key="global.name" size="20" />; 
 <;s:textfield name="age" key="global.age" size="20" />; 
 <;s:textfield name="email" key="global.email" size="20" />; 
 <;s:textfield name="telephone" key="global.telephone" size="20" />; 
 <;s:submit name="submit" key="global.submit" align="center" />; 
<;/s:form>; 
 
<;s:url id="localeEN" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;en<;/s:param>; 
<;/s:url>; 
<;s:url id="localeHN" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;hn<;/s:param>; 
<;/s:url>; 
<;s:url id="localeES" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;es<;/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="%{localeHN}" >;Hindi<;/s:a>; 
<;s:a href="%{localeES}" >;Spanish<;/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>; 
</pre>
</div>
<p><b>We will create <i>success.jsp</i> file which will be invoked in case defined action returns SUCCESS.</b></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;html>; 
<;head>; 
<;title>;<;s:property value="getText('global.form')" />; - Struts2 Demo | dineshonjava.com<;/title>; 
<;/head>; 
 
<;body>; 
 <;h2>;<;s:property value="getText('global.success')" />;.<;/h2>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>Here we would need to create following two actions. </b></p>
<p>(a) First action a to take care of Locale and display same <i><b>success.jsp</b></i> file with different language<br />
(b) Another action is to take care of submitting form itself.<br />
Both the actions will return SUCCESS, but we will take different actions based on return values because our purpose is different for both the actions:</p>
<p><b>EmployeeLocaleAction.java</b></p>
</div>
<pre class="highlight">package com.dineshonjava.com.struts2.action.locale; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmployeeLocaleAction extends ActionSupport { 
 
 private static final long serialVersionUID = 1L; 
 
 public String execute() { 
 return SUCCESS; 
 } 
} 
</pre>
<p><b>EmployeeAction.java</b></p>
<pre class="highlight">package com.dineshonjava.com.struts2.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmployeeAction extends ActionSupport { 
 
 private static final long serialVersionUID = 1L; 
 private String name; 
 private Integer age; 
 private String email; 
 private String telephone; 
 
 public String addEmployee() { 
 return SUCCESS; 
 } 
 public String getName() { 
 return name; 
 } 
 public void setName(String name) { 
 this.name = name; 
 } 
 public Integer getAge() { 
 return age; 
 } 
 public void setAge(Integer age) { 
 this.age = age; 
 } 
 public String getEmail() { 
 return email; 
 } 
 public void setEmail(String email) { 
 this.email = email; 
 } 
 public String getTelephone() { 
 return telephone; 
 } 
 public void setTelephone(String telephone) { 
 this.telephone = telephone; 
 } 
} 
</pre>
<p><b>We will create our <i>struts.xml</i> with two actions as follows:</b></p>
<pre class="highlight"><;?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.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="global" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="employee" class="com.dineshonjava.com.struts2.action.EmployeeAction">; 
 <;result name="success">;/success.jsp<;/result>; 
 <;result name="input">;/employee.jsp<;/result>; 
 <;result name="error">;/error.jsp<;/result>; 
 <;/action>; 
 
 <;action name="locale" class="com.dineshonjava.com.struts2.action.locale.EmployeeLocaleAction">; 
 <;result name="success">;/employee.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 
<;/struts>; 
</pre>
</div>
<p><b>Following is the content of <i>web.xml</i> file:</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2I18N<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;index.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
</div>
<p>Now, right click on the project name and click <b>Export >; WAR</b> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access<br />
<b>URL <i>http://localhost:8080/doj/employee.jsp.</i></b></p>
<p><b>This will give you following screen:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/11111111-1.png" border="0" /></div>
<p> ;</p>
</div>
<p><b>Now select on Hindi language.</b><br />
<i><b>http://localhost:8080/doj/locale.action?request_locale=hn</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/222222-2.png" border="0" /></div>
<p> ;</p>
</div>
<p><b>Now select on Spanish language.</b><br />
<i><b>http://localhost:8080/doj/locale.action?request_locale=es</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/33333-2.png" border="0" /></div>
</div>
<p><b>Now select on Chines language.</b><br />
<i><b>http://localhost:8080/doj/locale.action?request_locale=zh_CN</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/444444.png" border="0" /></div>
</div>
<p><b>Now select on German language.</b><br />
<i><b>http://localhost:8080/doj/locale.action?request_locale=de</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/55555-1.png" border="0" /></div>
</div>
<p><b>Now select on German language.</b><br />
<i><b>http://localhost:8080/doj/locale.action?request_locale=fr</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/66666-2.png" border="0" /></div>
<p> ;</p>
</div>
<p><b>Now after submit the form following success messages display as per as languages selected.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/77777.png" border="0" /></div>
<p> ;</p>
</div>
<h2><b>Download Source Code</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2I18N.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2I18N.zip</a></b></h2>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/struts2-validation-with-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/themes-in-struts-2/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/struts2-validation-with-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/themes-in-struts-2/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '381'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…