2010-02-18 38 views
21

我想國際化與屬性文件的UIBinder應用程序。由於我們已經有很多翻譯被com.google.gwt.i18n.client.Messages接口(GWT 1.7.1)公開,我們希望重用這些消息。如何使消息國際化GWT UIBinder頁面?

我曾嘗試以下:

<!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(); 
} 

但是,這是行不通的。我怎樣才能做到這一點?

回答

27

更新:

由於伊戈爾寫了他的答案,一直在UI粘結劑使用信息的新方法。

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

此方法使用GWT的text resource UiBinder integration

+1

如果您想改善回答一個鏈接到文檔,我相信你會得到應得的讚揚:)(我想編輯和添加鏈接自己,但在快速瀏覽文檔後,我似乎無法找到你的方法指的是)。 –

+0

感謝您的反饋Igor。我不確定我在哪裏找到這個技巧,但它絕對不在文檔中。 你知道任何人誰在谷歌工作誰可以錯誤得到這個添加到GWT文檔? – logan

+1

希望我知道;)但是您可以通過[GWT討論列表](http://groups.google.com/group/google-web-toolkit)與GWT開發者聯繫,或者在他們的[bugtracker]中打開一張新票( http://code.google.com/p/google-web-toolkit/issues/list)。 –

8

UiBinder結合國際化存在一個已知問題,請參閱gwt錯誤跟蹤器上最近的這個線程: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 
+0

感謝,對這個問題的意見很好的信息 - 用UiBinder的整體國際化似乎仍然在邊給我粗略的,我希望GWT隊會使其不太「哈克」。 –

+0

您可能還想看看使用GWTP提議的python腳本,以將您的本地化字符串集中在同一個文件中:http://code.google.com/p/gwt-platform/wiki/MergeLocale –

13

更新:
請檢查下面的logan's answer,以獲取最新版本的GWT中可用的解決方案。


我有(其實,有)同樣的問題,因爲你做的 - 遷移到GWT 2.0後,我有一個屬性文件,我想在我的UiBinder的文件中使用。不幸的是,我無法像我想的那樣工作 - 看起來GWT開發人員希望人們使用i18n guide for UiBinder中描述的語法 - 在編譯期間爲每個UiBinder模板創建消息接口等。

無論如何,你仍然可以使用外部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的標題等,申報單的內容:/你可以填補這些在課堂背後UiBinder模板,但應該有更好的方法。我希望你可以通過innerHTML設置內部HTML(因爲UiBinder應該識別該方法,並允許通過XML/UiBinder模板設置它的值),但唉,上次我檢查它沒有工作:/