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.
/**
* [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)
}
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)
class BaseL10nTranslationTest {
private val baseL10n = mock<BaseL10n>()
- private val translation = BaseL10nTranslation(baseL10n)
+ private val translation = BaseL10nTranslation { baseL10n }
@Test
fun `translate method is facade for the correct method`() {