From 8dbd4962313978d19dd464ead0b413d405921ae3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 18 Nov 2016 20:23:14 +0100 Subject: [PATCH] Add l10n text and parameter container --- src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java | 7 +++++++ src/main/java/net/pterodactylus/sone/freenet/L10nText.kt | 6 ++++++ .../kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt | 12 ++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/freenet/L10nText.kt 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")) + } + } -- 2.7.4