Revision: 9497
Author: [email protected]
Date: Wed Jan  5 11:56:03 2011
Log: Expose the locale.queryparam and locale.cookie config params to client
code.  This allows things like a locale selector widget that is
automatically configured based on the module configuration.

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

Review by: conroy, rjrjr, jlabanca

http://code.google.com/p/google-web-toolkit/source/detail?r=9497

Modified:
 /trunk/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml
/trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/ShowcaseShell.java
 /trunk/user/src/com/google/gwt/i18n/client/LocaleInfo.java
 /trunk/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java
 /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoContext.java
 /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java
 /trunk/user/src/com/google/gwt/i18n/rebind/LocaleUtils.java
 /trunk/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml
 /trunk/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java
 /trunk/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java

=======================================
--- /trunk/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml Mon Sep 13 12:31:36 2010 +++ /trunk/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml Wed Jan 5 11:56:03 2011
@@ -4,6 +4,7 @@
   <inherits name='com.google.gwt.user.User'/>
   <inherits name="com.google.gwt.i18n.I18N"/>
   <inherits name="com.google.gwt.i18n.CldrLocales"/>
+  <inherits name="com.google.gwt.i18n.LocaleConfigLinker"/>
   <inherits name="com.google.gwt.user.theme.standard.StandardResources"/>

   <!-- Enable debug ID. -->
@@ -20,8 +21,11 @@

   <!-- Internationalization support. -->
   <extend-property name="locale" values="en"/>
+  <!--
   <extend-property name="locale" values="ar"/>
   <extend-property name="locale" values="fr"/>
   <extend-property name="locale" values="zh"/>
+   -->
   <set-property-fallback name="locale" value="en"/>
+ <set-configuration-property name="locale.cookie" value="SHOWCASE_LOCALE"/>
 </module>
=======================================
--- /trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/ShowcaseShell.java Mon Oct 4 09:55:53 2010 +++ /trunk/samples/showcase/src/com/google/gwt/sample/showcase/client/ShowcaseShell.java Wed Jan 5 11:56:03 2011
@@ -18,6 +18,7 @@
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.dom.client.TableCellElement;
 import com.google.gwt.dom.client.TableElement;
+import com.google.gwt.dom.client.Style.Display;
 import com.google.gwt.event.dom.client.ChangeEvent;
 import com.google.gwt.event.dom.client.ChangeHandler;
 import com.google.gwt.event.dom.client.ClickEvent;
@@ -26,12 +27,13 @@
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.event.shared.HandlerRegistration;
 import com.google.gwt.http.client.UrlBuilder;
-import com.google.gwt.i18n.client.HasDirection.Direction;
 import com.google.gwt.i18n.client.LocaleInfo;
+import com.google.gwt.i18n.client.HasDirection.Direction;
 import com.google.gwt.uibinder.client.UiBinder;
 import com.google.gwt.uibinder.client.UiField;
 import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
+import com.google.gwt.user.client.Cookies;
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.Window.Location;
 import com.google.gwt.user.client.ui.AbstractImagePrototype;
@@ -43,6 +45,7 @@
 import com.google.gwt.user.client.ui.Widget;
 import com.google.gwt.view.client.TreeViewModel;

+import java.util.Date;
 import java.util.List;

 /**
@@ -305,6 +308,13 @@
    * Initialize the {...@link ListBox} used for locale selection.
    */
   private void initializeLocaleBox() {
+    final String cookieName = LocaleInfo.getLocaleCookieName();
+    final String queryParam = LocaleInfo.getLocaleQueryParam();
+    if (cookieName == null && queryParam == null) {
+ // if there is no way for us to affect the locale, don't show the selector
+      localeSelectionCell.getStyle().setDisplay(Display.NONE);
+      return;
+    }
     String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
     if (currentLocale.equals("default")) {
       currentLocale = "en";
@@ -320,11 +330,23 @@
       }
     }
     localeBox.addChangeHandler(new ChangeHandler() {
+      @SuppressWarnings("deprecation")
       public void onChange(ChangeEvent event) {
String localeName = localeBox.getValue(localeBox.getSelectedIndex());
-        UrlBuilder builder = Location.createUrlBuilder().setParameter(
-            "locale", localeName);
-        Window.Location.replace(builder.buildString());
+        if (cookieName != null) {
+          // expire in one year
+          Date expires = new Date();
+          expires.setYear(expires.getYear() + 1);
+          Cookies.setCookie(cookieName, localeName, expires);
+        }
+        if (queryParam != null) {
+          UrlBuilder builder = Location.createUrlBuilder().setParameter(
+              queryParam, localeName);
+          Window.Location.replace(builder.buildString());
+        } else {
+          // If we are using only cookies, just reload
+          Window.Location.reload();
+        }
       }
     });
   }
=======================================
--- /trunk/user/src/com/google/gwt/i18n/client/LocaleInfo.java Tue Nov 23 06:49:43 2010 +++ /trunk/user/src/com/google/gwt/i18n/client/LocaleInfo.java Wed Jan 5 11:56:03 2011
@@ -67,6 +67,16 @@
      */
     return instance;
   }
+
+  /**
+   * Returns the name of the name of the cookie holding the locale to use,
+   * which is defined in the config property {...@code locale.cookie}.
+   *
+   * @return locale cookie name, or null if none
+   */
+  public static final String getLocaleCookieName() {
+    return instance.infoImpl.getLocaleCookieName();
+  }

   /**
* Returns the display name of the requested locale in its native locale, if
@@ -86,6 +96,16 @@
      */
     return instance.infoImpl.getLocaleNativeDisplayName(localeName);
   }
+
+  /**
+ * Returns the name of the query parameter holding the locale to use, which is
+   * defined in the config property {...@code locale.queryparam}.
+   *
+   * @return locale URL query parameter name, or null if none
+   */
+  public static String getLocaleQueryParam() {
+    return instance.infoImpl.getLocaleQueryParam();
+  }

   /**
    * Returns true if any locale supported by this build of the app is RTL.
@@ -150,7 +170,7 @@
   /**
    * @return an implementation of {...@link LocalizedNames} for this locale.
    */
-  public LocalizedNames getLocalizedNames() {
+  public final LocalizedNames getLocalizedNames() {
     return infoImpl.getLocalizedNames();
   }

=======================================
--- /trunk/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java Tue Nov 23 06:49:43 2010 +++ /trunk/user/src/com/google/gwt/i18n/client/impl/LocaleInfoImpl.java Wed Jan 5 11:56:03 2011
@@ -60,6 +60,16 @@
   public DateTimeFormatInfo getDateTimeFormatInfo() {
     return GWT.create(DateTimeFormatInfoImpl.class);
   }
+
+  /**
+   * Returns the name of the name of the cookie holding the locale to use,
+   * which is defined in the config property {...@code locale.cookie}.
+   *
+   * @return locale cookie name, or null if none
+   */
+  public String getLocaleCookieName() {
+    return null;
+  }

   /**
    * Returns the current locale name, such as "default, "en_US", etc.
@@ -80,6 +90,16 @@
   public String getLocaleNativeDisplayName(String localeName) {
     return null;
   }
+
+  /**
+ * Returns the name of the query parameter holding the locale to use, which is
+   * defined in the config property {...@code locale.queryparam}.
+   *
+   * @return locale URL query parameter name, or null if none
+   */
+  public String getLocaleQueryParam() {
+    return null;
+  }

   /**
    * @return an implementation of {...@link LocalizedNames} for this locale.
=======================================
--- /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoContext.java Thu Sep 9 08:24:17 2010 +++ /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoContext.java Wed Jan 5 11:56:03 2011
@@ -25,23 +25,32 @@
  * A LocaleUtils specific context for caching.
  */
 public class LocaleInfoContext {
+
   /**
    * A key for lookup of computed values in a cache.
    */
   private static class CacheKey {
     private final SelectionProperty localeProperty;
     private final ConfigurationProperty runtimeLocaleProperty;
+    private ConfigurationProperty queryParamProperty;
+    private ConfigurationProperty cookieProperty;

     /**
      * Create a key for cache lookup.
      *
      * @param localeProperty "locale" property, must not be null
* @param runtimeLocaleProperty "runtime.locales" property, must not be null
+     * @param cookieProperty "locale.queryparam" property, must not be null
+     * @param queryParamProperty "locale.cookie" property, must not be null
      */
     public CacheKey(SelectionProperty localeProperty,
-        ConfigurationProperty runtimeLocaleProperty) {
+        ConfigurationProperty runtimeLocaleProperty,
+        ConfigurationProperty queryParamProperty,
+        ConfigurationProperty cookieProperty) {
       this.localeProperty = localeProperty;
       this.runtimeLocaleProperty = runtimeLocaleProperty;
+      this.queryParamProperty = queryParamProperty;
+      this.cookieProperty = cookieProperty;
     }

     @Override
@@ -57,7 +66,9 @@
       }
       CacheKey other = (CacheKey) obj;
       return localeProperty.equals(other.localeProperty)
-          && runtimeLocaleProperty.equals(other.runtimeLocaleProperty);
+          && runtimeLocaleProperty.equals(other.runtimeLocaleProperty)
+          && queryParamProperty.equals(other.queryParamProperty)
+          && cookieProperty.equals(other.cookieProperty);
     }

     @Override
@@ -66,6 +77,8 @@
       int result = 1;
       result = prime * result + localeProperty.hashCode();
       result = prime * result + runtimeLocaleProperty.hashCode();
+      result = prime * result + queryParamProperty.hashCode();
+      result = prime * result + cookieProperty.hashCode();
       return result;
     }
   }
@@ -74,15 +87,18 @@
       CacheKey, LocaleUtils>();

   public LocaleUtils getLocaleUtils(SelectionProperty localeProperty,
-      ConfigurationProperty runtimeLocaleProperty) {
-    CacheKey key = new CacheKey(localeProperty, runtimeLocaleProperty);
+      ConfigurationProperty runtimeLocaleProperty,
+ ConfigurationProperty queryParamProp, ConfigurationProperty cookieProp) {
+    CacheKey key = new CacheKey(localeProperty, runtimeLocaleProperty,
+        queryParamProp, cookieProp);
     return localeUtilsCache.get(key);
   }
-
+
   public void putLocaleUtils(SelectionProperty localeProperty,
- ConfigurationProperty runtimeLocaleProperty, LocaleUtils localeUtils) {
-    CacheKey key = new CacheKey(localeProperty, runtimeLocaleProperty);
+ ConfigurationProperty runtimeLocaleProperty, ConfigurationProperty queryParamProp,
+      ConfigurationProperty cookieProp, LocaleUtils localeUtils) {
+    CacheKey key = new CacheKey(localeProperty, runtimeLocaleProperty,
+        queryParamProp, cookieProp);
     localeUtilsCache.put(key, localeUtils);
   }
-
-}
+}
=======================================
--- /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java Tue Nov 23 06:49:43 2010 +++ /trunk/user/src/com/google/gwt/i18n/rebind/LocaleInfoGenerator.java Wed Jan 5 11:56:03 2011
@@ -276,6 +276,22 @@
       }
       writer.println("}");
       writer.println();
+      String queryParam = localeUtils.getQueryParam();
+      if (queryParam != null) {
+        writer.println("@Override");
+        writer.println("public String getLocaleQueryParam() {");
+        writer.println("  return \"" + quoteQuotes(queryParam) + "\";");
+        writer.println("}");
+        writer.println();
+      }
+      String cookie = localeUtils.getCookie();
+      if (cookie != null) {
+        writer.println("@Override");
+        writer.println("public String getLocaleCookieName() {");
+        writer.println("  return \"" + quoteQuotes(cookie) + "\";");
+        writer.println("}");
+        writer.println();
+      }
       writer.println("@Override");
writer.println("public DateTimeFormatInfo getDateTimeFormatInfo() {"); LocalizableGenerator localizableGenerator = new LocalizableGenerator();
=======================================
--- /trunk/user/src/com/google/gwt/i18n/rebind/LocaleUtils.java Fri Oct 8 06:15:38 2010 +++ /trunk/user/src/com/google/gwt/i18n/rebind/LocaleUtils.java Wed Jan 5 11:56:03 2011
@@ -43,6 +43,18 @@
    */
   private static final String PROP_LOCALE = "locale";

+  /**
+ * The config property identifying the URL query paramter name to possibly get
+   * the value of the locale property.
+   */
+ private static final String PROP_LOCALE_QUERY_PARAM = "locale.queryparam";
+
+  /**
+ * The config property identifying the cookie name to possibly get the value
+   * of the locale property.
+   */
+  private static final String PROP_LOCALE_COOKIE = "locale.cookie";
+
   /**
    * The token representing the runtime.locales configuration property.
    */
@@ -70,11 +82,18 @@
           = propertyOracle.getSelectionProperty(logger, PROP_LOCALE);
       ConfigurationProperty runtimeLocaleProp
           = propertyOracle.getConfigurationProperty(PROP_RUNTIME_LOCALES);
+      ConfigurationProperty queryParamProp
+ = propertyOracle.getConfigurationProperty(PROP_LOCALE_QUERY_PARAM);
+      ConfigurationProperty cookieProp
+          = propertyOracle.getConfigurationProperty(PROP_LOCALE_COOKIE);
       LocaleInfoContext localeInfoCtx = getLocaleInfoCtx(context);
- LocaleUtils localeUtils = localeInfoCtx.getLocaleUtils(localeProp, runtimeLocaleProp);
+      LocaleUtils localeUtils = localeInfoCtx.getLocaleUtils(localeProp,
+          runtimeLocaleProp, queryParamProp, cookieProp);
       if (localeUtils == null) {
-        localeUtils = createInstance(localeProp, runtimeLocaleProp);
- localeInfoCtx.putLocaleUtils(localeProp, runtimeLocaleProp, localeUtils);
+        localeUtils = createInstance(localeProp, runtimeLocaleProp,
+            queryParamProp, cookieProp);
+        localeInfoCtx.putLocaleUtils(localeProp, runtimeLocaleProp,
+            queryParamProp, cookieProp, localeUtils);
       }
       return localeUtils;
     } catch (BadPropertyValueException e) {
@@ -85,7 +104,7 @@
       Set<GwtLocale> allLocales = new HashSet<GwtLocale>();
       allLocales.add(defaultLocale);
       return new LocaleUtils(defaultLocale, allLocales, allLocales,
-          Collections.<GwtLocale>emptySet());
+          Collections.<GwtLocale>emptySet(), null, null);
     }
   }

@@ -99,12 +118,21 @@
   }

   private static LocaleUtils createInstance(SelectionProperty localeProp,
-      ConfigurationProperty prop) {
+      ConfigurationProperty prop, ConfigurationProperty queryParamProp,
+      ConfigurationProperty cookieProp) {
     GwtLocale compileLocale = null;
     Set<GwtLocale> allLocales = new HashSet<GwtLocale>();
     Set<GwtLocale> allCompileLocales = new HashSet<GwtLocale>();
     Set<GwtLocale> runtimeLocales = new HashSet<GwtLocale>();
     String localeName = localeProp.getCurrentValue();
+    String queryParam = queryParamProp.getValues().get(0);
+    if (queryParam.length() == 0) {
+      queryParam = null;
+    }
+    String cookie = cookieProp.getValues().get(0);
+    if (cookie.length() == 0) {
+      cookie = null;
+    }
     SortedSet<String> localeValues = localeProp.getPossibleValues();

     GwtLocaleFactory factoryInstance = getLocaleFactory();
@@ -140,7 +168,7 @@
       }
     }
     return new LocaleUtils(compileLocale, allLocales, allCompileLocales,
-        runtimeLocales);
+        runtimeLocales, queryParam, cookie);
   }

   private static synchronized LocaleInfoContext getLocaleInfoCtx(
@@ -164,12 +192,19 @@

   private final Set<GwtLocale> runtimeLocales;

+  private final String queryParam;
+
+  private final String cookie;
+
   private LocaleUtils(GwtLocale compileLocale, Set<GwtLocale> allLocales,
-      Set<GwtLocale> allCompileLocales, Set<GwtLocale> runtimeLocales) {
+      Set<GwtLocale> allCompileLocales, Set<GwtLocale> runtimeLocales,
+      String queryParam, String cookie) {
     this.compileLocale = compileLocale;
     this.allLocales = Collections.unmodifiableSet(allLocales);
this.allCompileLocales = Collections.unmodifiableSet(allCompileLocales);
     this.runtimeLocales = Collections.unmodifiableSet(runtimeLocales);
+    this.queryParam = queryParam;
+    this.cookie = cookie;
   }

   /**
@@ -197,6 +232,25 @@
   public GwtLocale getCompileLocale() {
     return compileLocale;
   }
+
+  /**
+ * Return the name of the cookie to potentially get the locale value from.
+   *
+   * @return the cookie name or null if none
+   */
+  public String getCookie() {
+    return cookie;
+  }
+
+  /**
+ * Return the name of the URL query param to potentially get the locale value
+   * from.
+   *
+   * @return the URL query param or null if none
+   */
+  public String getQueryParam() {
+    return queryParam;
+  }

   /**
* Returns a list of locales which are children of the current compile-time
=======================================
--- /trunk/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml Thu Apr 22 07:49:33 2010 +++ /trunk/user/test/com/google/gwt/i18n/I18NTest_en.gwt.xml Wed Jan 5 11:56:03 2011
@@ -13,12 +13,14 @@
<!-- limitations under the License. -->

 <module>
-       <!-- Inherit the JUnit support -->
-       <inherits name='com.google.gwt.junit.JUnit'/>
-       <inherits name = 'com.google.gwt.i18n.I18N'/>
-       <!-- 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"/>
+  <!-- Inherit the JUnit support -->
+  <inherits name='com.google.gwt.junit.JUnit'/>
+  <inherits name = 'com.google.gwt.i18n.I18N'/>
+  <!-- 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"/>
+  <set-configuration-property name="locale.queryparam" value=""/>
+  <set-configuration-property name="locale.cookie" value="LOCALE"/>
 </module>
=======================================
--- /trunk/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java Tue Nov 23 06:49:43 2010 +++ /trunk/user/test/com/google/gwt/i18n/client/LocaleInfoTest.java Wed Jan 5 11:56:03 2011
@@ -32,6 +32,11 @@
     assertArrayEquals(new String[] {
"default", "piglatin", "piglatin_UK", "piglatin_UK_WINDOWS"}, locales);
   }
+
+  public void testCookieName() {
+ String cookieName = LocaleInfo.getCurrentLocale().getLocaleCookieName();
+    assertNull(cookieName);
+  }

   public void testCurrentLocale() {
     String locale = LocaleInfo.getCurrentLocale().getLocaleName();
@@ -47,6 +52,11 @@
     displayName = LocaleInfo.getLocaleNativeDisplayName("piglatin");
     assertEquals("Igpay Atinlay", displayName);
   }
+
+  public void testQueryParam() {
+ String queryParam = LocaleInfo.getCurrentLocale().getLocaleQueryParam();
+    assertEquals("locale", queryParam);
+  }

   public void testRTL() {
     boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();
=======================================
--- /trunk/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java Thu Apr 22 07:49:33 2010 +++ /trunk/user/test/com/google/gwt/i18n/client/LocaleInfo_en_Test.java Wed Jan 5 11:56:03 2011
@@ -27,7 +27,8 @@

   @Override
   public String getModuleName() {
-    // This module is built in the en locale, but includes ar.
+    // This module is built in the en locale, but includes ar.  It also has
+    // custom settings for the locale query parameter and cookie name.
     return "com.google.gwt.i18n.I18NTest_en";
   }

@@ -39,6 +40,16 @@
     assertTrue(localeList.contains("default"));
     assertTrue(localeList.contains("en_US"));
   }
+
+  public void testCookieName() {
+ String cookieName = LocaleInfo.getCurrentLocale().getLocaleCookieName();
+    assertEquals("LOCALE", cookieName);
+  }
+
+  public void testQueryParam() {
+ String queryParam = LocaleInfo.getCurrentLocale().getLocaleQueryParam();
+    assertNull(queryParam);
+  }

   public void testRTL() {
     boolean isRTL = LocaleInfo.getCurrentLocale().isRTL();

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to