【问题标题】:GWT UiBinder I18nGWT UiBinder I18n
【发布时间】:2015-05-18 08:38:37
【问题描述】:

正如标题所说,我需要一些关于 GWT 的 i18n 与 UiBinder 一起使用的帮助。我想使用静态 i18n 国际化我的应用程序。我用来学习的书只提供了一种国际化 ui.xml 文件的方法,方法是让编译器为常量/消息和默认文件生成键,但必须有一种更简单的方法来做到这一点。这就是为什么我尝试使用这样的 ui:with 标签来使用我的国际化常量(在 upFace 内部):

<ui:with type="havis.ui.shared.resourcebundle.ConstantsResource" field="lang"></ui:with>    
<g:ToggleButton ui:field="observeButton">
        <g:upFace>{lang.observe}</g:upFace>
        <g:downFace>Observing</g:downFace>
</g:ToggleButton>

这不起作用,按钮显示文本 {lang.observe} 这似乎也是合乎逻辑的,但现在我的问题是:有没有办法使用这样的常量?如果没有,有人可以解释我应该如何在 UiBinder 文件中使用常量(无需编译器生成文件和密钥)?

【问题讨论】:

    标签: java gwt


    【解决方案1】:

    在任何接受 HTML 的地方(例如在upFace 内),您都可以使用&lt;ui:msg&gt;&lt;ui:text&gt;&lt;ui:safehtml&gt;(在任何需要纯文本的地方,您都可以使用&lt;ui:msg&gt;&lt;ui:text&gt;)。

    所以在你的情况下:

    <ui:with type="havis.ui.shared.resourcebundle.ConstantsResource" field="lang"></ui:with>    
    <g:ToggleButton ui:field="observeButton">
        <g:upFace><ui:text from="{lang.observe}"/></g:upFace>
        <g:downFace>Observing</g:downFace>
    </g:ToggleButton>
    

    请参阅 http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Hello_Text_Resourceshttp://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Hello_Html_Resources 关于 ui:textui:safehtml

    【讨论】:

      【解决方案2】:

      你可以像这样使用常量:

      .ui.xml:

      <ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'>
      <ui:with field="constants" type="my.client.resources.AppResources.AppConstants"/>
      
      <g:FlowPanel>
        <g:Label text="{constants.label}"/>
      </g:FlowPanel>
      

      和 AppResources 接口:

      public interface ApplicationResources extends ClientBundle {
      
      public static final ApplicationConstants CONSTANTS = GWT.create(ApplicationConstants.class);
      
        public interface ApplicationConstants extends com.google.gwt.i18n.client.Constants {
      
          @DefaultStringValue("my label")
          String label();
        }
      }
      

      但是对于 i18n,您应该真正遵循 GWT 手册所说的内容,即 除了准备所有属性文件(每种语言一个)并生成所有必需的排列之外,没有其他(干净的)方法。这主要代表 到 GWT 所有与语言检测相关的东西,以及提供的解决方案 GWT 在运行时执行得非常好。唯一的缺点是编译时间 稍微高一点(因为您将在您指定的每种语言中为每个浏览器进行排列)。

      【讨论】:

      • 我已经发现了这种方式,但我不太喜欢它/:但感谢您的帮助,我很感激(:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多