Add LocaleInfo.hasAnyRTL() which returns true if the application was
built with support for any RTL locales.  This will be used in improving
Bidi support in GWT widgets.

Public review at: http://gwt-code-reviews.appspot.com/378801/show

Patch by: jat
Review by: rice, tomerigo (TBR)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7966 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/i18n/client/LocaleInfo.java b/user/src/com/google/gwt/i18n/client/LocaleInfo.java
index 1eb9ca8..dac341e 100644
--- a/user/src/com/google/gwt/i18n/client/LocaleInfo.java
+++ b/user/src/com/google/gwt/i18n/client/LocaleInfo.java
@@ -85,7 +85,14 @@
      */
     return instance.infoImpl.getLocaleNativeDisplayName(localeName);
   }
-  
+
+  /**
+   * @return true if any locale supported by this build of the app is RTL.
+   */
+  public static boolean hasAnyRTL() {
+    return instance.infoImpl.hasAnyRTL();
+  }
+
   private final LocaleInfoImpl infoImpl;
 
   private final CldrImpl cldrImpl;
diff --git a/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java b/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java
index d98858b..7142858 100644
--- a/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java
+++ b/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java
@@ -85,4 +85,11 @@
   public NumberConstants getNumberConstants() {
     return GWT.create(NumberConstantsImpl.class);
   }
+  
+  /**
+   * @return true if any locale supported by this build of the app is RTL.
+   */
+  public boolean hasAnyRTL() {
+    return false;
+  }
 }
diff --git a/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java b/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java
index 8d0adcc..fd1ba61 100644
--- a/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java
+++ b/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java
@@ -67,6 +67,20 @@
   private static final String OVERRIDE_LOCALE_NATIVE_DISPLAY_NAMES = "com/google/gwt/i18n/client/impl/cldr/LocaleNativeDisplayNames-override.properties";
 
   /**
+   * Set of canonical language codes which are RTL.
+   */
+  private static final Set<String> RTL_LOCALES = new HashSet<String>();
+
+  static {
+    // TODO(jat): get this from CLDR data.
+    RTL_LOCALES.add("ar");
+    RTL_LOCALES.add("fa");
+    RTL_LOCALES.add("he");
+    RTL_LOCALES.add("ps");
+    RTL_LOCALES.add("ur");
+  }
+
+  /**
    * Generate an implementation for the given type.
    * 
    * @param logger error logger
@@ -114,9 +128,14 @@
       writer.println("@Override");
       writer.println("public String[] getAvailableLocaleNames() {");
       writer.println("  return new String[] {");
+      boolean hasAnyRtl = false;
       for (GwtLocaleImpl possibleLocale : allLocales) {
         writer.println("    \""
             + possibleLocale.toString().replaceAll("\"", "\\\"") + "\",");
+        if (RTL_LOCALES.contains(
+            possibleLocale.getCanonicalForm().getLanguage())) {
+          hasAnyRtl = true;
+        }
       }
       writer.println("  };");
       writer.println("}");
@@ -128,6 +147,11 @@
           + "::nativeDisplayNames[localeName];");
       writer.println("}-*/;");
       writer.println();
+      writer.println("@Override");
+      writer.println("public boolean hasAnyRTL() {");
+      writer.println("  return " + hasAnyRtl + ";");
+      writer.println("}");
+      writer.println();
       writer.println("private native void ensureNativeDisplayNames() /*-{");
       writer.println("  if (this.@" + qualName
           + "::nativeDisplayNames != null) {");
diff --git a/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml b/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml
index 73cf0dc..280d4ef 100644
--- a/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml
+++ b/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml
@@ -19,5 +19,6 @@
 	<!-- Include client-side source for the test cases -->
 	<source path="client"/>
 	<extend-property name="locale" values="en_US"/>
+        <extend-property name="locale" values="ar"/>
 	<set-property name = "locale" value = "en_US"/>
 </module>
diff --git a/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java b/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java
index 1b7c9e1..52aa6db 100644
--- a/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java
+++ b/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java
@@ -27,17 +27,17 @@
     return "com.google.gwt.i18n.I18NTest";
   }
 
-  public void testCurrentLocale() {
-    String locale = LocaleInfo.getCurrentLocale().getLocaleName();
-    assertEquals("piglatin_UK_WINDOWS", locale);
-  }
-
   public void testAvailableLocales() {
     String[] locales = LocaleInfo.getAvailableLocaleNames();
     assertArrayEquals(new String[] {
         "default", "piglatin", "piglatin_UK", "piglatin_UK_WINDOWS"}, locales);
   }
 
+  public void testCurrentLocale() {
+    String locale = LocaleInfo.getCurrentLocale().getLocaleName();
+    assertEquals("piglatin_UK_WINDOWS", locale);
+  }
+
   public void testNativeDisplayNames() {
     // en isn't in the property set for this module so should return null
     String displayName = LocaleInfo.getLocaleNativeDisplayName("en");
@@ -51,6 +51,8 @@
   public void testRTL() {
     boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();
     assertFalse(isRTL);
+    boolean hasRTL = LocaleInfo.hasAnyRTL();
+    assertFalse(hasRTL);
   }
 
   private void assertArrayEquals(String[] expected, String[] actual) {
diff --git a/user/test/com/google/gwt/i18n/client/LocaleInfo_ar_Test.java b/user/test/com/google/gwt/i18n/client/LocaleInfo_ar_Test.java
index 0dfdc16..6acd948 100644
--- a/user/test/com/google/gwt/i18n/client/LocaleInfo_ar_Test.java
+++ b/user/test/com/google/gwt/i18n/client/LocaleInfo_ar_Test.java
@@ -52,5 +52,7 @@
   public void testRTL() {
     boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();
     assertTrue(isRTL);
+    boolean hasRTL = LocaleInfo.hasAnyRTL();
+    assertTrue(hasRTL);
   }
 }
diff --git a/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java b/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java
new file mode 100644
index 0000000..4dfc5d3
--- /dev/null
+++ b/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010 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.i18n.client;
+
+import com.google.gwt.junit.client.GWTTestCase;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+/**
+ * Tests the LocaleInfo class and the associated generator.
+ */
+public class LocaleInfo_en_Test extends GWTTestCase {
+
+  @Override
+  public String getModuleName() {
+    // This module is built in the en locale, but includes ar.
+    return "com.google.gwt.i18n.I18NTest_en";
+  }
+
+  public void testAvailableLocales() {
+    String[] locales = LocaleInfo.getAvailableLocaleNames();
+    ArrayList<String> localeList = new ArrayList<String>();
+    Collections.addAll(localeList, locales);
+    assertTrue(localeList.contains("ar"));
+    assertTrue(localeList.contains("default"));
+    assertTrue(localeList.contains("en_US"));
+  }
+
+  public void testRTL() {
+    boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();
+    assertFalse(isRTL);
+    boolean hasRTL = LocaleInfo.hasAnyRTL();
+    assertTrue(hasRTL);
+  }
+}