package net.pterodactylus.sone.freenet
-import freenet.l10n.*
import net.pterodactylus.util.template.*
import java.text.*
-import java.util.*
/**
* [Filter] implementation replaces [String] values with their
* translated equivalents.
*/
-class L10nFilter(private val l10n: BaseL10n) : Filter {
+class L10nFilter(private val translation: Translation) : Filter {
override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map<String, Any>?): String {
val parameterValues = getParameters(data, parameters)
val text = getText(data)
return if (parameterValues.isEmpty()) {
- l10n.getString(text)
+ translation.translate(text)
} else
- MessageFormat(l10n.getString(text), Locale(l10n.selectedLanguage.shortCode)).format(parameterValues.toTypedArray())
+ MessageFormat(translation.translate(text), translation.currentLocale).format(parameterValues.toTypedArray())
}
private fun getText(data: Any?) = (data as? L10nText)?.text ?: data.toString()
package net.pterodactylus.sone.freenet
-import freenet.l10n.*
-import freenet.l10n.BaseL10n.LANGUAGE.*
-import net.pterodactylus.sone.test.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.junit.*
-import org.mockito.ArgumentMatchers.*
+import java.util.*
+import kotlin.collections.*
/**
* Unit test for [L10nFilter].
*/
class L10nFilterTest {
- private val l10n = mock<BaseL10n>()
- private val filter = L10nFilter(l10n)
private val translations = mutableMapOf<String, String>()
-
- @Before
- fun setupL10n() {
- whenever(l10n.selectedLanguage).thenReturn(ENGLISH)
- whenever(l10n.getString(anyString())).then { translations[it.arguments[0]] }
+ private val translation = object : Translation {
+ override val currentLocale = Locale.ENGLISH
+ override fun translate(key: String): String = translations[key] ?: ""
}
+ private val filter = L10nFilter(translation)
@Test
fun `translation without parameters returns translated string`() {