Skip to content

Commit 02dac65

Browse files
committed
Add I18n support to UIBinder and client code
1 parent d760ca3 commit 02dac65

10 files changed

+183
-14
lines changed

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,33 @@ Now, we add GWT (gwt-servlet and gwt-user artifacts) and Log4J to our project.
125125
<goals>
126126
<goal>compile</goal>
127127
<goal>test</goal>
128+
<goal>i18n</goal>
128129
</goals>
129130
</execution>
130131
</executions>
132+
<configuration>
133+
<!--
134+
use -extra parameter to generate I18N properties files.
135+
.properties files will be generated in target/extra/hellogwt/
136+
-->
137+
<extraParam>true</extraParam>
138+
<!--
139+
The resourceBundle parameter is used to declare
140+
your application bundle for i18n processing.
141+
If your application uses more than one bundle,
142+
you can nest multiple resourceBundle elements
143+
-->
144+
<resourceBundle>com.hellogwt.client.i18n</resourceBundle>
145+
<!--
146+
which properties file to translate.
147+
-->
148+
<i18nConstantsBundles>
149+
<i18nConstantsBundle>com.hellogwt.client.i18n.ClientMessages</i18nConstantsBundle>
150+
</i18nConstantsBundles>
151+
<i18nMessagesBundle>com.hellogwt.client.i18n.ClientMessages</i18nMessagesBundle>
152+
<!--
153+
-->
154+
</configuration>
131155
</plugin>
132156
<!-- $HOME/.m2/settings.xml
133157
<settings>

src/main/java/com/hellogwt/HelloGWT.gwt.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ XML module file for the com.hellogwt package.
2929

3030
<!-- only compile for Firefox to speed up GWT compile time -->
3131
<set-property name="user.agent" value="gecko1_8"/>
32+
33+
<inherits name="com.google.gwt.i18n.I18N"/>
34+
<extend-property name="locale" values="fr"/>
35+
<extend-property name="locale" values="en"/>
3236
</module>

src/main/java/com/hellogwt/client/HelloGWTWidget.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.gwt.user.client.ui.Label;
3131
import com.google.gwt.user.client.ui.SplitLayoutPanel;
3232
import com.google.gwt.user.client.ui.TextBox;
33+
import com.hellogwt.client.i18n.ClientMessages;
3334
import com.hellogwt.shared.domain.Greeting;
3435
import java.util.List;
3536

@@ -41,11 +42,13 @@ interface HelloGWTWidgetUiBinder extends UiBinder<SplitLayoutPanel, HelloGWTWidg
4142
private static HelloGWTWidgetUiBinder uiBinder = GWT.create(HelloGWTWidgetUiBinder.class);
4243

4344
private GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
45+
46+
private ClientMessages clientMsg = GWT.create(ClientMessages.class);
4447

4548
final AsyncCallback<String> textbox_callback = new AsyncCallback<String>() {
4649
@Override
4750
public void onFailure(Throwable caught) {
48-
greetingLabel.setText("ERROR!");
51+
greetingLabel.setText(clientMsg.error());
4952
}
5053

5154
@Override
@@ -57,7 +60,7 @@ public void onSuccess(String result) {
5760
private AsyncCallback<Void> btn_callback = new AsyncCallback<Void>() {
5861
@Override
5962
public void onFailure(Throwable caught) {
60-
Window.alert("ERROR: Cannot edit greetings!");
63+
Window.alert(clientMsg.editError());
6164
}
6265

6366
@Override
@@ -120,7 +123,7 @@ public void onSuccess(Greeting result) {
120123
if (result == null) {
121124
greetingService.addGreeting(authorTextBox.getText(), textTextBox.getText(), btn_callback);
122125
} else {
123-
statusMessage.setInnerText("Greeting already exists!");
126+
statusMessage.setInnerText(clientMsg.greetingAlreadyExists());
124127
}
125128
}
126129
});
@@ -160,8 +163,8 @@ public void onSuccess(List<Greeting> greetings) {
160163
private void fillGreetingsTable(List<Greeting> greetings) {
161164
greetingsFlexTable.removeAllRows();
162165

163-
greetingsFlexTable.setText(0, 0, "Author");
164-
greetingsFlexTable.setText(0, 1, "Text");
166+
greetingsFlexTable.setText(0, 0, clientMsg.author());
167+
greetingsFlexTable.setText(0, 1, clientMsg.text());
165168

166169
for (Greeting greeting : greetings) {
167170
int row = greetingsFlexTable.getRowCount();

src/main/java/com/hellogwt/client/HelloGWTWidget.ui.xml

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
<!--
22
Corresponds to HelloGWTWidget.java.
33
See http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html for doc.
4+
5+
ui:generateFormat
6+
We want a file in the java properties format to be generated.
7+
ui:generateKeys
8+
We want the property keys to be MD5 hashes of the message content,
9+
so that the translations will survive as the messages move around within
10+
the template.
11+
(And if we take some care with how we manage our translated properties
12+
files, a message that happens to be used in more than one template will
13+
only need to be translated once.)
14+
ui:generateLocales
15+
And we want the generated properties to be part of the default locale.
16+
17+
Generated files (in target/extra/hellogwt) must be copied into this directory,
18+
removing the package prefix "com.hellogwt.client.".
419
-->
520
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
6-
xmlns:g='urn:import:com.google.gwt.user.client.ui'>
21+
xmlns:g="urn:import:com.google.gwt.user.client.ui"
22+
ui:generateFormat="com.google.gwt.i18n.rebind.format.PropertiesFormat"
23+
ui:generateKeys="com.google.gwt.i18n.server.keygen.MD5KeyGenerator"
24+
ui:generateLocales="fr,en">
25+
<ui:with field="msgs" type="com.hellogwt.client.i18n.ClientMessages"/>
726
<ui:style>
827
.southPanel {
928
background-color: #777;
@@ -13,26 +32,36 @@ See http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html for doc.
1332
<g:north size="60" unit="EM">
1433
<g:HorizontalPanel>
1534
<g:TextBox ui:field="nameTextBox" width="100"/>
16-
<g:Label ui:field="greetingLabel" text="Hello, GWT!!" width="50"/>
35+
<g:Label ui:field="greetingLabel" width="50" text="{msgs.helloGwt}"/>
1736
</g:HorizontalPanel>
1837
</g:north>
1938

2039
<g:center>
2140
<g:VerticalPanel>
2241
<g:HorizontalPanel>
23-
<g:Label text="Author:" width="50"/>
42+
<g:Label width="50">
43+
<ui:msg key="authorLabel">Author:</ui:msg>
44+
</g:Label>
2445
<g:TextBox ui:field="authorTextBox" width="100"/>
2546
</g:HorizontalPanel>
2647

2748
<g:HorizontalPanel>
28-
<g:Label text="Text:" width="50"/>
49+
<g:Label width="50">
50+
<ui:msg key="textLabel">Text:</ui:msg>
51+
</g:Label>
2952
<g:TextBox ui:field="textTextBox" width="100"/>
3053
</g:HorizontalPanel>
3154

3255
<g:HorizontalPanel>
33-
<g:Button ui:field="addButton" text="Add" width="50"/>
34-
<g:Button ui:field="updateButton" text="Update" width="50"/>
35-
<g:Button ui:field="deleteButton" text="Delete" width="50"/>
56+
<g:Button ui:field="addButton" width="50">
57+
<ui:msg key="add" description="Text on button">Add</ui:msg>
58+
</g:Button>
59+
<g:Button ui:field="updateButton" width="50">
60+
<ui:msg key="update" description="Text on button">Update</ui:msg>
61+
</g:Button>
62+
<g:Button ui:field="deleteButton" width="50">
63+
<ui:msg key="delete" description="Text on button">Delete</ui:msg>
64+
</g:Button>
3665
</g:HorizontalPanel>
3766

3867
<g:FlexTable ui:field="greetingsFlexTable"/>
@@ -42,7 +71,13 @@ See http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html for doc.
4271
<g:south size="60" unit="EM">
4372
<g:FlowPanel styleName="{style.southPanel}">
4473
<g:HTML>
45-
<span ui:field="statusMessage">...</span>
74+
<span ui:field="statusMessage">
75+
<ui:msg key="defaultStatusMessage" meaning="Default status message.">Application is loaded.</ui:msg>
76+
</span>
77+
<p>
78+
<a href="?locale=fr">français</a>,
79+
<a href="?locale=en">English</a>
80+
</p>
4681
</g:HTML>
4782
</g:FlowPanel>
4883
</g:south>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated from com.hellogwt.client.HelloGWTWidgetHelloGWTWidgetUiBinderImplGenMessages
2+
# for locale en
3+
4+
# Description: Text on button
5+
add=Add
6+
7+
authorLabel=Author\:
8+
9+
# Meaning: Default status message.
10+
defaultStatusMessage=Application is loaded.
11+
12+
# Description: Text on button
13+
delete=Delete
14+
15+
textLabel=Text\:
16+
17+
# Description: Text on button
18+
update=Update
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Generated from com.hellogwt.client.HelloGWTWidgetHelloGWTWidgetUiBinderImplGenMessages
2+
# for locale fr
3+
4+
# Description: Default status message.
5+
defaultStatusMessage=L''application est charg\u00e9e.
6+
authorLabel=Auteur :
7+
add=Ajouter
8+
textLabel=Texte :
9+
update=Modifier
10+
delete=Supprimer
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2014 olivier.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hellogwt.client.i18n;
17+
18+
import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
19+
import com.google.gwt.i18n.client.LocalizableResource.Generate;
20+
import com.google.gwt.i18n.client.Messages;
21+
22+
@Generate(
23+
format = {"com.google.gwt.i18n.rebind.format.PropertiesFormat"},
24+
fileName = "ClientMessages",
25+
locales = {"fr", "en", "default"})
26+
@DefaultLocale("en")
27+
public interface ClientMessages extends Messages {
28+
29+
@DefaultMessage("Author")
30+
public String author();
31+
32+
@DefaultMessage("ERROR: Cannot edit greetings!")
33+
public String editError();
34+
35+
@DefaultMessage("Error!")
36+
public String error();
37+
38+
@DefaultMessage("Greeting already exists!")
39+
public String greetingAlreadyExists();
40+
41+
@DefaultMessage("Hello, GWT!")
42+
@Key("welcomeMessage")
43+
public String helloGwt();
44+
45+
@DefaultMessage("Text")
46+
public String text();
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated from com.hellogwt.client.i18n.ClientMessages
2+
# for locale default
3+
4+
author=Author
5+
6+
editError=ERROR\: Cannot edit greetings\!
7+
8+
error=Error\!
9+
10+
greetingAlreadyExists=Greeting already exists\!
11+
12+
text=Text
13+
14+
welcomeMessage=Hello, GWT\!
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated from com.hellogwt.client.i18n.ClientMessages
2+
# for locale fr
3+
4+
author=Auteur
5+
6+
editError=Erreur \: impossible de modifier les voeux \!
7+
8+
error=Erreur \!
9+
10+
greetingAlreadyExists=Ces voeux existent d\u00c3\u00a9j\u00c3\u00a0 \!
11+
12+
text=Texte
13+
14+
welcomeMessage=Salut, GWT \!

src/main/java/com/hellogwt/server/GreetingServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class GreetingServiceImpl implements GreetingService {
3434

3535
@Autowired
3636
private GreetingMapper greetingMapper;
37-
37+
3838
@Override
3939
public Greeting getGreeting(String text) {
4040
return greetingMapper.getGreeting(text);

0 commit comments

Comments
 (0)