From: David ‘Bombe’ Roden Date: Sat, 9 Mar 2019 00:00:06 +0000 (+0100) Subject: ♻️ Only hand in L10n to l10n filter X-Git-Tag: v79^2~80 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=c9b4749797931ab804673640f4a15511900f150a;hp=a10fb632973651096c8fdba923c932732d09eedc ♻️ Only hand in L10n to l10n filter --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java index ef6ef98..d667811 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java +++ b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java @@ -25,27 +25,21 @@ 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; +import freenet.l10n.BaseL10n; + /** * {@link Filter} implementation replaces {@link String} values with their * translated equivalents. */ public class L10nFilter implements Filter { - /** The web interface. */ - private final WebInterface webInterface; + private final BaseL10n l10n; - /** - * Creates a new L10n filter. - * - * @param webInterface - * The Sone web interface - */ - public L10nFilter(WebInterface webInterface) { - this.webInterface = webInterface; + public L10nFilter(BaseL10n l10n) { + this.l10n = l10n; } /** @@ -56,9 +50,9 @@ public class L10nFilter implements Filter { List parameterValues = getParameters(data, parameters); String text = getText(data); if (parameterValues.isEmpty()) { - return webInterface.getL10n().getString(text); + return l10n.getString(text); } - return new MessageFormat(webInterface.getL10n().getString(text), new Locale(webInterface.getL10n().getSelectedLanguage().shortCode)).format(parameterValues.toArray()); + return new MessageFormat(l10n.getString(text), new Locale(l10n.getSelectedLanguage().shortCode)).format(parameterValues.toArray()); } @Nonnull diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 125f496..e7ea688 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -260,7 +260,7 @@ public class WebInterface implements SessionProvider { private final ElementLoader elementLoader; private final LinkedElementRenderFilter linkedElementRenderFilter; private final TimeTextConverter timeTextConverter = new TimeTextConverter(); - private final L10nFilter l10nFilter = new L10nFilter(this); + private final L10nFilter l10nFilter; /** The “new Sone” notification. */ private final ListNotification newSoneNotification; @@ -320,6 +320,7 @@ public class WebInterface implements SessionProvider { this.elementLoader = elementLoader; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); soneTextParser = new SoneTextParser(getCore(), getCore()); + l10nFilter = new L10nFilter(getL10n()); templateContextFactory = new TemplateContextFactory(); templateContextFactory.addAccessor(Object.class, new ReflectionAccessor()); @@ -337,7 +338,7 @@ public class WebInterface implements SessionProvider { templateContextFactory.addFilter("html", new HtmlFilter()); templateContextFactory.addFilter("replace", new ReplaceFilter()); templateContextFactory.addFilter("store", new StoreFilter()); - templateContextFactory.addFilter("l10n", new L10nFilter(this)); + templateContextFactory.addFilter("l10n", l10nFilter); templateContextFactory.addFilter("substring", new SubstringFilter()); templateContextFactory.addFilter("xml", new XmlFilter()); templateContextFactory.addFilter("change", new RequestChangeFilter()); diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt index fe2cf1a..66b1ab1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt @@ -4,7 +4,6 @@ import freenet.l10n.BaseL10n import freenet.l10n.BaseL10n.LANGUAGE.ENGLISH import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.util.template.TemplateContext import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo @@ -17,18 +16,12 @@ import org.mockito.ArgumentMatchers.anyString */ class L10nFilterTest { - private val webInterface = mock() - private val filter = L10nFilter(webInterface) - private val templateContext = mock() private val l10n = mock() + private val filter = L10nFilter(l10n) + private val templateContext = mock() private val translations = mutableMapOf() @Before - fun setupWebInterface() { - whenever(webInterface.l10n).thenReturn(l10n) - } - - @Before fun setupL10n() { whenever(l10n.selectedLanguage).thenReturn(ENGLISH) whenever(l10n.getString(anyString())).then { translations[it.arguments[0]] }