|
| 1 | +package net.mullvad.mullvadvpn.lib.shared |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.res.Configuration |
| 5 | +import android.content.res.XmlResourceParser |
| 6 | +import co.touchlab.kermit.Logger |
| 7 | +import java.util.Locale |
| 8 | +import kotlin.collections.associate |
| 9 | +import kotlin.collections.set |
| 10 | +import kotlin.collections.toMap |
| 11 | +import kotlin.to |
| 12 | +import kotlinx.coroutines.CoroutineDispatcher |
| 13 | +import kotlinx.coroutines.CoroutineScope |
| 14 | +import kotlinx.coroutines.Dispatchers |
| 15 | +import kotlinx.coroutines.MainScope |
| 16 | +import kotlinx.coroutines.flow.SharingStarted |
| 17 | +import kotlinx.coroutines.flow.StateFlow |
| 18 | +import kotlinx.coroutines.flow.map |
| 19 | +import kotlinx.coroutines.flow.stateIn |
| 20 | +import kotlinx.coroutines.withContext |
| 21 | + |
| 22 | +typealias Translations = Map<String, String> |
| 23 | + |
| 24 | +class RelayLocationTranslationRepository( |
| 25 | + val context: Context, |
| 26 | + val localeRepository: LocaleRepository, |
| 27 | + externalScope: CoroutineScope = MainScope(), |
| 28 | + val dispatcher: CoroutineDispatcher = Dispatchers.IO |
| 29 | +) { |
| 30 | + val translations: StateFlow<Translations> = |
| 31 | + localeRepository.currentLocale |
| 32 | + .map { loadTranslations(it) } |
| 33 | + .stateIn(externalScope, SharingStarted.Eagerly, emptyMap()) |
| 34 | + |
| 35 | + private val defaultTranslation: Map<String, String> |
| 36 | + |
| 37 | + init { |
| 38 | + val defaultConfiguration = defaultConfiguration() |
| 39 | + val confContext = context.createConfigurationContext(defaultConfiguration) |
| 40 | + val defaultTranslationXml = confContext.resources.getXml(R.xml.relay_locations) |
| 41 | + defaultTranslation = loadRelayTranslation(defaultTranslationXml) |
| 42 | + } |
| 43 | + |
| 44 | + private suspend fun loadTranslations(locale: Locale?): Translations = |
| 45 | + withContext(dispatcher) { |
| 46 | + Logger.d("Updating translations based $locale") |
| 47 | + if (locale == null || locale.language == DEFAULT_LANGUAGE) emptyMap() |
| 48 | + else { |
| 49 | + // Load current translations |
| 50 | + val xml = context.resources.getXml(R.xml.relay_locations) |
| 51 | + val translation = loadRelayTranslation(xml) |
| 52 | + |
| 53 | + translation.entries.associate { (id, name) -> defaultTranslation[id]!! to name } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private fun loadRelayTranslation(xml: XmlResourceParser): Map<String, String> { |
| 58 | + val translation = mutableMapOf<String, String>() |
| 59 | + while (xml.eventType != XmlResourceParser.END_DOCUMENT) { |
| 60 | + if (xml.eventType == XmlResourceParser.START_TAG && xml.name == "string") { |
| 61 | + val key = xml.getAttributeValue(null, "name") |
| 62 | + val value = xml.nextText() |
| 63 | + translation[key] = value |
| 64 | + } |
| 65 | + xml.next() |
| 66 | + } |
| 67 | + return translation.toMap() |
| 68 | + } |
| 69 | + |
| 70 | + private fun defaultConfiguration(): Configuration { |
| 71 | + val configuration = context.resources.configuration |
| 72 | + configuration.setLocale(Locale(DEFAULT_LANGUAGE)) |
| 73 | + return configuration |
| 74 | + } |
| 75 | + |
| 76 | + companion object { |
| 77 | + private const val DEFAULT_LANGUAGE = "en" |
| 78 | + } |
| 79 | +} |
0 commit comments