Skip to content

Commit ec73202

Browse files
authored
Merge pull request #4500 from Meemaw/fix/i18n-message-format-locale
Format i18n message arguments with the message locale
2 parents 4c09620 + 3d47ff6 commit ec73202

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/main/java/graphql/i18n/I18n.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private String msgImpl(String msgKey, Object[] msgArgs) {
8989
} catch (MissingResourceException e) {
9090
assertShouldNeverHappen("There must be a resource bundle key called %s", msgKey);
9191
}
92-
return new MessageFormat(msgPattern).format(msgArgs);
92+
// format any arguments (eg numbers) with the same locale as the message rather than the JVM default locale
93+
return new MessageFormat(msgPattern, locale).format(msgArgs);
9394
}
9495
}

‎src/test/groovy/graphql/i18n/I18nTest.groovy‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ class I18nTest extends Specification {
7373
message == "Validierungsfehler ({0}) : Type definition '{1}' ist nicht ausführbar"
7474
}
7575

76+
def "message arguments are formatted with the message locale not the JVM default locale"() {
77+
// Slovenian formats -1 with a unicode minus sign and groups thousands with a dot
78+
def originalDefault = Locale.getDefault()
79+
def originalFormatDefault = Locale.getDefault(Locale.Category.FORMAT)
80+
Locale.setDefault(Locale.forLanguageTag("sl-SI"))
81+
82+
when:
83+
def englishMessage = I18n.i18n(BundleType.Parsing, Locale.ENGLISH).msg("InvalidSyntaxMoreTokens.noMessage", -1, 1234)
84+
def germanMessage = I18n.i18n(BundleType.Parsing, Locale.GERMAN).msg("InvalidSyntaxMoreTokens.noMessage", -1, 1234)
85+
86+
then:
87+
englishMessage.endsWith("Offending token at line -1 column 1,234")
88+
germanMessage.endsWith("Ungültiges Token in Zeile -1 Spalte 1.234")
89+
90+
cleanup:
91+
Locale.setDefault(originalDefault)
92+
Locale.setDefault(Locale.Category.FORMAT, originalFormatDefault)
93+
}
94+
7695
def "integration test of valid messages"() {
7796
def sdl = """
7897
type Query {

0 commit comments

Comments
 (0)