简体   繁体   中英

GWT: Using i18n in UiBinder and dynamically

I want to ask a question related to i18n in GWT.

I have a web application which uses UIBinding, I followed online instructions and successfully made the UIBinder i18n. The property generated by the GWT compiler is put into a directory of:

src/main/java/com/google/gwt/i18n/client/LocalizableResource_en.properties
src/main/java/com/google/gwt/i18n/client/LocalizableResource_de.properties
src/main/java/com/google/gwt/i18n/client/LocalizableResource_es.properties

I can therefore in the web app switch language by specifying "&locale=de" in the query string of URL.

Here is the problem. I have some other string that cannot be fit into UiBinder. They are decided at runtime and therefore needs to be read dynamically. For example, given a file, I need to workout its file type and display the file type in the proper language and display something like:

mytextfile.txt -> "TEXT"

The word "TEXT" needs to be change depends on language. Because the I get the file from server and could be any file, I cannot statically bind the "TEXT" into any translation key.

Question 1: How can I do this in UiBinder?

I understand I can do this without using UIBinder. I can use a class that extends Constants and create getter method to read i18n property files. However, the question is that in the property files above, the string key is a MD5 string. This is generated by GWT compiler. For those properties that are not in the UiBinder, there is no equivalent MD5 with it. How can I place them in the property file?

Will my property file look something like this:

952D2C56D0485958336747BCDD98590D=Hello
Apology=Sorry

the first property is used in UIBinder and the second one is used dynamically.

QUESTION 2: Is there any chance I can avoid this hybrid way of mixing properties? Either uses separate property files or something else.

QUESTION 3 : Another question is that: The property file is currently named as above. This is because some online material says GWT will automatically pick the property file in:

com/google/gwt/i18n/client/

and the filename

LocalizableResource_xxxx.properties

Is it possible for me to put this property file in the Maven's standard resource directory and tells GWT to find there?

Many thanks.

For the question 1:

I don't fully understand the question. If you know the different file types and their translations, you could put them in your localized .properties isn't it ?

For instance you would have

txt=TEXTE

in

LocalizableResource_fr.properties

Then when you change the locale you just have to use

Constants myConstants = GWT.create(MyConstants.class);    
String fileTypeProperlyTranslated = myConstants.txt();

You could also check "dictionary" , and ConstantWithLookup as suggested in the comments.

For the question 3:

You have to add a package to your gwt.xml. Assuming you put your .properties in the com.company.myProject.client package then you should have this in you gwt.xml

<entry-point class="com.company.myProject.client.MyEntryPoint" />
<source path="client" />

Then put your .properties in Maven-compliant src/main/resources/com/company/myProject/client/

You can configure the GWT Maven Plugin... It looks like this in your POM.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>gwt-maven-plugin</artifactId>
  <version>${gwt-maven-plugin.version}</version>
    <executions>
      <execution>
        <goals>
        <goal>i18n</goal>
        <goal>...</goal>
            ...
        </goals>
      </execution>
    </executions>

Assuming your properties files are name Messages_en.properties, Messages_es.properties, etc., in the "configuration" part add the following options. (For more options see gwt-maven-plugin doc)

   <configuration>
    <module>${gwt.module}</module>
    <hostedWebapp>${webappDirectory}</hostedWebapp>
    <i18nMessagesBundle>com.company.myProject.client.Messages</i18nMessagesBundle>
    <i18nConstantsWithLookupBundle>com.company.myProject.client.DynamicMessages</i18nConstantsWithLookupBundle>
   </configuration>
</plugin>

I added the i18nConstantsWithLookupBundle, assuming you had some DynamicMessages_en.properties, DynamicMessages_es.properties if ConstantsWithLookup satisfy your needs for question 1.

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