Fix handling of edge cases with i18n property files.

Patch by: jat
Review by: ecc



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1525 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/i18n/rebind/util/MessagesInterfaceCreator.java b/user/src/com/google/gwt/i18n/rebind/util/MessagesInterfaceCreator.java
index 03a9299..bef8b5d 100644
--- a/user/src/com/google/gwt/i18n/rebind/util/MessagesInterfaceCreator.java
+++ b/user/src/com/google/gwt/i18n/rebind/util/MessagesInterfaceCreator.java
@@ -37,11 +37,25 @@
    * @throws ParseException
    */
   public static int numberOfMessageArgs(String template) throws ParseException {
-    // As parse, unlike format, cannot deal with single quotes, we can escape
-    // them instead. If sub-formats are supported in the future, this code will
-    // have to change.
-    String escapedDefault = template.replaceAll("'", "x");
-    int numArgs = new MessageFormat(escapedDefault).parse(escapedDefault).length;
+    /*
+     * As parse, unlike format, cannot deal with single quotes, we have to remove
+     * them. First, we remove doubled quotes (which go into the output as single
+     * quotes.  Then, we remove any quoted strings.
+     *
+     * If sub-formats are supported in the future, this code will
+     * have to change.
+     */
+    // strip doubled single-quotes
+    template = template.replace("''", "");
+    
+    // delete quoted sections
+    template = template.replaceAll("'[^']+'", "");
+    
+    if (template.length() == 0) {
+      // special case empty strings since MessageFormat.parse chokes on them.
+      return 0;
+    }
+    int numArgs = new MessageFormat(template).parse(template).length;
     return numArgs;
   }
 
diff --git a/user/test/com/google/gwt/i18n/client/I18NTest.java b/user/test/com/google/gwt/i18n/client/I18NTest.java
index bf20925..1dbf88d 100644
--- a/user/test/com/google/gwt/i18n/client/I18NTest.java
+++ b/user/test/com/google/gwt/i18n/client/I18NTest.java
@@ -464,6 +464,8 @@
         s.testLotsOfUsageOfArgs("a", "b"));
     assertEquals("\"~\" ~~ \"~~~~ \"\"", s.testWithXs());
     assertEquals("お好你好好", s.unicode("好", "好"));
+    assertEquals("", s.empty());
+    assertEquals("{quoted}", s.quotedBraces());
   }
 
   public void testTypedMessages() {
diff --git a/user/test/com/google/gwt/i18n/client/gen/TestMessages.java b/user/test/com/google/gwt/i18n/client/gen/TestMessages.java
index 204b45b..136c8cd 100644
--- a/user/test/com/google/gwt/i18n/client/gen/TestMessages.java
+++ b/user/test/com/google/gwt/i18n/client/gen/TestMessages.java
@@ -103,4 +103,20 @@
    * @gwt.key args1
    */
   String args1(String arg0);
+
+  /**
+   * Translated "{quoted}".
+   * 
+   * @return translated "{quoted}"
+   * @gwt.key quotedBraces
+   */
+  String quotedBraces();
+
+  /**
+   * Translated "".
+   * 
+   * @return translated ""
+   * @gwt.key empty
+   */
+  String empty();
 }
diff --git a/user/test/com/google/gwt/i18n/client/gen/TestMessages.properties b/user/test/com/google/gwt/i18n/client/gen/TestMessages.properties
index ca574bb..cc0dfe2 100644
--- a/user/test/com/google/gwt/i18n/client/gen/TestMessages.properties
+++ b/user/test/com/google/gwt/i18n/client/gen/TestMessages.properties
@@ -8,4 +8,5 @@
 unicode= お{0}你{1}好
 argsTest = arg0arg1 arg0,arg1 {0}arg4
 simpleMessageTest={0}
-
+quotedBraces='{'quoted'}'
+empty=