#summary GWT UiBinder i18n use cases = GWT UiBinder Internationalization Use Cases = Ray Ryan
freely plagiarizing from the brilliant minds behind [http://code.google.com/p/gxp/ gxp] This document explores internationalization use cases supported the GWT UiBinder. = Background = The basic idea is that the localizable bits of your ui.xml template file are marked up as such. When the UiBinder code generator processes the template, it gathers these and creates an interface extends com.google.gwt.i18.client.Messages. You then provide localized versions of these messages as you would for any other portion of your app. (See the Binding to Properties Files section of the [http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/i18n/client/Messages.html Messages javadoc].) Values for annotations that apply at the Messages interface level are set as attributes on the root element. Here is an example with all possible values set (normally you'd let some of these default). {{{ }}} = Open Issues = Support for Constants and ConstantsWithLookup is not yet designed. PluralText is theoretically supported, but untested. So far, I have found no use for Gxp's element. Is it really not needed, or do I just lack imagination? Unannotated user visible text is allowed--a feature is needed to make this a compile time error. = Complete sentence or headline with no internal tags. = *Original* {{{

User Account Management.

}}} *Tagged* {{{

User Account Management.

}}} *Generated* {{{ public class MyUiBinderImpl implements MyUiBinder { static final MyUiMessages messages = GWT.create(MyUiMessages.class); void bindUi(MyUi myUi) { HTML html = new HTML(); html.setHtml("

" + messages.message1() + "

"); myUi.setElement(html.getElement()); } } @DefaultLocale("en_US") @GenerateKeys @GeneratedFrom("com.foo.bar.MyUi.ul.xml") public interface MyUiMessages extends com.google.gwt.i18.client.Messages { @DefaultMessage("User Account Management.") @Description("Title of UAM page") String message1(); } }}} This is the hello world case‚ a simple, unparametrized string. = Simple HTML tags inside message = *Original* {{{ We strongly urge you to reconsider. }}} *Tagged* {{{ We strongly urge you to reconsider. }}} *Generated* As above Simple formatting is reasonable to put in front of a translator, and so UiBinder supports html in messages, not just text. = Messages with unclobberable portions = *Original* {{{
Colgate, with MFP!TM
}}} *Tagged* {{{
Colgate, with MFP!TM
}}} *Generated* {{{ public class MyUiBinderImpl implements MyUiBinder { static final String MESSAGE1_BRANDED_SPAN_OPEN = ""; static final String MESSAGE1_BRANDED_SPAN_CLOSE = ""; static final String MESSAGE1_TRADEMARK = "TM"; static final MyUiMessages messages = GWT.create(MyUiMessages.class); static final MyUiMessages messages = GWT.create(MyUiMessages.class); public void bind(MyUi myUi) { HTML html = new HTML(); html.setHtml("
" + messages.message1(MESSAGE1_BRANDED_SPAN_OPEN, MESSAGE1_BRANDED_SPAN_CLOSE, MESSAGE1_TRADEMARK) + "
"); } } @DefaultLocale("en_US") @Description("blurb") @GenerateKeys @GeneratedFrom("com.foo.bar.MyUi.ul.xml") public interface MyUiMessages extends com.google.gwt.i18.client.Messages { @DefaultMessage("{0}Colgate{1}, with MFP!{2}") String message1( @Example(MyUiBinderImpl.MESSAGE1_BRANDED_SPAN_OPEN) String brandedSpanOpen, @Example(MyUiBinderImpl.MESSAGE1_BRANDED_SPAN_CLOSE) String brandedSpanClose, @Example(MyUiBinderImpl.MESSAGE1_TRADEMARK) String trademark ); } }}} There are two examples in here. First, you see a {{{ui:ph}}} attribute that can be added to any child of a {{{ui:msg}}}, to indicate that placholders should be created to protect it from translators. Two placeholders are created, for the opening and closing tags of the element (in this case, brandedSpanOpen and brandedSpanClose). Second, we see an element, also named {{{ui:ph}}}, that can surround an arbitrary bit of markup to be protected in its entirety (in this case, the trademark placeholder). So, you have both an element **to surround untranslatable runs**, and the attribute{{{<}}}/span> to put in arbitrary elements to hide their begin and end tags from translators, but keep their content as part of the message. = Messages with runtime computed values = *Original* {{{ (closed through ) }}} *Tagged* {{{ (closed through ) }}} The element is necessary to mark the span as opaque to the translator. Without it, she'd be allowed to fill in the body of the span. The {{{example}}} attribute is optional, and allows more useful text to be provided in the {{{@Example()}}} attribute of the generated Mesages interface (and hence to the translator). = Message containing widgets (HTMLPanel only) = *Original* {{{ Meeting starts at and ends at . }}} *Tagged* {{{ Meeting starts at and ends at . }}} Note that there is no {{{ui:ph}}} attribute on the widgets. There's no need for them, as there is no ambiguity about what must be done when a widget shows up in the middle of a message. Note also that you can only do this kind of thing (widgets in messages) inside of an HTMLPanel, the only widget in the GWT collection that intermixes markup and child widgets. = Message containing widgets with text bodies (HasText or HasHtml inside HTMLPanel) = *Original* {{{ To do the thing, click here and massage vigorously. }}} *Tagged* {{{ To do the thing, click here and massage vigorously. }}} *Generated* {{{ @Default("To do the thing, {0}click here{1} and massage vigorously.") String message536( @Example("") String startWidget, @Example("") String endWidget ); HTMLPanel panel = ..." + messages.message536("", "") + "... HyperLink link = new HyperLink(); link.setHistoryToken("/doThe#thing"); link.setText(panel.getElementById("xyz").getInnerText()); panel.addAndReplace(link, "xyz"); }}} Again, only an HTMLPanel can intermix markup and widgets, and so only an HTMLPanel can hold a {{{}}} in this style. Further, only the bodies of HasHTML and HasText children can be recognized as part of the message. = HTML attributes that need translation = *Original* {{{ Keyword }}} *Tagged* {{{ Keyword }}} *Generated* {{{ @DefaultLocale("en_US") @GenerateKeys @GeneratedFrom("com.foo.bar.MyUi.ul.xml") public class MyUiBinderImpl implements MyUiBinder { static final MyUiMessages messages = GWT.create(MyUiMessages.class); void bindUi(MyUi myUi) { HTML html = new HTML(); html.setHtml("