Visit Sponsor

Written by 12:46 pm Core Java

Struts2 Localization and Internationalization – Example and Setup

Modern web applications often need to support multiple languages and regional formats. In Struts2, this capability is referred to as Localization (l10n) and Internationalization (i18n). Localization adapts texts and formats for a specific locale, while Internationalization enables your app to support that adaptability in the first place.

In this guide, we’ll show how to configure Struts2 for localization and internationalization with message bundles, locale selection, and practical examples.

What Is Localization & Internationalization?

  • Internationalization (i18n)
    The process of making your application capable of supporting multiple locales without extensive code changes.
  • Localization (l10n)
    The process of adapting your app to a particular language or region (e.g., English, Hindi, Spanish).

Struts2 natively supports i18n/l10n using resource bundles (properties files) and configurable locale resolvers.

How Struts2 i18n Works

Struts2 localization works by:

  1. Providing localized resource bundles (.properties)
  2. Configuring Struts2 and result pages to use these bundles
  3. Passing locale parameters from the UI
  4. The framework resolves the appropriate bundle for the current locale

Project Setup

Ensure your web project includes standard Struts2 dependencies. If you’re using Maven, add:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts2.version}</version>
</dependency>

Also include a locale interceptor in your stack (default stack includes it).

Resource Bundles (Message Properties)

Create localized message property files under src/main/resources:

  • Global Messages (default)
    GlobalMessages.properties
  • Spanish locale
    GlobalMessages_es.properties

Example — GlobalMessages.properties:

welcome.message=Welcome to Struts2 Internationalization!
label.username=Username
label.password=Password

Example — GlobalMessages_es.properties:

welcome.message=¡Bienvenido a la Internacionalización de Struts2!
label.username=Nombre de usuario
label.password=Contraseña

These files contain key/value pairs for UI text.

struts.xml Configuration

Struts2 automatically uses message bundles when configured properly. Put this in your struts.xml:

<constant name="struts.custom.i18n.resources" value="GlobalMessages"/>

This tells Struts2 to load GlobalMessages*.properties as the resource bundle.

Interceptor Stack

Struts2 uses the i18n interceptor by default in the defaultStack. But if you override stacks, ensure it’s present:

<interceptors>
    <interceptor name="i18n" class="org.apache.struts2.interceptor.I18nInterceptor"/>
    <interceptor-stack name="appStack">
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
</interceptors>

<default-interceptor-ref name="appStack"/>

This interceptor listens for locale changes (via parameters like request_locale).

Passing Locale from UI

On your JSP or HTML page, add language switch links:

<a href="?request_locale=en">English</a> |
<a href="?request_locale=es">Español</a>

When clicked, Struts2 captures request_locale and switches the locale accordingly.

Consume Resource Bundles in JSP

In Struts2 JSPs, use <s:text> tag to display localized text:

<s:text name="welcome.message"/>
<s:text name="label.username"/>
<s:text name="label.password"/>

Struts2 will automatically resolve the correct message based on the current locale.

Action Example

Your Struts2 Action can also use localized text:

public class LoginAction extends ActionSupport {

    public String execute() {
        String welcome = getText("welcome.message");
        addActionMessage(welcome);
        return SUCCESS;
    }
}

getText() fetches the localized message from the current resource bundle.

Changing Locale Programmatically

Sometimes you need to change the locale via logic (user selection on a form). You can do:

public String changeLanguage() {
    Locale locale = new Locale(selectedLang);
    ActionContext.getContext().setLocale(locale);
    return SUCCESS;
}

Then pass selectedLang from a drop-down (e.g., en, es, hi).

Formatting Dates and Numbers

Struts2 integrates with conversion and formatting based on locale. You can format numbers and dates transparently in JSP:

<s:property value="%{#attr.someDate, 'dd MMMM yyyy'}"/>
<s:property value="%{#attr.someNumber, '#,##0.00'}"/>

This respects current locale formats.

Best Practices (Senior Engineering Insight)

✔ Always centralize messages in resource bundles — avoid hardcoded strings.
✔ Use key naming conventions (e.g., label.*, msg.*) for clarity.
✔ Support fallback locales by providing default messages.
✔ Keep property files encoded in UTF-8 to avoid encoding issues.
✔ Load only required bundles to keep memory usage optimal.

Common Localization Issues & Fixes

Missing keys throw exceptions:
Provide default messages or include fallback resource bundles.

Language switch doesn’t persist:
Use Session or cookie to store request_locale parameters.

Encoding issues with special characters:
Ensure .properties files are saved in UTF-8 and Struts2 is configured accordingly.

Summary

Struts2 localization and internationalization allow your application to support multiple languages seamlessly. You define message bundles (.properties), configure resource paths in struts.xml, and use Struts2 tags to render localized content. With clean resource organization and proper locale resolution (via request_locale), you can deliver accurate text and formatting to users worldwide.

Visited 6 times, 1 visit(s) today
Close