Skip to content

Commit

Permalink
Merge pull request #1 from tuenti/fix-apostrophe-with-placeholders
Browse files Browse the repository at this point in the history
 fix jgettext#55 Using the apostrophe character with {0}
  • Loading branch information
victormferrara authored Jun 19, 2018
2 parents b239852 + 039a43e commit dd6b09f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/java/org/xnap/commons/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -324,6 +324,12 @@ public final String trn(String text, String pluralText, long n)
/**
* Returns the plural form for <code>n</code> of the translation of
* <code>text</code>.
* <p>
* Occurrences of {number} placeholders in text are replaced by
* <code>objects</code>.
* <p>
* Invokes
* {@link MessageFormat#format(java.lang.String, java.lang.Object[])}.
*
* @param text
* the key string to be translated.
Expand All @@ -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 <code>MessageFormat.format()</code>
* @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);
}

/**
Expand Down Expand Up @@ -525,6 +531,12 @@ public final String trnc(String context, String singularText, String pluralText,
/**
* Returns the plural form for <code>n</code> of the translation of
* <code>text</code>.
* <p>
* Occurrences of {number} placeholders in text are replaced by
* <code>objects</code>.
* <p>
* Invokes
* {@link MessageFormat#format(java.lang.String, java.lang.Object[])}.
*
* @param context
* the context of the message to disambiguate it when translating
Expand All @@ -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 <code>MessageFormat.format()</code>
* @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);
}

/**
Expand All @@ -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 });
}

/**
Expand All @@ -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 });
}

/**
Expand All @@ -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 });
}

/**
Expand All @@ -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);
}

}
44 changes: 44 additions & 0 deletions src/test/org/xnap/commons/i18n/I18nTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd6b09f

Please sign in to comment.