【问题标题】:How to implement i18n in GWT application?如何在 GWT 应用程序中实现 i18n?
【发布时间】:2012-05-19 16:03:23
【问题描述】:

我在国际化方面遇到了问题。我正在尝试在我的 GWT 应用程序中实现对两种语言的支持。不幸的是,我从来没有找到一个完整的例子,如何在 UiBinder 的帮助下做到这一点。这就是我所做的:

我的模块I18nexample.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='i18nexample'>
<inherits name="com.google.gwt.user.User" />
<inherits name='com.google.gwt.user.theme.clean.Clean' />
<inherits name="com.google.gwt.i18n.I18N" />
<inherits name="com.google.gwt.i18n.CldrLocales" />
<entry-point class='com.myexample.i18nexample.client.ExampleI18N' />
<servlet path="/start" class="com.myexample.i18nexample.server.StartServiceImpl" />
    <extend-property name="locale" values="en, fr" />
    <set-property-fallback name="locale" value="en" />
</module>

我的界面Message.java

package com.myexample.i18nexample.client;

import com.google.gwt.i18n.client.Constants;

public interface Message extends Constants {

    String greeting();
}

同一个包com.myexample.i18nexample.client有三个属性文件:

Message.properties

greeting = hello

Message_en.properties

greeting = hello

Message_fr.properties

greeting = bonjour

我的 UiBinder 文件Greeting.ui.xml

<!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:generateFormat="com.google.gwt.i18n.rebind.format.PropertiesFormat"
    ui:generateKeys="com.google.gwt.i18n.rebind.keygen.MD5KeyGenerator"
    ui:generateLocales="default" >
    <ui:with type="com.myexample.i18nexample.client.Message" field="string" />
    <g:HTMLPanel>   
        <ui:msg key="greeting" description="greeting">Default greeting</ui:msg>     
    </g:HTMLPanel>
</ui:UiBinder> 

当应用程序启动时,我总是在浏览器中得到输出:

Default greeting

为什么?我究竟做错了什么?

我尝试从不同的 URL 运行应用程序:

http://127.0.0.1:8888/i18nexample.html?gwt.codesvr=127.0.0.1:9997

http://127.0.0.1:8888/i18nexample.html?locale=en&gwt.codesvr=127.0.0.1:9997

http://127.0.0.1:8888/i18nexample.html?locale=fr&gwt.codesvr=127.0.0.1:9997

结果不变。虽然我希望在最后一种情况下收到一条消息bonjour

例如,如果我使用g:Buttton 而不是消息ui:msg

<g:HTMLPanel>   
     <g:Button text="{string.greeting}" />      
</g:HTMLPanel>

然后我得到一个带有文本"hello"的按钮

如果我输入网址:

http://127.0.0.1:8888/i18nexample.html?locale=fr&gwt.codesvr=127.0.0.1:9997

按钮上的文本变为"bonjour"。在这里一切都按预期工作。但是为什么国际化在我的第一个案例中不起作用?

以及以下是否有区别:

<ui:msg description="greeting">Default greeting</ui:msg>

<ui:msg description="greeting">hello</ui:msg>

<ui:msg description="greeting"></ui:msg>

在这些情况下应该有不同的结果吗?如何正确书写?

请向我解释 GWT 中的国际化原则以及为什么我的示例不起作用。 任何建议将不胜感激。

【问题讨论】:

    标签: gwt internationalization gwt2 uibinder


    【解决方案1】:

    首先,文件应命名为Message_fr.properties(或Message_en.properties),而不是Message.properties_fr(或Message.properties_en)。

    然后ui:msg 等。在 UiBinder 中将生成一个接口(扩展com.google.gwt.i18n.client.Messages)),而不是使用您定义的接口。为此,您必须使用{string.greeting}(其中string 是您提供给ui:withui:field)。 UiBinder 生成器将对您的ui:withtype 类执行GWT.create(),这就是您在Java 代码中所做的:

    Message string = GWT.create(Message.class);
    String localizedGreeting = string.greeting();
    

    在隐式Messages接口(由UiBinder生成)中,ui:UiBinder上的各种ui:generateXxx属性会转化为接口上的注解(@Generate注解的属性,或者@GenerateKeys的值注释)。
    然后,将为每个ui:msg生成一个方法,其中属性生成等效的注解(@Key@Description),ui:msg元素的内容是@DefaultMessage注解的值。当您在内容中拥有或小部件时,它们将在 @DefaultMessage 文本中转换为方法和占位符的参数(值将由 UiBinder 填充)。

    我建议你先在没有 UiBinder 的情况下制作一些东西,并了解它是如何工作的;然后在 UiBinder 中尝试 ui:msg,在 DevMode 或编译器中使用 -gen,这样你就可以准确地看到 UiBinder 生成了什么代码(是的,它实际上只生成你可以自己编写的代码)。

    此外,您应该添加一个 &lt;set-property name="locale" value="en, fr" /&gt; 或者您仍然可以使用 default 语言环境,尽管 set-property-fallback(它永远不会被使用))。

    【讨论】:

    • 对不起,.properties 文件当然是正确命名的。这只是一个错字。谢谢您的回答。但不幸的是,它并没有为我和我的问题增加清晰度。不使用 UiBinder 一切正常。但我想了解如何使用 UiBinder 正确正确地国际化应用程序。
    • 您能否准确地写出我需要修复的内容或提供完整示例的链接?我将不胜感激!
    • developers.google.com/web-toolkit/doc/latest/… 如果你想使用ui:msg,你必须在同一个包中提供属性文件,名称与UiBinder接口相同,后缀为ImplGenMessages。或者,您可以使用com/google/gwt/i18n/client/LocalizedResource_&lt;locale&gt;.properties 和/但然后集中所有消息。再次使用-gen 并查看生成的代码。
    • 您可以在另一个包中提供属性,但是如果您将自定义接口设置为基本接口,例如,您必须自己编写接口(正常工作不多)。 ui:baseMessagesInterface="com.company.i18n.CoreMessages"
    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多