🌐 Add Fred-based translation implementation
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 18 Nov 2019 20:17:57 +0000 (21:17 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 18 Nov 2019 20:17:57 +0000 (21:17 +0100)
src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt [new file with mode: 0644]

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 (file)
index 0000000..1b41f70
--- /dev/null
@@ -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 (file)
index 0000000..a188d47
--- /dev/null
@@ -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<BaseL10n>()
+       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))
+       }
+
+}