views:

263

answers:

2

I'm using GWT internationalization Messages. The documentation for Plural Forms says this should work:

@DefaultMessage("{0} {1,number} hours {2}")
@PluralText({"one", "an hour"})
String hours(String prefix, @PluralCount int count, String suffix);

Well, it doesn't. Whatever value of count it still delivers DefaultMessage (e.g. "1 hours ago"). Same if I use a .properties file:

hours[one]=an hour
hours[few]=some hours
hours={0} {1,number} hours {2}

Is there a bug in the docs or in GWT (I'm using GWT 2.0.3) or in me? If any of the two former, anyone knows of a workaround?

EDIT: More clues to this mystery. I can get plural handling to work if I don't rely on default locale handling. That is, I need:

  • In my module's gwt.xml file:

    <extend-property name="locale" values="en"/>

  • In my Messages extentsion:

    @DefaultLocale("en")

    public interface MyMessages extends Messages { ...

  • Explicitly load the "en" locale by appending to the URL

    &locale=en

See http://groups.google.com/group/google-web-toolkit/browse_thread/thread/80ae300213cc6adb where I have cross posted this question.

EDIT 2: The reason I entered this GWT plurals land is that I'm creating a "GWT HUman Readable Relative Timestamps" module. Open sourced at GitHub: http://github.com/PEZ/GWT-Relative-Time Please check it out. It'll very soon have correct singular forms and support for some languages. =)

+1  A: 

Getting used to answering my own questions =) Here's cross post of my "answer" on the GWT Google group:

There seems to be a bug with the default locale handling. Here's how I have reached that conclusion:

I wanted to add some locales to my module. Figured I could get a boiler plate for the properties file if I used the @Generate annotation. I noticed that it created both an _en.properties file and a _default.properties. What's more; the _en file completely lacked the plural form info! The _default file had them though.

I then moved the _defaults file to the same directory as my TimeMessages.java file and renamed it TimeMessages_default.properties.

With this in place I can remove <extend-property name="locale" values="en"/> from my module's .gwt.xml file and, more important, the &locale=en from the URL when running my app. I still need the @DefaultLocale("en") annotation though, even though the documentation clearly states that this is not necessary.

In conclusion, if you run into this problem, try:

  • generating properties files using @Generate
  • place YourMessages_default.properties side by side with YourMessages.java
  • prepend the YourMessages interface with a @DefaultLocale("en") annotation.

About that @Generate. This is what worked for me. Just before my extension of the Message interface:

@Generate(format = {"com.google.gwt.i18n.rebind.format.PropertiesFormat"})

The GWT log said it created my properties file, but I couldn't find it. I fixed that by adding the compiler flag -extra extras and then found the properties files generated in the extras directory. Including this info here since I spent more than an hour figuring it out.

PEZ
+1  A: 

One more thing I found is that you can define a fallback locale like this: <set-property-fallback name="locale" value="en"/> in your module XML file

Guillaume Polet