Share / Save this...

Share/Bookmark

2010-06-30

GWT + Struts 1.x + Netbeans Tutorial (Part 7a)




<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 7: Tying the I18N support from both GWT and Struts.

We have a working Struts application with regular page navigation, and GWT support, which is also i18n aware. This is important to keep in mind, it's ONLY i18n aware, in the sense that it knows about different locales. If you access you application from a web browser with an accept-language header different from "en" there are some glitches in the GWT display, it doesn't detect the language quite right.
It uses the "default" locale, which is English. I don't know if this just happened to myself or if it's a glitch in general. I will treat it as a general issue, because the fix i propose is also a nice-to-have feature, as usual.

I want both my Struts application and my GWT modules to keep in sync of what languages each one of them is using. Since changing the Struts language is easier i will make the GWT language a slave of the Struts language. This is something you would do if you have an existing i18n Struts application to which you just added GWT support. My application in this tutorial is a new application as well, and the steps needed to synchronize both views to the same language requires only a few lines of code in the JSPs, we also need to configure our gwt.xml file for this module to inherit the i18n classes and finally we're going to make our message resources available to GWT, just as it is available to Struts, but with a nice improvement which is provided by the i18n module in GWT.

In the previous section i mentioned the Constants and Messages interfaces, which provide utilities to internationalize an application. We're going to build a class that finds our international messages automatically from the given keys. GWT also comes with a command line tool called i18nCreator which can help us create this interface for us automatically. I'm gonna do it manually, since the procedure for the i18nCreator is pretty straightforward and requires little input. I recommend you read about i18n and i18nCreator in the GWT documentation and use it as a way to save you both time and patience.

I'm assuming our language translations are located in the com.example.strutsgwt.client.i18n package. Our entry point is located in the com.example.strutsgwt.client package. Our module gwt.xml file is located in the com.example.strutsgwt package. Our JSPs in the usual location.

The first thing we're going to do is configure our module's gwt.xml file by inheriting the i18n module. To achieve this we're going to edit it by adding the following after our <module> tag:

<inherits name="com.google.gwt.i18n.I18N"/>

Now we need to declare the languages we're going to use, i wish it could be dynamic, and i suspect there's a way to do it which escapes me, so if anyone knows how, please post below and I'll add it here (with correct attribution of course). Now I'll give you an example of the code you need to place inside your gwt.xml file to declare your languages:

<!-- Internationalization support. -->
<extend-property name="locale" values="en"/>
<extend-property name="locale" values="es"/>
<set-property-fallback name="locale" value="en"/>

I'm going to explain these lines and in this way you're going to adapt it to your case. I declared 2 languages: en and es (English and Spanish, respectively) and in the last line i declared the "default" language, which in this case is en (English). You can declare your own languages in this fashion and remember to read the documentation for the GWT locales.


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