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 :
- Login.java
- Login_en.properties and Login_hi.properties
- struts.xml
- index.jsp
- 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
Output
Now let’s change the language code. Here, we are using chrome browser.
Click on the settings.
Now, click on the show advanced settings.
Now, click on the language and input settings.
Now, add hindi language and move it to the top side and then click on done.
If again, you run the application, it will show hindi message.
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.