*/
@Override
public String format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+ if (data instanceof L10nText) {
+ L10nText l10nText = (L10nText) data;
+ if (l10nText.getParameters().isEmpty()) {
+ return webInterface.getL10n().getString(l10nText.getText());
+ }
+ return new MessageFormat(webInterface.getL10n().getString(l10nText.getText()), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(l10nText.getParameters().toArray());
+ }
if (parameters.isEmpty()) {
return webInterface.getL10n().getString(String.valueOf(data));
}
--- /dev/null
+package net.pterodactylus.sone.freenet
+
+/**
+ * Container for an l10n key and optional values.
+ */
+data class L10nText(val text: String, val parameters: List<Any?> = emptyList())
assertThat(filter.format(templateContext, "data", mapOf("0" to 4.5, "1" to "data")), equalTo("translated 4.5 data"))
}
+ @Test
+ fun `filter processes l10n text without parameters correctly`() {
+ translations["data"] = "translated data"
+ assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data"))
+ }
+
+ @Test
+ fun `filter processes l10n text with parameters correctly`() {
+ translations["data"] = "translated {0,number} {1}"
+ assertThat(filter.format(templateContext, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data"))
+ }
+
}