You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/06/26 21:07:52 UTC

svn commit: r671988 - in /velocity/tools/trunk/src: main/java/org/apache/velocity/tools/generic/ResourceTool.java test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java

Author: nbubna
Date: Thu Jun 26 12:07:52 2008
New Revision: 671988

URL: http://svn.apache.org/viewvc?rev=671988&view=rev
Log:
have Locale methods accept locales in string form too

Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ResourceTool.java
    velocity/tools/trunk/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ResourceTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ResourceTool.java?rev=671988&r1=671987&r2=671988&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ResourceTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ResourceTool.java Thu Jun 26 12:07:52 2008
@@ -23,6 +23,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
+import org.apache.velocity.tools.ConversionUtils;
 import org.apache.velocity.tools.config.DefaultKey;
 
 /**
@@ -146,7 +147,7 @@
         return new Key(null, new String[] { bundle }, getLocale(), null);
     }
 
-    public Key locale(Locale locale)
+    public Key locale(Object locale)
     {
         return new Key(null, this.bundles, locale, null);
     }
@@ -176,18 +177,32 @@
      * Returns the value for the specified key in the ResourceBundle for
      * the specified basename and locale.  If no such resource can be
      * found, no errors are thrown and {@code null} is returned.
+     *
+     * @param k the key for the requested resource
+     * @param baseName the base name of the resource bundle to search
+     * @param l the locale to use
      */
-    public Object get(Object k, String baseName, Locale locale)
+    public Object get(Object k, String baseName, Object l)
     {
         if (baseName == null || k == null)
         {
             return null;
         }
         String key = k == null ? null : String.valueOf(k);
-        if (locale == null)
+        Locale locale;
+        if (l == null)
         {
             locale = getLocale();
         }
+        else
+        {
+            locale = toLocale(l);
+            // if conversion fails, return null to indicate an error
+            if (locale == null)
+            {
+                return null;
+            }
+        }
 
         ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale);
         if (bundle != null)
@@ -209,13 +224,17 @@
      * specified bundles in which a matching resource is found.
      * If no resource is found, no exception will be thrown and {@code null}
      * will be returned.
+     *
+     * @param k the key for the requested resource
+     * @param bundles the resource bundles to search
+     * @param l the locale to use
      */
-    public Object get(Object k, String[] bundles, Locale locale)
+    public Object get(Object k, String[] bundles, Object l)
     {
         String key = k == null ? null : String.valueOf(k);
         for (int i=0; i < bundles.length; i++)
         {
-            Object resource = get(key, bundles[i], locale);
+            Object resource = get(key, bundles[i], l);
             if (resource != null)
             {
                 return resource;
@@ -224,6 +243,20 @@
         return null;
     }
 
+    private Locale toLocale(Object obj)
+    {
+        if (obj == null)
+        {
+            return null;
+        }
+        if (obj instanceof Locale)
+        {
+            return (Locale)obj;
+        }
+        String s = String.valueOf(obj);
+        return ConversionUtils.toLocale(s);
+    }
+
     /**
      * Renders the specified resource value and arguments as a String.
      * The resource is treated as a {@link MessageFormat} pattern which
@@ -253,14 +286,14 @@
         // these are copied and/or altered when a mutator is called
         private final String[] bundles;
         private final String key;
-        private final Locale locale;
+        private final Object locale;
         private final Object[] args;
 
         // these are not copied when a mutator is called
         private boolean cached = false;
         private Object rawValue;
 
-        public Key(String key, String[] bundles, Locale locale, Object[] args)
+        public Key(String key, String[] bundles, Object locale, Object[] args)
         {
             this.key = key;
             this.bundles = bundles;
@@ -295,7 +328,7 @@
             return new Key(this.key, newBundles, this.locale, this.args);
         }
 
-        public Key locale(Locale locale)
+        public Key locale(Object locale)
         {
             return new Key(this.key, this.bundles, locale, this.args);
         }

Modified: velocity/tools/trunk/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java?rev=671988&r1=671987&r2=671988&view=diff
==============================================================================
--- velocity/tools/trunk/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java (original)
+++ velocity/tools/trunk/src/test/java/org/apache/velocity/tools/test/whitebox/GenericToolsTests.java Thu Jun 26 12:07:52 2008
@@ -194,7 +194,7 @@
         ResourceTool.Key halfFrenchHelloWorld = helloWorld.locale(Locale.FRENCH);
         assertStringEquals("Bonjour World!", halfFrenchHelloWorld);
 
-        ResourceTool.Key frenchTool = textTool.locale(Locale.FRENCH);
+        ResourceTool.Key frenchTool = textTool.locale("fr");
         ResourceTool.Key frenchHelloWorld =
             frenchTool.get("hello.whoever").insert(frenchTool.get("world"));
         assertStringEquals("Bonjour Monde!", frenchHelloWorld);