HOWTO: Implementing I18N in GWT
This HOWTO provides a short overview of the requirements for implementing i18n in GWT 1.4.
MODULE.gwt.xml
In order to activate i18n, your module needs to inherit the i18n GWT module. You also need to add all the locales you are going to support to your module file, so GWT knows which files it will need to compile. Your module file should then contain at least the following code:
<module> <inherits name="com.google.gwt.user.User" /> <inherits name="com.google.gwt.i18n.I18N" /> ... <entry-point class="your.gwt.EntryPoint" /> ... <extend-property name="locale" values="en" /> <extend-property name="locale" values="fr" /> <extend-property name="locale" values="nl" /> ... </module>
the Constants interface
To be able to use i18n in your GWT-code, you create an interface that extends com.google.gwt.i18n.client.Constants. You can of course create multiple interfaces to group similar properties together. For every i18n property, you create a matching method in this interface. Here is a simple example:
import com.google.gwt.i18n.client.*;
public interface MenuConstants extends Constants {
String pages();
String navigation();
String versionManagement();
...
}
To use this interface you use the following code:
import com.google.gwt.core.client.*;
import com.google.gwt.user.client.ui.*;
public class Menu extends MenuBar {
// load an instance of the MenuConstants interface
private MenuConstants menuConstants = (MenuConstants) GWT.create(MenuConstants.class);
public Menu() {
super();
MenuBar pages = new MenuBar(true);
MenuBar navigationBar = new MenuBar(true);
MenuBar versionBar = new MenuBar(true);
...
MenuItem pagesBarItem = new MenuItem(menuConstants.pages(), pagesBar);
MenuItem navigationBarItem = new MenuItem(menuConstants.navigation(), navigationBar);
MenuItem versionBarItem = new MenuItem(menuConstants.versionManagement(), versionBar);
...
}
}
The property files
And finally you need to create your property files. For each interface that you create, you add a properties file with the same name as the interface. This properties file will be used when no locale was specified. For every locale that you want to support, you will also need to add a matching properties file with a name scheme like: interfacename_locale.properties. This is what the MenuConstants.properties file looks like:
pages=Pages navigation=Navigation versionManagement=Version Management ...
And then the MenuConstansts_nl.properties file
pages=Pagina's navigation=Navigatie versionManagement=Versiebeheer ...
Loading a different language
You can now compile your GWT-code in just the same way as you've always done before. When you want to load a different language than the default one, you only have to add a locale parameter to your request as so: http://yourhost/MODULE.html?locale=nl.