From: David ‘Bombe’ Roden Date: Mon, 18 Nov 2019 20:21:34 +0000 (+0100) Subject: 🌐 Use new translation in l10n filter X-Git-Tag: v81^2~38 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=aa6d42a2b44ef4c0d23c24415f70542603b89f45;p=Sone.git 🌐 Use new translation in l10n filter --- diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt index 4b0d90f..b48bfec 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/L10nFilter.kt @@ -17,24 +17,22 @@ package net.pterodactylus.sone.freenet -import freenet.l10n.* import net.pterodactylus.util.template.* import java.text.* -import java.util.* /** * [Filter] implementation replaces [String] values with their * translated equivalents. */ -class L10nFilter(private val l10n: BaseL10n) : Filter { +class L10nFilter(private val translation: Translation) : Filter { override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map?): String { val parameterValues = getParameters(data, parameters) val text = getText(data) return if (parameterValues.isEmpty()) { - l10n.getString(text) + translation.translate(text) } else - MessageFormat(l10n.getString(text), Locale(l10n.selectedLanguage.shortCode)).format(parameterValues.toTypedArray()) + MessageFormat(translation.translate(text), translation.currentLocale).format(parameterValues.toTypedArray()) } private fun getText(data: Any?) = (data as? L10nText)?.text ?: data.toString() diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt index 7024171..358d106 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt @@ -1,27 +1,22 @@ package net.pterodactylus.sone.freenet -import freenet.l10n.* -import freenet.l10n.BaseL10n.LANGUAGE.* -import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.junit.* -import org.mockito.ArgumentMatchers.* +import java.util.* +import kotlin.collections.* /** * Unit test for [L10nFilter]. */ class L10nFilterTest { - private val l10n = mock() - private val filter = L10nFilter(l10n) private val translations = mutableMapOf() - - @Before - fun setupL10n() { - whenever(l10n.selectedLanguage).thenReturn(ENGLISH) - whenever(l10n.getString(anyString())).then { translations[it.arguments[0]] } + private val translation = object : Translation { + override val currentLocale = Locale.ENGLISH + override fun translate(key: String): String = translations[key] ?: "" } + private val filter = L10nFilter(translation) @Test fun `translation without parameters returns translated string`() {