Commits

Anonymous committed 716bbe8

Refactored localeFromString method to LocalizedTextUtil (was todo)
Issue number:
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@897e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork/interceptor/I18nInterceptor.java

 package com.opensymphony.xwork.interceptor;
 
 import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.util.LocalizedTextUtil;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
                     + invocation.getProxy().getNamespace() + "/"
                     + invocation.getProxy().getActionName() + "' { ");
         }
-
         //get requested locale
         Map params = invocation.getInvocationContext().getParameters();
         Object requested_locale = params.remove(parameterName);
         if (session != null) {
             if (requested_locale != null) {
                 Locale locale = (requested_locale instanceof Locale) ?
-                        (Locale) requested_locale : localeFromString(requested_locale.toString());
+                        (Locale) requested_locale : LocalizedTextUtil.localeFromString(requested_locale.toString(), null);
                 if (log.isDebugEnabled()) {
                     log.debug("store locale=" + locale);
                 }
                     log.debug("apply locale=" + locale);
                 }
 
-                invocation.getInvocationContext().setLocale((Locale) locale);
+                saveLocale(invocation, (Locale)locale);
             }
         }
 
         return result;
     }
 
-    private Locale localeFromString(String localeStr) {
-        if ((localeStr == null) || (localeStr.trim().length() == 0) || (localeStr.equals("_"))) {
-            return Locale.getDefault();
-        }
-
-        int index = localeStr.indexOf('_');
-        if (index < 0) {
-            return new Locale(localeStr);
-        }
-
-        String language = localeStr.substring(0, index);
-        if (index == localeStr.length()) {
-            return new Locale(language);
-        }
-
-        localeStr = localeStr.substring(index + 1);
-        index = localeStr.indexOf('_');
-        if (index < 0) {
-            return new Locale(language, localeStr);
-        }
-
-        String country = localeStr.substring(0, index);
-        if (index == localeStr.length()) {
-            return new Locale(language, country);
-        }
-
-        localeStr = localeStr.substring(index + 1);
-        return new Locale(language, country, localeStr);
+    /**
+     * Save the given locale to the ActionInvocation.
+     *
+     * @param invocation The ActionInvocation.
+     * @param locale The locale to save.
+     */
+    protected void saveLocale(ActionInvocation invocation, Locale locale) {
+        invocation.getInvocationContext().setLocale(locale);
     }
+
 }

src/java/com/opensymphony/xwork/util/LocalizedTextUtil.java

     }
 
     /**
+     * Builds a {@link java.util.Locale} from a String of the form en_US_foo into a Locale
+     * with language "en", country "US" and variant "foo". This will parse the output of
+     * {@link java.util.Locale#toString()}.
+     *
+     * @param localeStr The locale String to parse.
+     * @param defaultLocale The locale to use if localeStr is <tt>null</tt>.
+     * @return requested Locale
+     */
+    public static Locale localeFromString(String localeStr, Locale defaultLocale) {
+        if ((localeStr == null) || (localeStr.trim().length() == 0) || (localeStr.equals("_"))) {
+            if ( defaultLocale != null) {
+                return defaultLocale;
+            }
+            return Locale.getDefault();
+        }
+
+        int index = localeStr.indexOf('_');
+        if (index < 0) {
+            return new Locale(localeStr);
+        }
+
+        String language = localeStr.substring(0, index);
+        if (index == localeStr.length()) {
+            return new Locale(language);
+        }
+
+        localeStr = localeStr.substring(index + 1);
+        index = localeStr.indexOf('_');
+        if (index < 0) {
+            return new Locale(language, localeStr);
+        }
+
+        String country = localeStr.substring(0, index);
+        if (index == localeStr.length()) {
+            return new Locale(language, country);
+        }
+
+        localeStr = localeStr.substring(index + 1);
+        return new Locale(language, country, localeStr);
+    }
+
+    /**
      * Returns a localized message for the specified key, aTextName.  Neither the key nor the
      * message is evaluated.
      *

src/test/com/opensymphony/xwork/interceptor/I18nInterceptorTest.java

 
         assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
 
+        System.out.println("session: " + session);
         assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
         assertEquals(Locale.getDefault(), session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
     }