Tuesday, May 22, 2012

UiBinder and i18n of gwt-maven-plugin based project

In my current project we are actively using gwt-maven-plugin. In particular, I'm happy with the plugin feature Generate i18n interfaces for message bundles, i.e., we have property files with localized labels, and gwt-maven-plugin generates interface Messages containing all labels in GWT-style (annotated java methods):
public interface Messages extends com.google.gwt.i18n.client.Messages {
  /**
   * Translated "Birth Date:".
   * 
   * @return translated "Birth Date:"
   */
  @DefaultMessage("Birth Date:")
  @Key("birthDate")
  String birthDate();

  /**
   * Translated "Cancel".
   * 
   * @return translated "Cancel"
   */
  @DefaultMessage("Cancel")
  @Key("cancel")
  String cancel();
  
  ...
}

We started making project UI in java classes but recently changed our mind to use the UiBinder and xml-based layout. Ok. But how to internationalize *ui.xml file and preserve labels in property files?

Google documentation for the GWT sometimes is so lean that only Google Search may help. And, thank you God, you'd created stackoverflow.com! Mature guys posted there a solution:
first import generated Messages interface in your ui.xml file (note the alias 'res' for the imported resource)
<ui:uibinder xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:ui="urn:ui:com.google.gwt.uibinder">

  <ui:with field="res" type="com.my.app.client.Messages"/>
  ...
and then use it like this (get the label for the key 'newOrder')
<g:button ui:field="newOrderButton">
  <ui:text from="{res.newOrder}"/>
</g:button>

P.S. Here are doc remains from the official Google on this topic.

No comments:

Post a Comment