diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java b/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java index 58d4d3065..31eb5d36c 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java @@ -22,6 +22,7 @@ import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import java.io.Serializable; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Locale; @@ -134,6 +135,25 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex return getTextProvider().getText(key, defaultValue, args, stack); } + /** + * Dedicated method to support I10N and conversion errors + * + * @param key message which contains formatting string + * @param expr that should be formatted + * @return formatted expr with format specified by key + */ + public String getFormatted(String key, String expr) { + Map conversionErrors = ActionContext.getContext().getConversionErrors(); + if (conversionErrors.containsKey(expr)) { + String[] vals = (String[]) conversionErrors.get(expr); + return vals[0]; + } else { + final ValueStack valueStack = ActionContext.getContext().getValueStack(); + final Object val = valueStack.findValue(expr); + return getText(key, Arrays.asList(val)); + } + } + public ResourceBundle getTexts() { return getTextProvider().getTexts(); } diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java index aec6acb6f..23206abb4 100644 --- a/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java +++ b/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java @@ -313,8 +313,35 @@ public class ActionSupportTest extends XWorkTestCase { assertEquals(rb, mas.getTexts(MyActionSupport.class.getName())); } + public void testFormattingSupport() throws Exception { + ActionContext.getContext().setLocale(new Locale("da")); + MyActionSupport mas = new MyActionSupport(); + ActionContext.getContext().getValueStack().push(mas); + + mas.setVal(234d); + + String formatted = mas.getFormatted("format.number", "val"); + + assertEquals("234,0", formatted); + } + + public void testFormattingSupportWithConversionError() throws Exception { + ActionContext.getContext().getConversionErrors().put("val", new String[]{"4567def"}); + ActionContext.getContext().setLocale(new Locale("da")); + MyActionSupport mas = new MyActionSupport(); + ActionContext.getContext().getValueStack().push(mas); + + mas.setVal(234d); + + String formatted = mas.getFormatted("format.number", "val"); + + assertEquals("4567def", formatted); + } + private class MyActionSupport extends ActionSupport { + private Double val; + @Override public String doDefault() throws Exception { return "santa"; @@ -325,6 +352,14 @@ public class ActionSupportTest extends XWorkTestCase { super.validate(); // to have code coverage addActionMessage("validation was called"); } + + public Double getVal() { + return val; + } + + public void setVal(Double val) { + this.val = val; + } } } diff --git a/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties b/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties index b01a30fa2..1a4a51b79 100644 --- a/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties +++ b/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties @@ -6,3 +6,5 @@ hello=Hello World hello.0=Hello World {0} hello.1=Hello World. This is {0} speaking {1} + +format.number = {0,number,#0.0##}