From 51e20ad24f9b0eae0b08ed5dbe0c69299d17116e 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:32:32 +0100 Subject: [PATCH] Refactor l10n filter --- .../net/pterodactylus/sone/freenet/L10nFilter.java | 28 +++++++++++++++------- .../pterodactylus/sone/freenet/L10nFilterTest.kt | 8 ++++++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java index e56321d..7ca9a5c 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java +++ b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import javax.annotation.Nonnull; + import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.TemplateContext; @@ -53,15 +55,23 @@ 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()); + List parameterValues = getParameters(data, parameters); + String text = getText(data); + if (parameterValues.isEmpty()) { + return webInterface.getL10n().getString(text); } - if (parameters.isEmpty()) { - return webInterface.getL10n().getString(String.valueOf(data)); + return new MessageFormat(webInterface.getL10n().getString(text), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(parameterValues.toArray()); + } + + @Nonnull + private String getText(Object data) { + return (data instanceof L10nText) ? ((L10nText) data).getText() : String.valueOf(data); + } + + @Nonnull + private List getParameters(Object data, Map parameters) { + if (data instanceof L10nText) { + return ((L10nText) data).getParameters(); } List parameterValues = new ArrayList(); int parameterIndex = 0; @@ -70,7 +80,7 @@ public class L10nFilter implements Filter { parameterValues.add(value); ++parameterIndex; } - return new MessageFormat(webInterface.getL10n().getString(String.valueOf(data)), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(parameterValues.toArray()); + return parameterValues; } } diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt index d5d801e..39196c1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt @@ -49,7 +49,7 @@ class L10nFilterTest { @Test fun `filter processes l10n text without parameters correctly`() { translations["data"] = "translated data" - assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data")) + assertThat(filter.format(templateContext, L10nText("data"), emptyMap()), equalTo("translated data")) } @Test @@ -58,4 +58,10 @@ class L10nFilterTest { assertThat(filter.format(templateContext, L10nText("data", listOf(4.5, "data")), emptyMap()), equalTo("translated 4.5 data")) } + @Test + fun `filter does not replace values if there are no parameters`() { + translations["data"] = "{link}" + assertThat(filter.format(templateContext, "data", emptyMap()), equalTo("{link}")); + } + } -- 2.7.4