Update the I18N sample to demonstrate i18n annotations, including plural
forms.

Patch by: jat
Review by: bruce (TBR)



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2117 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/I18N.gwt.xml b/samples/i18n/src/com/google/gwt/sample/i18n/I18N.gwt.xml
index 4f0e60f..69e34a2 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/I18N.gwt.xml
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/I18N.gwt.xml
@@ -16,5 +16,5 @@
 	<inherits name="com.google.gwt.user.User" />
     <inherits name="com.google.gwt.i18n.I18N"/>
 	<entry-point class="com.google.gwt.sample.i18n.client.I18N" />
-	<extend-property name="locale" values="fr" />
+	<extend-property name="locale" values="en,fr" />
 </module>
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/AbstractFormatExampleConstants.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/AbstractFormatExampleConstants.java
index 15810ae..d3941aa 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/AbstractFormatExampleConstants.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/AbstractFormatExampleConstants.java
@@ -22,13 +22,18 @@
  */
 public interface AbstractFormatExampleConstants extends Constants {
 
+  @DefaultStringValue("Unable to parse input")
   String failedToParseInput();
 
+  @DefaultStringValue("Formatted value")
   String formattedOutput();
 
+  @DefaultStringValue("Value to format")
   String inputValue();
 
+  @DefaultStringValue("Invalid pattern")
   String invalidPattern();
 
+  @DefaultStringValue("Pattern")
   String pattern();
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/ColorConstants.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/ColorConstants.java
index 1c24911..de41c5c 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/ColorConstants.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/ColorConstants.java
@@ -24,20 +24,27 @@
  */
 public interface ColorConstants extends ConstantsWithLookup {
 
+  @DefaultStringValue("Black")
   String black();
 
+  @DefaultStringValue("Blue")
   String blue();
 
+  @DefaultStringValue("Green")
   String green();
 
+  @DefaultStringValue("Grey")
   String grey();
 
+  @DefaultStringValue("Light Grey")
   String lightGrey();
 
+  @DefaultStringValue("Red")
   String red();
 
+  @DefaultStringValue("White")
   String white();
 
+  @DefaultStringValue("Yellow")
   String yellow();
-
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/ConstantsExampleConstants.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/ConstantsExampleConstants.java
index 7bfc0f0..005cf2f 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/ConstantsExampleConstants.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/ConstantsExampleConstants.java
@@ -23,11 +23,17 @@
  * Internationalized constants used to demonstrate {@link Constants}.
  */
 public interface ConstantsExampleConstants extends Constants {
+
+  @DefaultStringMapValue({"black", "Black", "blue", "Blue", "green", "Green", "grey", "Grey",
+      "lightGrey", "Light Grey", "red", "Red", "white", "White", "yellow", "Yellow"})
   Map<String, String> colorMap();
 
+  @DefaultStringValue("Favorite color")
   String favoriteColor();
 
+  @DefaultStringValue("First Name")
   String firstName();
 
+  @DefaultStringValue("Last Name")
   String lastName();
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/DateTimeFormatExampleConstants.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/DateTimeFormatExampleConstants.java
index 34567df..93fb108 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/DateTimeFormatExampleConstants.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/DateTimeFormatExampleConstants.java
@@ -22,5 +22,10 @@
  */
 public interface DateTimeFormatExampleConstants extends
     AbstractFormatExampleConstants {
+  @DefaultStringMapValue({"fullDateTime", "Full Date/Time", "longDateTime", "Long Date/Time",
+      "mediumDateTime", "Medium Date/Time", "shortDateTime", "Short Date/Time", "fullDate",
+      "Full Date", "longDate", "Long Date", "mediumDate", "Medium Date", "shortDate", "Short Date",
+      "fullTime", "Full Time", "longTime", "Long Time", "mediumTime", "Medium Time", "shortTime",
+      "Short Time", "custom", "Custom"})
   Map<String, String> dateTimeFormatPatterns();
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.java
index 645727f..f9accb0 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.java
@@ -21,6 +21,7 @@
  * Internationalized messages used by {@link MessagesExampleController}.
  */
 public interface ErrorMessages extends Messages {
+  @DefaultMessage("User ''{0}'' has security clearance ''{1}'' and cannot access ''{2}''")
   String permissionDenied(String username, String securityClearance,
       String inaccessibleResource);
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.properties b/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.properties
index 2234a53..6515cd3 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.properties
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/ErrorMessages.properties
@@ -14,4 +14,7 @@
 # the License.
 # 
 
+# This file is not actually used since the annotations are present.  This file
+# is left as an example.
+
 permissionDenied = User ''{0}'' has security clearance ''{1}'' and cannot access ''{2}''
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/I18N.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/I18N.java
index be37b28..bffcf58 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/I18N.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/I18N.java
@@ -166,6 +166,10 @@
     bindElement("messagesArg3Text", controller.txtArg3);
     bindElement("messagesFormattedOutputCaption", constants.formattedMessage());
     bindElement("messagesFormattedOutputText", controller.lblFormattedMessage);
+    bindElement("pluralsArg1Caption", constants.arg1());
+    bindElement("pluralsArg1Text", controller.pluralCount);
+    bindElement("pluralsFormattedOutputCaption", constants.formattedMessage());
+    bindElement("pluralsFormattedOutputText", controller.lblPluralMessage);
   }
 
   private void initNumberFormatExample(NumberFormatExampleController controller) {
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleConstants.properties b/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleConstants.properties
index fa35b22..1ace2a1 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleConstants.properties
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleConstants.properties
@@ -14,6 +14,9 @@
 # the License.
 # 
 
+# This file is not actually used since the annotations are present.  This file
+# is left as an example.
+
 arg1 = Argument {0}
 
 arg2 = Argument {1}
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleController.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleController.java
index fc56ad9..c984419 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleController.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/MessagesExampleController.java
@@ -26,6 +26,7 @@
  */
 public class MessagesExampleController {
   private static final ErrorMessages ERRORS = GWT.create(ErrorMessages.class);
+  private static final PluralMessages PLURALS = GWT.create(PluralMessages.class);
 
   public final TextBox txtArg1 = new TextBox();
   public final TextBox txtArg2 = new TextBox();
@@ -33,9 +34,13 @@
   public final Label lblFormattedMessage = new Label();
   public final Label lblMessageTemplate = new Label();
 
+  public final TextBox pluralCount = new TextBox();
+  public final Label lblPluralMessage = new Label();
+
   private String prevArg1;
   private String prevArg2;
   private String prevArg3;
+  private String prevCount;
   private final MessagesExampleConstants constants;
 
   public MessagesExampleController(MessagesExampleConstants constants) {
@@ -53,11 +58,14 @@
     txtArg1.addKeyboardListener(listener);
     txtArg2.addKeyboardListener(listener);
     txtArg3.addKeyboardListener(listener);
+    pluralCount.addKeyboardListener(listener);
 
     txtArg1.setText("amelie");
     txtArg2.setText("guest");
     txtArg3.setText("/secure/blueprints.xml");
 
+    pluralCount.setText("13");
+
     maybeRefreshFormattedMessage();
   }
 
@@ -70,20 +78,30 @@
     String arg2 = txtArg2.getText().trim();
     String arg3 = txtArg3.getText().trim();
 
-    if (arg1.equals(prevArg1)) {
-      if (arg2.equals(prevArg2)) {
-        if (arg3.equals(prevArg3)) {
-          // Nothing has changed.
-          return;
-        }
-      }
+    // Check if the permission denied message should be regenerated.
+    if (!arg1.equals(prevArg1)
+      || !arg2.equals(prevArg2)
+      || !arg3.equals(prevArg3)) {
+      prevArg1 = arg1;
+      prevArg2 = arg2;
+      prevArg3 = arg3;
+
+      String formattedMessage = ERRORS.permissionDenied(arg1, arg2, arg3);
+      lblFormattedMessage.setText(formattedMessage);
     }
 
-    prevArg1 = arg1;
-    prevArg2 = arg2;
-    prevArg3 = arg3;
+    String count = pluralCount.getText().trim();
 
-    String formattedMessage = ERRORS.permissionDenied(arg1, arg2, arg3);
-    lblFormattedMessage.setText(formattedMessage);
+    // Check if the plurals message should be regenerated.
+    if (!count.equals(prevCount) && count.trim().length() > 0) {
+      prevCount = count;
+
+      try {
+        String formattedMessage = PLURALS.treeCount(Integer.valueOf(count));
+        lblPluralMessage.setText(formattedMessage);
+      } catch (NumberFormatException e) {
+        // Ignore bogus numbers
+      }
+    }
   }
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.java
index fd00ec9..82f5ed8 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.java
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.java
@@ -23,6 +23,7 @@
 public interface NumberFormatExampleConstants extends
     AbstractFormatExampleConstants {
 
+  @DefaultStringMapValue({"decimal", "Decimal", "currency", "Currency", "scientific", "Scientific",
+      "percent", "Percent", "custom", "Custom" })
   Map<String, String> numberFormatPatterns();
-
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.properties b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.properties
index 5afdfde..7452306 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.properties
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants.properties
@@ -14,6 +14,9 @@
 # the License.
 # 
 
+# This file is not actually used since the annotations are present.  This file
+# is left as an example.
+
 decimal = Decimal
 currency = Currency
 scientific = Scientific
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants_fr.properties b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants_fr.properties
index 377d5ce..368850a 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants_fr.properties
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/NumberFormatExampleConstants_fr.properties
@@ -20,4 +20,6 @@
 percent = Pourcentage
 custom = Personnalisé
 
-numberFormatPatterns = decimal, currency, scientific, percent, custom   
+# By default, we will use the key set from our parent.  If we wanted to define new keys,
+# we could do so with a line like: 
+#numberFormatPatterns = decimal, currency, scientific, percent, custom   
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages.java b/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages.java
new file mode 100644
index 0000000..686ce09
--- /dev/null
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2007 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.sample.i18n.client;
+
+import com.google.gwt.i18n.client.Messages;
+import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
+
+/**
+ * Internationalized messages used by {@link MessagesExampleController}.
+ * Used to demonstrate plural forms support.
+ */
+@DefaultLocale("en_US")
+public interface PluralMessages extends Messages {
+  @DefaultMessage("You have {0} trees.")
+  @PluralText({"one", "You have one tree."})
+  String treeCount(@PluralCount int count);
+}
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages_fr.properties b/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages_fr.properties
new file mode 100644
index 0000000..494f63d
--- /dev/null
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/client/PluralMessages_fr.properties
@@ -0,0 +1,24 @@
+#
+# Copyright 2007 Google Inc.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+# 
+
+# The default message to use for the message.
+treeCount=Vous avez {0} arbres.
+
+# The message to use for the plural form "one", which in French is applied to counts of 0 or 1.
+treeCount[one]=Vous avez {0} arbre.
+
+
+ 
\ No newline at end of file
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.css b/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.css
index 38cfe1b..d8d8033 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.css
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.css
@@ -39,6 +39,10 @@
   border-bottom: 1px solid #CCCCCC;
 }
 
+h3 {
+  color: #AAAAAA;
+}
+
 a {
   color: #FFFFFF;
 }
diff --git a/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.html b/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.html
index 37b91e2..452fe60 100644
--- a/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.html
+++ b/samples/i18n/src/com/google/gwt/sample/i18n/public/I18N.html
@@ -33,6 +33,7 @@
 <!-- The following commented-out <meta> element is an alternate way to specify the locale to use -->
 <!-- if the locale is not present in the URL query string.-->
 <!-- <meta name='gwt:property' content='locale=fr'>-->
+<meta name="gwt:property" content="locale=en">
 
 <link rel="stylesheet" type="text/css" href="I18N.css">
 
@@ -172,11 +173,39 @@
         </tr>
         
         <tr>
-          <th class="col1" id="messagesFormattedOutputCaption"></th >
+          <th class="col1" id="messagesFormattedOutputCaption"></th>
           <td class="col2" id="messagesFormattedOutputText"></td>
         </tr>
       </table>
     </div>
+
+    <div class="plurals example">
+      <h3>Plural Forms</h3>
+      <p>
+        The <code>Messages</code> interface also provides support for plural forms, choosing the
+        correct form based on the count.  In the example below, note that the English plural forms
+        are included in the source file via annotations.
+      </p>
+      <pre class="code">
+@DefaultLocale("en_US")
+public interface MyMessages extends Messages {
+  @DefaultMessage("You have {0} trees.")
+  @PluralText({"one", "You have one tree."})
+  String treeCount(@PluralCount int count);
+}
+      </pre>
+      <table>
+        <tr>
+          <th class="col1" id="pluralsArg1Caption"></th>
+          <td class="col2" id="pluralsArg1Text"></td>
+        </tr>
+        
+        <tr>
+          <th class="col1" id="pluralsFormattedOutputCaption"></th>
+          <td class="col2" id="pluralsFormattedOutputText"></td>
+        </tr>
+      </table>
+    </div>
     
     <div class="constants example">
       <h2>Constants</h2>