Struts Framework has built-in support for Internationalization. In other words, you don't need to worry about writing pages in different languages. All you have to do is to create a resource file for each language that you want. The resource file will contain titles, messages, and other text in the language of your user.
Let's suppose that users in Italy will be accessing your site along with users in the United States. You, of course, want to greet the Italian users in Italian. To do this, you will be creating two resource files, one for an English greeting and one for an Italian greeting. The two resource files will be:
ApplicationResources.properties ApplicationResources_it.propertiesNote the _it in the second file. It means that if the user's locate is Italian, then read all resources from this file. The general format of this file is ApplicationResources_xx.properties, where xx stands for the country locale.
Enough theory, let's go ahead and work through an example. We will show you a very simple example, but this example will give you all you need to add internationalization to your application.
We will create just one JSP page, with a title. The title will be displayed in either English or Italian depending on user locale. We will be changing our locale to see how the example works.
Go ahead and lunch Struts Studio. If you don't have Struts Studio installed, please refer to the readme file and to http://www.StrutsStudio.com for installation steps.
Click on the Create New Project button. For the name of the project, enter strutsI18N. Leave all other fields with their default values making sure the Blank template is selected. Click Finish. (You can also click Next-> to see the other steps in creating a new Struts project, but we will not be changing any values in those steps.)
Now, you should have a new Struts project in the Eclipse Package Explorer window. Next, we will go ahead and open the Web Flow for editing. To open the Web Flow, browse to strutsI18N/WebContent/WEB-INF/struts-config.xml and double-click it.
We are ready to create our JSP Page. To do this, just right-click anywhere in the Diagram view and select Add/Page.... For the name of the JSP page, enter: welcome. Right-click the new JSP page on the Web Flow diagram and select Open Page. This will open the file for editing.
Let's now use the Struts Palette to insert JSP tags into our page. If the Struts Palette does not already appear on the right side of the JSP page, select Window/Show View/Struts Palette from the menu bar.
Using the Palette, you cans start to insert JSP tags. Click the Common item and insert the JSP Page snippet code from the list of displayed "Common" snippets. Then, place the mouse cursor in between the <body> and /<body> tags. After this, click the Bean item and insert the Message snippet code. Finally, in the <bean:> tag, add our language dependent value as a parameter.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html locale="true"> <head> <title>i18n</title> </head> <body> <h2><bean:message key="page.title"/></h2> </body> </html:html>
Great, that's all we have to do for our JSP page. Next, we are going to create two resources that will contain titles in different languages.
We need to create the resource files where the built-in Ant script will automatically copy them to the classes folder so that the Runtime can find them. So, right-click JavaSource and select New/Properties File. Create the following two files:
ApplicationResources ApplicationResources_itThe first one will hold a title in English and the second one in Italian. (If you would prefer to create these files in a different location outside of JavaSource, modify the Ant build script so it will pick them up during the build and copy them to the classes folder.
Now, you need to edit the resource files. Add this value to the ApplicationResources.properties file:
page.title=Thank you for visiting!and add this value to ApplicationResources_it.properties file:
page.title=Grazie per la vostra visita!
One last thing that we need to do is to add a reference to our resources file in the struts-config.xml file. Select the Tree viewer tabbed in the editor for this file. Right-click the Resources node in the tree and select Define Message Resources.... For Parameter value, click Change... and browse to your resource file at strutsI18N/ApplicationResources.properties. Select it and click Ok. Leave all other values empty and click Finish.
Please note that we only need to add the default resource file to struts-config.xml. When we change the locale, the appropriate resource file (ApplicationResources_xx.properties) will be looked up automatically.
Great! We are almost done. Save all changes.
Now we just need to compile our project. We don't have any Java classes, but we will still build our project so that our resources files will be copied. To compile, click the button shown below in the Eclipse toolbar. You can check that the resource files were copied to classes folder by browsing to strutsI18N/WebContent/WEB-INF/classes.
Start Tomcat server by clicking the green arrow in the Servlet Engine Control toolbar in Eclipse. Go back to the diagram of struts-config.xml. Right-click the JSP page and select Run. You should see the following page.
Let's now change our locale to Italian so that we can see page in Italian. To change the locale on your browser.
Tools->Internet Options...->Languages...->Add...Then, locate Italian (Italy) [it] and move it to the top of the list.
Edit->Preferences...->Navigator/Languages...->Add...Then, locate Italian [it] and move it to the top of the list.
After you have changed the locale, close the browser and run the application again. You should now get a screen like the above with the message Grazie per la vostra visita.