91-9990449935 0120-4256464 |
Struts 2 with i18n example TutorialThe 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 interceptorThere are 2 parameters defined for i18n interceptor. Both are optional.
Example of i18n interceptorIn this example, we are creating following pages :
1) Create the action classTo use the i18n interceptor, you need to extend the ActionSupport class that implements TextProvider. Login.java2) Create properties filesNow create 2 properties file inside the package. Its name should be actionname_languagecode.properties. Login_en.properties Login_hi.properties3) Create index.jsp for inputThis jsp page creates a form using struts UI tags. It receives name from the user. index.jspDefine action in struts.xmlstruts.xml4) Create view componentNow use text tag to get the data. It is used in i18n. login-success.jspDirectory Structure in Eclipse IDEOutputNow 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. |