Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
*/
public final class PropertiesTranslationProvider implements TranslationProvider {

// https://regex101.com/r/FaX3tj/1
private static final Pattern TRANSLATION_ARG_FORMAT_PATTERN = Pattern.compile("\\{(\\d+)\\$.+?\\$}");
// https://regex101.com/r/Xs7lNp/1
private static final Pattern TRANSLATION_ARG_FORMAT_PATTERN = Pattern.compile("\\{(\\d+|\\d+,.+?)\\$.+?\\$}");

// a lock that must be held when using a MessageFormat to format a translation
// this is due to the fact that MessageFormats are not thread safe, but we don't expect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,21 @@ void testTranslationProvidersUnregisterByClassLoader() {
Assertions.assertEquals("<missing translation for 1 in en-US>", i18n.translate("1"));
Assertions.assertEquals("<missing translation for 2 in en-US>", i18n.translate("2"));
}

@Test
void testAdvancedMessageFormatPatternsArePickedUp() {
var properties = new Properties();
properties.put("1", "Hello {0$name$}, your status is {1, choice, 0#deactivated|1#active$status$}");
properties.put("2", "{0} {1$name$} {2,number,integer$an int$} {3,choice,0#hello|1#world|2#test$some description$}");
var provider = PropertiesTranslationProvider.fromProperties(properties);

var i18n = I18n.i18n();
i18n.registerProvider(i18n.selectedLanguage(), provider);

Assertions.assertEquals("Hello Rob, your status is active", i18n.translate("1", "Rob", 1));
Assertions.assertEquals("Hello Klaro, your status is deactivated", i18n.translate("1", "Klaro", 0));

Assertions.assertEquals("World Rob 56,789 test", provider.translate("2", "World", "Rob", 56789, 2));
Assertions.assertEquals("Hello World 1,234,568 world", provider.translate("2", "Hello", "World", 1234567.89, 1));
}
}
Loading