GWT ramblings of a Flex developer - localization / I18N

Hello again,


This time I want to talk about localization (a.k.a. i18n).

As Flex developers, as I assume you already know, all you have to do is create a *.properties file (say and in it define keys with localized values, like:



Then, in the main application mxml, you would define the resource bundles:



And basically you're done. All you need to do to use it, is use the ResourceManager class to access the localized value of the key.

Simple stuff, right?


Not so with GWT...

Oh, you still need to define the *.properties file pretty much the same way, but using it is a hassle.


First, you need to make sure the GWT maven plugin knows about the bundle, you do this by adding the following to the GWT plugin pom definition, under the configuration node:



Second, you need to compile it. Yes, compile it.

You heard me.

Every modification you make, even if it's just a modified value and not a single change to the keys, you need to compile.

That is because when you compile using the GWT maven plugin with the i18n goal, a Java inteface is generated for each properties file, and it looks something like (for the example above):

   * Translated "Name".
   * @return translated "Name"
  String columnHeaderName();



Now, you want to use it right? Even more Yuckiness.

You need to have GWT create an instance of it everywhere you want to use it, by calling:

public static Messages MESSAGES = GWT.create(Messages.class);


To facilitate it better, and also for CSS bundles (we'll discuss this in the next post, just ignore that code for now) I have created a Services class that centralizes it:

public class Services {
	public static Messages MESSAGES = GWT.create(Messages.class);
	public static MyBundle MY_BUNDLE = GWT.create(MyBundle.class);
	public static MyCss MY_CSS = MY_BUNDLE.css();



Then, when you need to use it, you'd call:



When using UiBinder, you'd have to define a <ui:with> tag:

<ui:with type="com.samples.resources.Messages" field="constants"/>


And then use it, for example:

<g:InlineLabel text="{constants.enterPasswordHere}"/>


For me, the whole thing is unnecessarily combersome.

When you take into consideration that you need to compile on every change, this makes the development process combersome even more.


Stay tuned, next post will discuss CSS handling...