interceptor is responsible for i18n
In Struts2, the i18nInterceptor is responsible for providing Internationalization (i18n) and Localization (L10n) support within the framework. This interceptor ensures that applications can display content in different languages based on the user’s locale preferences, as well as format dates, numbers, and currencies according to locale-specific conventions.
Table of Contents
i18nInterceptor
The I18nInterceptor performs the following tasks:
- 1. Â Locale Detection : It detects the user’s locale preference based on request parameters, session attributes, browser settings, or configurations.
- 2. Â Resource Bundle Lookup : It retrieves localized messages and resources from property files (.properties) based on the detected locale.
- 3. Â Content Localization : It substitutes placeholders in JSPs or other templates with appropriate localized messages and formats.
Java Example
Let’s demonstrate how the I18nInterceptor works in a Struts2 application.
java
package com.example.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
// Retrieve message from resource bundle based on current locale
message = getText("hello.message");
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Explanation :
- HelloWorldAction.java : This action class retrieves a localized message using the getText() method provided by ActionSupport. The getText() method retrieves the message from a resource bundle (messages.properties or similar files) based on the current locale.
xml
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="i18n" class="org.apache.struts2.interceptor.I18nInterceptor"/>
<interceptor-stack name="defaultStack">
<interceptor-ref name="i18n"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="helloWorld" class="com.example.action.HelloWorldAction" method="execute">
<result name="success">/hello.jsp</result>
</action>
<!-- Other action configurations -->
</package>
</struts>
Explanation :
- I18nInterceptor Configuration : The i18n interceptor is configured in the defaultStack interceptor stack. This ensures that the I18nInterceptor is invoked before other interceptors in the stack, allowing for localization of messages before rendering the action result.
Benefits
- Localization : Provides seamless support for displaying content in multiple languages based on user preferences.
- Standardization : Ensures consistent handling of internationalization across the application, simplifying maintenance and updates.
- Configurability : Allows customization of locale detection and resource bundle lookup strategies to suit application requirements.
Conclusion
The I18nInterceptor in Struts2 facilitates internationalization and localization of applications by managing locale detection, resource bundle lookup, and content substitution. It ensures that applications can adapt to different language and cultural preferences, enhancing user experience in globalized environments.