From: David ‘Bombe’ Roden Date: Fri, 18 Nov 2016 19:23:14 +0000 (+0100) Subject: Add l10n text and parameter container X-Git-Tag: 0.9.7^2~423 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8dbd4962313978d19dd464ead0b413d405921ae3;p=Sone.git Add l10n text and parameter container --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java index 3b4af2a..e56321d 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java +++ b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java @@ -53,6 +53,13 @@ public class L10nFilter implements Filter { */ @Override public String format(TemplateContext templateContext, Object data, Map 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)); } diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nText.kt b/src/main/java/net/pterodactylus/sone/freenet/L10nText.kt new file mode 100644 index 0000000..0c975be --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/L10nText.kt @@ -0,0 +1,6 @@ +package net.pterodactylus.sone.freenet + +/** + * Container for an l10n key and optional values. + */ +data class L10nText(val text: String, val parameters: List = emptyList()) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt index f070df2..d5d801e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt @@ -46,4 +46,16 @@ class L10nFilterTest { 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")) + } + }