From: David ‘Bombe’ Roden Date: Mon, 18 Nov 2019 20:17:57 +0000 (+0100) Subject: 🌐 Add Fred-based translation implementation X-Git-Tag: v81^2~40 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e579135d793d42c0422f28ca8de15f0dbef7cb6a;p=Sone.git 🌐 Add Fred-based translation implementation --- diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt new file mode 100644 index 0000000..1b41f70 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt @@ -0,0 +1,16 @@ +package net.pterodactylus.sone.freenet + +import freenet.l10n.* +import java.util.* + +/** + * [Translation] implementation based on Fred’s [BaseL10n]. + */ +class BaseL10nTranslation(private val baseL10n: BaseL10n) : Translation { + + override val currentLocale: Locale + get() = Locale(baseL10n.selectedLanguage.shortCode) + + override fun translate(key: String): String = baseL10n.getString(key) + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt new file mode 100644 index 0000000..a188d47 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt @@ -0,0 +1,30 @@ +package net.pterodactylus.sone.freenet + +import freenet.l10n.* +import net.pterodactylus.sone.test.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import java.util.* + +/** + * Test for [BaseL10nTranslation]. + */ +class BaseL10nTranslationTest { + + private val baseL10n = mock() + private val translation = BaseL10nTranslation(baseL10n) + + @Test + fun `translate method is facade for the correct method`() { + whenever(baseL10n.getString("test")).thenReturn("answer") + assertThat(translation.translate("test"), equalTo("answer")) + } + + @Test + fun `language exposes correct short code`() { + whenever(baseL10n.selectedLanguage).thenReturn(BaseL10n.LANGUAGE.ENGLISH) + assertThat(translation.currentLocale, equalTo(Locale.ENGLISH)) + } + +}