From 642e558da0610f9a605fdef601d0572002577643 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 6 Jun 2021 11:13:13 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Restore=20ability=20to=20change?= =?utf8?q?=20languages=20on-the-fly?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Hieronymus (sone://-wFZ6ezwU3QgvM1u8~uiLbtjxSQ19tuqn4Q94lvTrwE) noticed that changing the language in Fred’s web interface didn’t change Sone’s language anymore. Part of my (ongoing) restructuring of Sone’s source code is abstracting away all the parts that are hard to test, i.e. every interaction with Freenet itself and the translation mechanisms are a part of that. So when I introduced a Translation object I completely forgot that it’s possible to change the language during runtime. Luckily that is easily solvable using a Supplier instead of the concrete BaseL10n object. In theory that does have a performance impact (because now there are at least two more function calls involved per translation) but I am quite confident that the additional time actually used by this is far below the 1-ms range so I am not worried about it. --- .../kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt | 6 +++--- src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt | 2 +- .../net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt index 1b41f70..099f2c6 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslation.kt @@ -6,11 +6,11 @@ import java.util.* /** * [Translation] implementation based on Fred’s [BaseL10n]. */ -class BaseL10nTranslation(private val baseL10n: BaseL10n) : Translation { +class BaseL10nTranslation(private val baseL10nSupplier: () -> BaseL10n) : Translation { override val currentLocale: Locale - get() = Locale(baseL10n.selectedLanguage.shortCode) + get() = Locale(baseL10nSupplier().selectedLanguage.shortCode) - override fun translate(key: String): String = baseL10n.getString(key) + override fun translate(key: String): String = baseL10nSupplier().getString(key) } diff --git a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt index 212751a..a4533c5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt @@ -58,7 +58,7 @@ open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: bind(PluginYear::class.java).toInstance(PluginYear(sonePlugin.year)) bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage)) bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java) - bind(Translation::class.java).toInstance(BaseL10nTranslation(sonePlugin.l10n().base)) + bind(Translation::class.java).toInstance(BaseL10nTranslation { sonePlugin.l10n().base }) loaders?.let { bind(Loaders::class.java).toInstance(it) } bind(MetricRegistry::class.java).`in`(Singleton::class.java) bind(WebOfTrustConnector::class.java).to(PluginWebOfTrustConnector::class.java).`in`(Singleton::class.java) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt index a188d47..def48e3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/BaseL10nTranslationTest.kt @@ -13,7 +13,7 @@ import java.util.* class BaseL10nTranslationTest { private val baseL10n = mock() - private val translation = BaseL10nTranslation(baseL10n) + private val translation = BaseL10nTranslation { baseL10n } @Test fun `translate method is facade for the correct method`() { -- 2.7.4