diff --git a/src/java/org/xnap/commons/i18n/I18n.java b/src/java/org/xnap/commons/i18n/I18n.java index d0ddd1f..d7e0d72 100644 --- a/src/java/org/xnap/commons/i18n/I18n.java +++ b/src/java/org/xnap/commons/i18n/I18n.java @@ -255,7 +255,7 @@ public final String tr(String text) */ public final String tr(String text, Object[] objects) { - return MessageFormat.format(tr(text), objects); + return formatMessage(tr(text), objects); } /** @@ -324,6 +324,12 @@ public final String trn(String text, String pluralText, long n) /** * Returns the plural form for n of the translation of * text. + *

+ * Occurrences of {number} placeholders in text are replaced by + * objects. + *

+ * Invokes + * {@link MessageFormat#format(java.lang.String, java.lang.Object[])}. * * @param text * the key string to be translated. @@ -332,13 +338,13 @@ public final String trn(String text, String pluralText, long n) * @param n * value that determines the plural form * @param objects - * object args to be formatted and substituted. + * arguments to MessageFormat.format() * @return the translated text * @since 0.9 */ public final String trn(String text, String pluralText, long n, Object[] objects) { - return MessageFormat.format(trn(text, pluralText, n), objects); + return formatMessage(trn(text, pluralText, n), objects); } /** @@ -525,6 +531,12 @@ public final String trnc(String context, String singularText, String pluralText, /** * Returns the plural form for n of the translation of * text. + *

+ * Occurrences of {number} placeholders in text are replaced by + * objects. + *

+ * Invokes + * {@link MessageFormat#format(java.lang.String, java.lang.Object[])}. * * @param context * the context of the message to disambiguate it when translating @@ -535,12 +547,12 @@ public final String trnc(String context, String singularText, String pluralText, * @param n * value that determines the plural form * @param objects - * object args to be formatted and substituted. + * arguments to MessageFormat.format() * @return the translated text * @since 0.9 */ public final String trnc(String context, String singularText, String pluralText, long n, Object[] objects) { - return MessageFormat.format(trnc(context, singularText, pluralText, n), objects); + return formatMessage(trnc(context, singularText, pluralText, n), objects); } /** @@ -551,7 +563,7 @@ public final String trnc(String context, String singularText, String pluralText, * @since 0.9.5 */ public final String trnc(String comment, String singularText, String pluralText, long n, Object obj) { - return MessageFormat.format(trnc(comment, singularText, pluralText, n), new Object[] { obj }); + return trnc(comment, singularText, pluralText, n, new Object[] { obj }); } /** @@ -562,7 +574,7 @@ public final String trnc(String comment, String singularText, String pluralText, * @since 0.9.5 */ public final String trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2) { - return MessageFormat.format(trnc(comment, singularText, pluralText, n), new Object[] { obj1, obj2 }); + return trnc(comment, singularText, pluralText, n, new Object[] { obj1, obj2 }); } /** @@ -573,7 +585,7 @@ public final String trnc(String comment, String singularText, String pluralText, * @since 0.9.5 */ public final String trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2, Object obj3) { - return MessageFormat.format(trnc(comment, singularText, pluralText, n), new Object[] { obj1, obj2, obj3 }); + return trnc(comment, singularText, pluralText, n, new Object[] { obj1, obj2, obj3 }); } /** @@ -584,7 +596,15 @@ public final String trnc(String comment, String singularText, String pluralText, * @since 0.9.5 */ public final String trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2, Object obj3, Object obj4) { - return MessageFormat.format(trnc(comment, singularText, pluralText, n), new Object[] { obj1, obj2, obj3, obj4 }); + return trnc(comment, singularText, pluralText, n, new Object[] { obj1, obj2, obj3, obj4 }); } - + + /** + * Convenience method that invokes {@link MessageFormat#format(java.lang.String, java.lang.Object[])} escaping + * the single quotes to avoid loosing them. + */ + private String formatMessage(String message, Object[] objects) { + return MessageFormat.format(message.replace("'", "''"), objects); + } + } diff --git a/src/test/org/xnap/commons/i18n/I18nTest.java b/src/test/org/xnap/commons/i18n/I18nTest.java index da3fa06..452ae1b 100644 --- a/src/test/org/xnap/commons/i18n/I18nTest.java +++ b/src/test/org/xnap/commons/i18n/I18nTest.java @@ -86,6 +86,12 @@ public void testTr4() assertEquals("Foo foo bar baz boing", i18nEN.tr("Foo {0} {1} {2} {3}", "foo", "bar", "baz", "boing")); } + public void testTrApostropheWithPlaceholder() + { + assertEquals("It's friday!", i18nEN.tr("It's {0}!", "friday")); + assertEquals("It's friday! Yes it's friday!", i18nEN.tr("It's {0}! Yes it's {0}!", "friday")); + } + public void testMarktr() { assertEquals(I18n.marktr("Foo"), "Foo"); @@ -184,6 +190,44 @@ public void testTrn4() .trn("Foo {0} {1} {2} {3}", "Foos", 1, "foo", "bar", "baz", "boing")); } + public void testTrnApostropheWithPlaceholder() + { + assertEquals("It's friday!", i18nEN.trn("It's {0}!", "Foos", 1, "friday")); + assertEquals("It's friday! Yes it's friday!", i18nEN.trn("It's {0}! Yes it's {0}!", "Foos", 1, "friday")); + } + + public void testTrnc() + { + assertEquals("Foo", i18nEN.trnc("ctx", "Foo", "{0} Bars", 1)); + assertEquals("{0} Bars", i18nEN.trnc("ctx", "Foo", "{0} Bars", 2)); + assertEquals("2 Bars", i18nEN.trnc("ctx", "Foo", "{0} Bars", 2, new Integer(2))); + } + + public void testTrnc1() + { + assertEquals("Foo foo ", i18nEN.trnc("ctx", "Foo {0} ", "Foos {0}", 1, "foo")); + } + + public void testTrnc2() + { + assertEquals("Foo bar foo", i18nEN.trnc("ctx", "Foo {1} {0}", "Foos", 1, "foo", "bar")); + assertEquals("Foo foo bar", i18nEN.trnc("ctx", "Foo {0} {1}", "Foos", 1, "foo", "bar")); + } + + public void testTrnc3() + { + assertEquals("Foo bar baz foo", i18nEN.trnc("ctx", "Foo {1} {2} {0}", "Foos", 1, "foo", "bar", "baz")); + assertEquals("Foo foo bar baz", i18nEN.trnc("ctx", "Foo {0} {1} {2}", "Foos", 1, "foo", "bar", "baz")); + } + + public void testTrnc4() + { + assertEquals("Foo bar baz boing foo", i18nEN + .trnc("ctx", "Foo {1} {2} {3} {0}", "Foos", 1, "foo", "bar", "baz", "boing")); + assertEquals("Foo foo bar baz boing", i18nEN + .trnc("ctx", "Foo {0} {1} {2} {3}", "Foos", 1, "foo", "bar", "baz", "boing")); + } + public void testSetEmptyResources() { // this should load the empty resource bundle