简体   繁体   中英

GWT UiBinder I18n

As the title says I need some help with GWT's i18n in use with UiBinder. I want to internationalize my application using static i18n. The book I use for learning only presents a way of internationalizing ui.xml files by having the compiler generate keys for Constants/Messages and a default file, but there has to be an easier way to do this. That's why I tried using the ui:with tag like this to use my internationalized constants (inside the 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>

This doesn't work, the button shows the text {lang.observe} which also seems logical, but now my question is: Is there any way to use constants like this? And if not could someone explain how I should use constants in UiBinder files instead (without having the compiler generate files and keys)?

Anywhere HTML is accepted (such as within upFace ), you can use <ui:msg> , <ui:text> and <ui:safehtml> (and anywhere plain text is expected, you can use <ui:msg> and <ui:text> ).

So in your case:

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

See http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Hello_Text_Resources and http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html#Hello_Html_Resources about ui:text and ui:safehtml .

You can use constants like this :

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

and the AppResources interface :

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

But for i18n you should really follow what the GWT manual says, ie there is no other (clean) way than prepare all the property files (one for each language) and generate all the required permutations. This primarily delegates to GWT all the language detection-related stuff, and the solution provided by GWT performs quite well at runtime. The only drawback is that the compile time is a little higher (since you'll have permutations for every browser in every language you specify).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM