【问题标题】:How can I internationalize a GWT UIBinder page with Messages?如何使用 Messages 国际化 GWT UIBinder 页面?
【发布时间】:2011-01-18 06:20:42
【问题描述】:

我正在尝试使用属性文件将 UIBinder 应用程序国际化。由于我们已经通过 com.google.gwt.i18n.client.Messages 接口 (GWT 1.7.1) 公开了很多翻译,因此我们希望重用这些消息。

我尝试了以下方法:

<!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>

MessageConstants 类如下所示:

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

但是,这不起作用。我该怎么做?

【问题讨论】:

    标签: gwt internationalization uibinder


    【解决方案1】:

    更新:

    自从伊戈尔写下他的答案后,就有了一种在 ui binder 中使用消息的新方法。

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

    此方法使用GWT的text resource UiBinder integration

    【讨论】:

    • 如果您想通过文档链接改进您的答案,我相信您会得到应得的支持 :) (我想自己编辑和添加链接,但在快速浏览后在文档中,我似乎找不到您所指的方法)。
    • 感谢伊戈尔的反馈。我不确定我在哪里找到了这个技巧,但它肯定不在文档中。你知道在 google 工作的任何人,我可以将它添加到 GWT 文档中吗?
    • 希望我知道 ;) 但是您可以通过 GWT discussion list 联系 GWT 开发人员或在他们的 bugtracker 中打开新票。
    • 经过大约 2 天的搜索后,我找到了您的答案,而且效果很好,谢谢(我只能给 +1)
    • @logan,我如何访问参数化消息?
    【解决方案2】:

    更新:
    请查看下方的logan's answer,了解最新版本的 GWT 中可用的解决方案。


    我有(实际上,有)和你一样的问题——在迁移到 GWT 2.0 之后,我有一个属性文件,我想在我的 UiBinder 文件中使用。不幸的是,我无法让它像我想要的那样工作 - 似乎 GWT 开发人员希望人们使用i18n guide for UiBinder 中描述的语法 - 在编译期间为每个 UiBinder 模板创建一个 Message 接口,等等。

    无论如何,您仍然可以像这样使用外部Messages 接口:

    <!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> 
    

    不过,这仅适用于 Button 的标题等内容, div 的内容:/ 您可以在 UiBinder 模板后面的类中填写这些内容,但应该有一个更好的方法。

    我希望您可以通过 innerHTML 设置内部 HTML(因为 UiBinder 应该识别该方法并允许通过 XML/UiBinder 模板设置它的值)但是唉,上次我检查它不起作用:/

    【讨论】:

      【解决方案3】:

      UiBinder 与国际化结合存在一个已知问题,另请参阅 gwt bugtracker 上的此最新帖子:http://code.google.com/p/google-web-toolkit/issues/detail?id=4355

      comment 4 中给出了解决方案:

      如果你的 UiBinder 文件被调用,比如说, 'LoginView.ui.xml' 然后调用 属性文件 LoginViewLoginViewUiBinderImplGenMessages.properties (是的 'LoginView' 出现两次)并将其放在视图的 java 旁边 文件 在源代码包中,因此您总共将拥有 3 个文件:

      LoginView.ui.xml
      LoginView.java
      LoginViewLoginViewUiBinderImplGenMessages.properties
      

      【讨论】:

      • 谢谢,关于这个问题的 cmets 提供了很好的信息 - 带有 UiBinder 的整个 i18n 对我来说似乎仍然很粗糙,我希望 GWT 团队能减少它的“hacky”。​​
      • 您可能还想查看与 GWTP 一起提出的 python 脚本,以将您的本地化字符串集中在同一文件中:code.google.com/p/gwt-platform/wiki/MergeLocale
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多