Home » Struts 2 with i18n example tutorial

Struts 2 with i18n example tutorial

by Online Tutorials Library

Struts 2 with i18n example Tutorial

The i18n interceptor provides multi-lingual support for your application.

It handles setting locale for the action. It can be used if user wants to set his/her locale and get data according to the locale provided.

It is found in the defaultStack bydefault, so you don’t have to specify it explicitly.


Parameters of i18n interceptor

There are 2 parameters defined for i18n interceptor. Both are optional.

Parameter Description
parameterName It specifies the name of the HTTP request parameter. It is set to request_locale bydefault.
attributeName specifies the name of the session key to store the locale. It is WW_TRANS_I18N_LOCALE bydefault.

Example of i18n interceptor

In this example, we are creating following pages :

  1. Login.java
  2. Login_en.properties and Login_hi.properties
  3. struts.xml
  4. index.jsp
  5. login-success.jsp

1) Create the action class

To use the i18n interceptor, you need to extend the ActionSupport class that implements TextProvider.

Login.java


2) Create properties files

Now create 2 properties file inside the package. Its name should be actionname_languagecode.properties.

Login_en.properties

Login_hi.properties


3) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name from the user.

index.jsp


Define action in struts.xml

struts.xml


4) Create view component

Now use text tag to get the data. It is used in i18n.

login-success.jsp


Directory Structure in Eclipse IDE

i18n directory structure



Output

struts 2 i18n example output 1 struts 2 i18n example output 2


Now let’s change the language code. Here, we are using chrome browser.

Click on the settings.

struts 2 i18n example output 3


Now, click on the show advanced settings.

struts 2 i18n example output 4


Now, click on the language and input settings.

struts 2 i18n example output 5


Now, add hindi language and move it to the top side and then click on done.

struts 2 i18n example output 6


If again, you run the application, it will show hindi message.

struts 2 i18n example output 7 struts 2 i18n example output 8


If you use request_locale property name in index.jsp file, message will be shown according to the given language code.

Let’s see the output.

struts 2 i18n example output 9 struts 2 i18n example output 10


struts 2 i18n example output 11 struts 2 i18n example output 12

You may also like