Skip to content

Commit

Permalink
fix jgettext#55 Using the apostrophe character with {0}
Browse files Browse the repository at this point in the history
  • Loading branch information
vmunoz committed Jun 19, 2018
1 parent b239852 commit a697ef5
Show file tree
Hide file tree
Showing 2 changed files with 42 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);
}

}
12 changes: 12 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,12 @@ 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 testSetEmptyResources()
{
// this should load the empty resource bundle
Expand Down

0 comments on commit a697ef5

Please sign in to comment.