Does @DefaultMessage and @PluralText really work?

105 views
Skip to first unread message

PEZ

unread,
Jun 9, 2010, 4:38:41 PM6/9/10
to Google Web Toolkit
I'm making (porting really) a "relative time" module for human
readable date-and-time messages to GWT. The first (alpha-ish) version
is up on Github: http://github.com/PEZ/GWT-Relative-Time

Anyone who tries it will notice that it reports all times as plurals.
When it should say "an hour ago", it says "1 hours ago". My messages
interface looks like so:

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

From what I can understand of the documentation, that is how you
should be able to deal with singular and plural forms, right? Well, it
doesn't work. Anyone who knows how this works? Or, if it's a bug in
GWT, can I work myself around it?

PEZ

unread,
Jun 10, 2010, 8:54:45 AM6/10/10
to Google Web Toolkit
I'm using GWT 2.0.3. I have now also tried using a .properties file
instead. Like so:

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

With the same, boring, results.

/PEZ

PEZ

unread,
Jun 11, 2010, 5:05:34 AM6/11/10
to Google Web Toolkit
Finally got some traction thanks to the clues I got from Federico
Kerek, author of Essential GWT - http://my.safaribooksonline.com/9780321705631

It seems like it is something I don't understand with the default
locale mechanism. I haven't figured it out, but it seems that to get
plural handling to work 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

Does this provide enough clues for anyone to see where I might be
doing this wrong?

Regards,
/PEZ

PEZ

unread,
Jun 11, 2010, 8:44:15 AM6/11/10
to Google Web Toolkit
I'm back with more clues. =)

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

Seriously, this part of the GWT docs is not up to the standards of
what's common with GWT. Is there anyway I can help with fixing that?

Regards,
/PEZ

On Jun 11, 11:05 am, PEZ <p...@pezius.com> wrote:
> Finally got some traction thanks to the clues I got from Federico
> Kerek, author of Essential GWT -http://my.safaribooksonline.com/9780321705631

Thomas Broyer

unread,
Jun 11, 2010, 9:05:54 AM6/11/10
to Google Web Toolkit


On 11 juin, 11:05, PEZ <p...@pezius.com> wrote:
> Finally got some traction thanks to the clues I got from Federico
> Kerek, author of Essential GWT -http://my.safaribooksonline.com/9780321705631
>
> It seems like it is something I don't understand with the default
> locale mechanism. I haven't figured it out, but it seems that to get
> plural handling to work 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
>
> Does this provide enough clues for anyone to see where I might be
> doing this wrong?

Have you tried adding <set-property-fallback name="locale" value="en" /
>? it should make the "default" locale (i.e. when no locale is
"explicitly loaded") map to the "en" locale, so you don't need the
&locale=en thing (or <meta name='gwt:property' content='locale=en'>)
You'd still have to <extend-property> and @DefaultLocale though.

AFAICT, @DefaultLocale tells GWT what locale the @DefaultMessage (or
similar annotations for Constants) are written into. If you don't use
it, it'll consider your Messages interface to be in the "default"
locale, which has no associated "plural rule" (the DefaultRule for the
default locale only has an "other" rule).

If it works the way I think it does, then you don't need the
YourMessages_default.properties file.

PEZ

unread,
Jun 11, 2010, 3:15:49 PM6/11/10
to Google Web Toolkit
Indeed, it seems to work as you think it does. I just tried removing
the _default.properties file and thing still work. Was that
@DefaultLocal("en") all that was lacking to begin with? I'm not even
using the set-property-fallback.

Thanks for sharing! I was beginning to dispare.

/PEZ
Reply all
Reply to author
Forward
0 new messages