Back to: Java Struts Tutorials
Struts 2 Localization with Examples
In this article, I am going to discuss Struts 2 Localization with Examples. Please read our previous article where we discussed Struts 2 Validation.
What is Localization?
In the context of Struts 2, localization refers to the process of adapting a web application to different languages, regions, and cultures. It involves configuring the application to support multiple languages and providing translated resources, such as messages, labels, and error messages, in different languages.
Struts 2 provides built-in support for localization through the use of resource bundles. A resource bundle is a collection of key-value pairs where each key represents a message or label and its corresponding value is the translated text in a specific language. The resource bundles are typically stored in properties files, with each file representing a specific language or locale.
To enable localization in a Struts 2 application, you need to perform the following steps:
- Configure the localization settings in the struts.xml configuration file. This involves specifying the default locale and the resource bundles to be used.
- Create resource bundle properties files for each supported language. For example, you might have files like messages_en.properties for English, messages_fr.properties for French, and so on. These files should be placed in the application’s classpath.
- In your Struts 2 actions or JSP pages, use the appropriate tags or methods to retrieve localized messages and labels. For example, you can use the <s:text> tag or the getText() method to retrieve the translated text from the resource bundles.
By using localization in Struts 2, you can create web applications that can be easily adapted to different languages and provide a localized user experience to your users.
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Initially, the pom.xml file should contain the following content:
Modify pom.xml to add the required dependencies and the build configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Step 3: In the src/main/webapp directory, create a new file called index.jsp. Add the following content to the file:
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1" pageEncoding = "ISO-8859-1"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Student Form with Multilingual Support</title> </head> <body> <h1><s:text name = "global.heading"/></h1> <s:url id = "indexEN" namespace="/" action = "locale" > <s:param name = "request_locale" >en</s:param> </s:url> <s:url id = "indexES" namespace="/" action = "locale" > <s:param name = "request_locale" >de</s:param> </s:url> <s:url id = "indexFR" namespace="/" action = "locale" > <s:param name = "request_locale" >fr</s:param> </s:url> <s:a href="%{indexEN}" >English</s:a> <s:a href="%{indexES}" >German</s:a> <s:a href="%{indexFR}" >French</s:a> <s:form action = "empinfo" method = "post" namespace = "/"> <s:textfield name = "name" key = "global.name" size = "20" /> <s:textfield name = "age" key = "global.age" size = "20" /> <s:submit name = "submit" key = "global.submit" /> </s:form> </body> </html>
This webpage basically gives the option for three different languages in which the webpage can be viewed: English, German, and French.
Step 4: In the src/main/webapp directory, create a new file called success.jsp. This file simply shows a success message. Add the following content to the file:
<%@ page contentType = "text/html; charset = UTF-8" %> <%@ taglib prefix = "s" uri = "/struts-tags" %> <html> <head> <title>Success</title> </head> <body> <s:property value = "getText('global.success')" /> </body> </html>
Step 5: In the src/main/java directory, create a new file called LocaleAction.java. This file is responsible for implementing the locale. Add the following content to the file:
package com.dotnet; import com.opensymphony.xwork2.ActionSupport; public class LocaleAction extends ActionSupport { public String execute() { return SUCCESS; } }
Step 6: In the src/main/java directory, create a new file called StudentAction.java. The file contains the Java action code. This code should be executed when the “Show details” button is clicked. Add the following content to the file:
package com.dotnet; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { private String name; private int age; public String execute() { return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Step 7: In the src/main/resources directory, create a new file called global.properties. The file is responsible for storing the text in the default language, which is English. Add the following content to the file:
global.name = Name global.age = Age global.submit = Submit global.heading = Select Locale global.success = Successfully authenticated
Step 8: In the src/main/resources directory, create a new file called global_de.properties. The file is responsible for storing the text in German. Add the following content to the file:
global.name = Name global.age = Alter global.submit = Einreichen global.heading = Wählen Sie Gebietsschema aus global.success = Erfolgreich authentifiziert
Step 8: In the src/main/resources directory, create a new file called global_fr.properties. The file is responsible for storing the text in French. Add the following content to the file:
global.name = Nom global.age = Âge global.submit = Soumettre global.heading = Sélectionnez les paramètres régionaux global.success = Authentifié avec succès
Step 9: In the src/main/resources directory, create a new file called struts.xml. The file is responsible for configuring struts2. Add the following content to the file:
<?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" /> <constant name = "struts.custom.i18n.resources" value = "global" /> <package name = "helloworld" extends = "struts-default"> <action name = "StudentAction" class = "com.dotnet.StudentAction" method = "execute"> <result name = "input">/index.jsp</result> <result name = "success">/success.jsp</result> </action> <action name = "LocaleAction" class = "com.dotnet.LocaleAction" method = "execute"> <result name = "success">/index.jsp</result> </action> </package> </struts>
Step 10: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
<?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" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Step 11: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
If you click on the “German” link, the following webpage will be displayed:
If you click on the “French” link, the following webpage will be displayed:
Once you enter the proper details, one of the following web pages will be displayed:
English:
German:
French:
Congratulations! You have successfully implemented localization in struts 2.
In the next article, I am going to discuss Type Conversion in Struts 2 with Examples. Here, in this article, I try to explain Struts 2 Localization with Examples and I hope you enjoy this Struts 2 with Localization article.