Commits

Anonymous committed bea98bc

DefaultTextProvider does not parse default message with a MessageFormat
Issue number: XW-363
Obtained from:
Submitted by:
Reviewed by:

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

  • Participants
  • Parent commits cd33994

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork/DefaultTextProvider.java

 import java.util.ResourceBundle;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.text.MessageFormat;
 
 /**
  * DefaultTextProvider gets texts from only the default resource bundles associated with the
     public String getText(String key, String defaultValue, List args) {
         String text = getText(key, args);
         if (text == null) {
-            return defaultValue;
+            MessageFormat format = new MessageFormat(defaultValue);
+            format.setLocale(ActionContext.getContext().getLocale());
+            format.applyPattern(defaultValue);
+
+            Object[] params;
+            if (args != null) {
+                params = args.toArray();
+            } else {
+                params = EMPTY_ARGS;
+            }
+
+            return format.format(params);
         }
         return text;
     }
     public String getText(String key, String defaultValue, String[] args) {
         String text = getText(key, args);
         if (text == null) {
-            return defaultValue;
+            MessageFormat format = new MessageFormat(defaultValue);
+            format.setLocale(ActionContext.getContext().getLocale());
+            format.applyPattern(defaultValue);
+
+            if (args == null) {
+                return format.format(EMPTY_ARGS);
+            }
+
+            return format.format(args);
         }
         return text;
     }

src/test/com/opensymphony/xwork/DefaultTextProviderTest.java

         assertEquals("Hello World", tp.getText("hello", "this is default", "from me")); // no args in bundle
         assertEquals("Hello World from me", tp.getText("hello.0", "this is default", "from me"));
         assertEquals("this is default", tp.getText("not.in.bundle", "this is default", "from me"));
-        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", "from me"));
+        assertEquals("this is default from me", tp.getText("not.in.bundle", "this is default {0}", "from me"));
 
         assertEquals(null, tp.getText("not.in.bundle"));
     }
         assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args));
 
         assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args));
-        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args));
-        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
 
         assertEquals("Hello World", tp.getText("hello", args)); // no args in bundle
         assertEquals("Hello World Santa", tp.getText("hello.0", args)); // only 1 arg in bundle
         assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args));
 
         assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args));
-        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args));
-        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
 
         assertEquals("Hello World", tp.getText("hello", args)); // no args in bundle
         assertEquals("Hello World Santa", tp.getText("hello.0", args)); // only 1 arg in bundle
         assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args, stack));
 
         assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args, stack));
-        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args, stack));
-        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args, stack));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
     }
 
     public void testGetTextsWithArrayAndStack() throws Exception {
         assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args, stack));
 
         assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args, stack));
-        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args, stack));
-        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args, stack));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
     }
 
     public void testGetBundle() throws Exception {