From: David ‘Bombe’ Roden Date: Sun, 6 Jun 2021 09:13:13 +0000 (+0200) Subject: 🐛 Restore ability to change languages on-the-fly X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=642e558da0610f9a605fdef601d0572002577643;p=Sone.git 🐛 Restore ability to change languages on-the-fly 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. --- 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`() {