This page describes how to setup internationalisation in your GWT application. The example on this page builds from the page listed in the requirements section.
Requirements
Project Configuration
In order to enable internationalisation support you need to add the following “goal” and “i18nMessagesBundle” elements inside “gwt-maven-plugin”:
vi pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<goals>
...
<goal>i18n</goal>
...
</goals>
...
insert the “i18nMessagesBundle” element here:
<configuration>
...
<i18nMessagesBundles>
<i18nMessagesBundle>com.test.client.Messages</i18nMessagesBundle>
</i18nMessagesBundles>
...
<configuration>
Externalize the language strings to a property file.
The following message bundle is converted to a Java Class by Maven. In order to specify additional bundles just insert an additional “i18nMessagesBundle” tag in the pom.xml file.
Properties File
Create the directory that will hold the property file if not done so already:
mkdir -p src/main/resources/com/test/client
vi src/main/resources/com/test/client/Messages.properties
sendButton = Send
hello = Hello {0}!
Java code Change
Modify the java code to reference the property values instead of hard-coded strings.
--- matrix-blank/src/main/java/com/test/client/Matrix.java
+++ matrix/src/main/java/com/test/client/Matrix.java
@@ -10,10 +10,11 @@
public class Matrix implements EntryPoint {
+ private final Messages messages = GWT.create(Messages.class);
public void onModuleLoad() {
- Button button = new Button("Send", new ClickHandler() {
+ Button button = new Button(messages.sendButton(), new ClickHandler() {
public void onClick(ClickEvent event) {
- Window.alert("Hello World!");
+ Window.alert(messages.hello("World"));
}
});
button.setStyleName("sendButton");
Test the Change
At this time you would want to verify that the Message.java class has been generated by the GWT framework. If this class is not found then try to mvn compile and refresh the eclipse project. You may also want to right click, update Maven project configuration if that does not work.
mvn compile gwt:run
The French Version
vi src/main/resources/com/test/client/Messages_fr.properties
sendButton = Envoyer
hello = bonjour {0}!
Configure
You can add the following line into the module element of your “gwt.xml” file for each locale you want to support:
vi src/main/java/com/test/Matrix.gwt.xml
<extend-property name="locale" values="fr"/>
Start the application
mvn compile gwt:run
Test the Change
At this point you have 2 options to see the French version of the site:
- You can put the following line in the host HTML page
<meta name="gwt:property" content="locale=fr">
- Append the client property value to the query string of the URL: http://127.0.0.1:8888/Matrix.html?gwt.codesvr=127.0.0.1:9997?locale=fr
Either way you will see the French version of the site.
Reference
http://code.google.com/webtoolkit/doc/latest/tutorial/i18n.html
Appendix
Creating Keys for place holder parameters, the lines of the property file should look like this:
myString = First parm is {0}, second parm is {1}, third parm is {2}.
The key “myString” can be used in static HTML by wrapping the content with a html tag with an assigned id.
<h1 id="myString"></h1>
The id attribute can be used as a handle to replace the text in Java code like this:
RootPanel.get("appTitle").add(new Label(constants.myString("one","two","three")));
To get the list of locale’s supported by your JVM just run the following code:
public static void main(String args[]) {
Locale locales[] = SimpleDateFormat.getAvailableLocales();
List arrayList = Arrays.asList(locales);
Collections.sort(arrayList, new Comparator() {
public int compare(Locale o1, Locale o2) {
return o1.getDisplayName().compareTo(o2.getDisplayName());
}
});
for (Locale locale : arrayList) {
System.out.println(locale.toString() + "\t" + locale.getDisplayName());
}
}