Oct
10
Submitted by braam on 10 October, 2011 - 17:38
Another hurdle taken; currencies.
Say you want to display a product priced at 25 dollars an 45 cents, you should be able to use the GWT NumberFormat to get $ 25.45 or $ 25,45 using the Dutch locale for instance.
It turns out though, that there is no way to specify the currency symbol, so your always supposed to use the "default" currency for your locale. This is quite strange because I can imagine that there are countries using more than one currency?
My solution is has been to use the Messages system to define a format, such as:
@DefaultMessage("{0} {1,number,###,###.00}")where 0 is the symbol and 1 is the amount. This allows a different format for each language, so it's quite flexible.
Next is the currency symbol itself. It can't be determined on the server, and sending it from the client just seems a little to much trouble.
Thankfully GWT does provide a way to access the list of symbols that it houses, you just have to know where to find it (I found it here).
private static CurrencyCodeMapConstants lookup = GWT.create(CurrencyCodeMapConstants.class);
Map currencyMap = lookup.getCurrencyMap();
String symbol = currencyMap.get(currencyCode);That's it, combining the formatting with this lookup solves my use case. Google does not document these things that well, but I'm glad we have the sources so we can figure it out anyway.