Share / Save this...

Share/Bookmark

2010-06-28

GWT + Struts 1.x + Netbeans Tutorial (Part 6)




<c:choose>
     <c:when test="${user.likes}"
          Share this with your friends
     </c:when>
     <c:otherwise>  
          Send me feedback.
     </c:otherwise>
<:choose>
oDesk Certified Java Developer

Part 6: Add I18N support to a Struts 1.x application.

TL;DR: move the MessageResource.properties file(s) to the "client" package. I used a package called "i18n" inside it, so mine points to:
com.example.strutsgwt.client.i18n.MessageResource
also modify struts-config.xml to reflect the change.

You might think this is a joke, because Struts has a very simple and straightforward way to add i18n support. Netbeans creates each new Struts project already having a default properties file (in English) which you can simply copy and rename, adding the appropriate language and country codes to the end, to add a new language to your application. So, is this a joke? No, this is not a joke.

This is a foreword to the i18n support in both Struts and GWT and how they work. The title should read "Adding i18n support to Struts in preparation for GWT".

Now that we have that clear, we need to know a few things about how Struts and GWT each handle i18n support. Struts uses properties files in a key=value format for all definitions we may want to have internationalized and you can also put placeholders for any dynamic strings you want to pass "as is" into the internationalized version.  GWT uses 2 interfaces: Constants and Messages (there are other classes too, read the API). The first works just as the key=value properties file in Struts and the second works like the  key=value properties with placeholders. They are pretty similar, right? Yes, they are. In fact what we're going to do is prepare the Struts project to accept the GWT i18n out of the box and also from the GWT point of view we're going to use the existing i18n support already provided by the Struts project.

GWT compiles everything in the "client" package into the JavaScript permutations, or so i think (please correct me if i'm wrong in the comment section). Therefore, to be able to use the i18n properties files from Struts we need to place those files into the "client" package. When you created the Struts project you were asked where to put the resources.

It was something like com.example.strutsgwt.MessageResource and you might have changed it a bit. So, now navigate to that package in your source structure.

I'm going to use the following structure to keep things tidy: com.example.strutsgwt.client.i18n. So create those packages and place your provided properties files inside.

After you move those files, you need to configure Struts to find them at the new location. You configure Struts to find the location of the message resources file (which i'm calling MessageResources.properties) by modifying the struts-config.xml file and changing the <message-resources> tag to <message-resources parameter="com/example/strutsgwt/client/i18n/MessageResources" /> and check everything works by compiling and testing.

You can then go ahead and add a new language by copying the MessageResources.properties file to the same package, renaming that file to MessageResources_es.properties or MessageResources_fr.properties (or your favorite language code) and modify the contents to the correct translation for each key that is found in the default file.

You should now have a working internationalized Struts web application project which is ready for GWT.


If you have any suggestions, ideas for full blown system integration (sb) or comments, leave a message below.

I would appreciate if you could Share this with your friends



No comments:

Post a Comment

Hits