Share / Save this...

Share/Bookmark

2010-06-30

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




<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

The annotations tell the GWT compiler which method accesses which key. That's all we need to do to access a key inside the properties file, just add an access method and annotate it.

Finally to test all our configuration we're going to modify our EntryPoint to make use of the internationalized messages by asking GWT to create an implementation of our interface, but don't worry because all it takes is one line in our GWT code. I declared an attribute in the EntryPoint named messageResource of type MessageResource in this way:

MessageResource messageResource = (MessageResource) (GWT.create(MessageResource.class)); 

and now you can use that object to call the appropriate translated message by calling each declared method. In my example i have a label and a button that toggles the label when clicked. I removed the static text from the button and replaced it with messageResource.getClickMe() and for the label i did the same with the messageResource.getHelloGWT() method. So it should read:

final Label label = new Label(messageResource.getHelloGWT());
final Button button = new Button(messageResource.getClickMe());

That's how you manage to get the internationalized version of your messages from GWT. There's just one more thing you need to do to close the circle and that is associate the locale parameter to your Struts locale when it's present, because it takes precedence over the gwt:property defined in the meta tag. To do it just place the following code before the <html:html lang="true"> tag:

<logic:present parameter="locale">
<% request.getSession().setAttribute("org.apache.struts.action.LOCALE", new Locale(request.getParameter("locale"))); %>
</logic:present>

Now if the locale parameter is present, then Struts will use that setting and if it's not present then GWT will use whatever Struts is using. They're collaborating to present the information in the same way. Now you could create a menu to choose languages using the GWT facilty that returns all the available languages (just an idea).

After compiling you should have a perfectly synchronized locale between both Struts and GWT and an easily internationalized application, just drop a new properties file with a new language and declare it in the gwt.xml file and you're set. You have access to the message resources from both the Struts' tags and your GWT Messages interface.

The steps followed in this part of the tutorial are:
  • Configure the module's gwt.xml file to use GWT's i18n support and declare languages.
  • Tie Struts' i18n locale detection and set it as the current locale in GWT by editing JSPs.
  • Create Messages, Contants or ConstantsWithLookUp sub-interfaces for the language properties files.
  • Use your interface to access the key/value pairs by declaring methods in it and using @annotations.

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