22

I am trying to internationalize a UIBinder application with property files. Since we have already a lot of translations exposed by the com.google.gwt.i18n.client.Messages interface (GWT 1.7.1), we would like to reuse these messages.

I have tried the following:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
 xmlns:g="urn:import:com.google.gwt.user.client.ui" 
 xmlns:res="ui:with:be.credoc.iov.webapp.client.MessageConstants">

 <g:HTMLPanel>
  <div>
   <res:msg key="email">Emaileke</res:msg>:
   <g:TextBox ui:field="email" />
  </div>
 </g:HTMLPanel>
</ui:UiBinder>

The MessageConstants class looks like this:

@DefaultLocale("nl")
public interface MessageConstants extends Messages {
 String email();
}

However, this does not work. How can I do this?

Jan
  • 1,306
  • 2
  • 10
  • 25

3 Answers3

29

Update:

Since Igor wrote his answer, there has been a new method of using messages in ui binder.

<span>This <ui:text from="{msgRes.message}" /> has been internationalized</span>

This method uses GWT's text resource UiBinder integration

logan
  • 3,416
  • 1
  • 33
  • 45
  • 1
    If you'd improve your answer with a link to documentation, I'm sure you'd get the deserved upvotes :) (I wanted to edit and add the link myself, but after a quick glance at the docs, I can't seem to find the method you are referring to). – Igor Klimer Oct 18 '11 at 18:11
  • Thanks for the feedback Igor. I'm not sure where I found this trick, but it definitely wasn't in the documentation. Do you know anyone who works at google who I could bug to get this added to the GWT docs? – logan Oct 18 '11 at 23:10
  • 1
    Wish I knew ;) But you can reach the GWT developers through the [GWT discussion list](http://groups.google.com/group/google-web-toolkit) or open a new ticket in their [bugtracker](http://code.google.com/p/google-web-toolkit/issues/list). – Igor Klimer Oct 22 '11 at 18:54
  • after about 2 days of searching I found your answer and it's working perfectly thanks (i can give only +1 ) – nidomiro Jul 04 '12 at 14:21
  • @logan, how can I access parameterised messages? – Konstantin Milyutin Nov 06 '14 at 08:01
13

Update:
please check logan's answer below for a solution available in recent versions of GWT.


I had (actually, have) the same problem as you do - after migrating to GWT 2.0 I had a properties file I wanted to use in my UiBinder files. Unfortunately, I couldn't get it to work like I wanted to - it seems the GWT devs want people to use the syntax described in the i18n guide for UiBinder - where a Message interface is created during compile for every UiBinder template, etc.

Anyway, you can still use external Messages interfaces like this:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:with field='cc' type='path.to.i18n.SomeMessages'/>
    <ui:with field='res' type='path.to.resource.ResourceBundle'/>
    <ui:style>
        .required {
      color: red;
      font-weight: bold;
    }
    .right {
      text-align: right;    
    }
    .textBox {
      margin-right: 7px;
    }
    </ui:style>
    <g:HTMLPanel styleName='{res.style.form} {res.style.mbox}' ui:field='mainPanel'>
        <h2 ui:field='loginHeader' />
      <h3 ui:field='loginLabel' />
      <div class='{style.textBox}'><g:TextBox ui:field="loginBox" /></div>
      <h3 ui:field='passwordLabel' />
    <div class='{style.textBox}'><g:PasswordTextBox ui:field="passwordBox" /></div>
    <div><g:CheckBox ui:field='rememberMeCheckBox' text='{cc.rememberMeCheckboxText}' /></div>
    <div class='{style.right}'><g:Button ui:field='submitButton' text='{cc.loginSubmitButtonText}' /></div>
    </g:HTMLPanel>
</ui:UiBinder> 

Though, that only works for stuff like Button's title, etc, not content of divs :/ You can fill those in the class behind the UiBinder template, but there should be a better way.

I was hoping that you could set the inner HTML via innerHTML (since UiBinder should recognize that method and allow to set it's value via XML/UiBinder template) but alas, last time I checked it didn't work :/

Community
  • 1
  • 1
Igor Klimer
  • 15,321
  • 3
  • 47
  • 57
8

There is a known issue with the UiBinder in combination with internationalization, see also this recent thread on the gwt bugtracker: http://code.google.com/p/google-web-toolkit/issues/detail?id=4355.

In comment 4 a solution is given:

If your UiBinder file is called, say, 'LoginView.ui.xml' then call the properties file LoginViewLoginViewUiBinderImplGenMessages.properties (yes 'LoginView' appears twice) and put it next to the view's java file in the source package, so altogether you'll have 3 files:

LoginView.ui.xml
LoginView.java
LoginViewLoginViewUiBinderImplGenMessages.properties
Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • Thanks, nice info in the comments on that issue - the whole i18n with UiBinder seems still rough on the edges to me, I wish the GWT team would make it less "hacky". – Igor Klimer Feb 18 '10 at 11:31
  • You may also want to check out the python script proposed with GWTP to centralize your localized strings in the same file: http://code.google.com/p/gwt-platform/wiki/MergeLocale – Philippe Beaudoin Aug 20 '10 at 18:22