-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...rc/main/kotlin/net/mullvad/mullvadvpn/broadcastreceiver/LocaleChangedBroadcastReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.mullvad.mullvadvpn.broadcastreceiver | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import co.touchlab.kermit.Logger | ||
import net.mullvad.mullvadvpn.lib.shared.LocaleRepository | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class LocaleChangedBroadcastReceiver : BroadcastReceiver(), KoinComponent { | ||
private val localeRepository by inject<LocaleRepository>() | ||
|
||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (intent?.action == Intent.ACTION_LOCALE_CHANGED) { | ||
Logger.d("AppLang: New App Language") | ||
localeRepository.refreshLocale() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/LocaleRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.mullvad.mullvadvpn.lib.shared | ||
|
||
import android.content.res.Resources | ||
import co.touchlab.kermit.Logger | ||
import java.util.Locale | ||
import kotlin.also | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
class LocaleRepository(val resources: Resources) { | ||
private val _currentLocale = MutableStateFlow(getLocale()) | ||
val currentLocale: StateFlow<Locale?> = _currentLocale | ||
|
||
private fun getLocale(): Locale? = resources.configuration.locales.get(0) | ||
|
||
fun refreshLocale() { | ||
Logger.d("AppLang: Refreshing locale") | ||
_currentLocale.value = getLocale().also { Logger.d("AppLang: New locale: $it") } | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...d/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.mullvad.mullvadvpn.lib.shared | ||
|
||
import android.content.Context | ||
import android.content.res.Configuration | ||
import android.content.res.XmlResourceParser | ||
import co.touchlab.kermit.Logger | ||
import java.util.Locale | ||
import kotlin.also | ||
import kotlin.collections.associate | ||
import kotlin.collections.set | ||
import kotlin.collections.toMap | ||
import kotlin.to | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.withContext | ||
|
||
typealias Translations = Map<String, String> | ||
|
||
class RelayLocationTranslationRepository( | ||
val context: Context, | ||
val localeRepository: LocaleRepository, | ||
externalScope: CoroutineScope = MainScope(), | ||
val dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) { | ||
val translations: StateFlow<Translations> = | ||
localeRepository.currentLocale | ||
.map { loadTranslations(it) } | ||
.stateIn(externalScope, SharingStarted.Eagerly, emptyMap()) | ||
|
||
private val defaultTranslation: Map<String, String> | ||
|
||
init { | ||
val defaultConfiguration = defaultConfiguration() | ||
val confContext = context.createConfigurationContext(defaultConfiguration) | ||
val defaultTranslationXml = confContext.resources.getXml(R.xml.relay_locations) | ||
defaultTranslation = loadRelayTranslation(defaultTranslationXml) | ||
} | ||
|
||
private suspend fun loadTranslations(locale: Locale?): Translations = | ||
withContext(dispatcher) { | ||
Logger.d( | ||
"AppLang ${this@RelayLocationTranslationRepository}: Updating based on current locale to $locale" | ||
) | ||
if (locale == null || locale.language == DEFAULT_LANGUAGE) emptyMap() | ||
else { | ||
// Load current translations | ||
val xml = context.resources.getXml(R.xml.relay_locations) | ||
val translation = loadRelayTranslation(xml) | ||
|
||
translation.entries | ||
.associate { (id, name) -> defaultTranslation[id]!! to name } | ||
.also { Logger.d("AppLang: New translationTable: $it") } | ||
} | ||
} | ||
|
||
private fun loadRelayTranslation(xml: XmlResourceParser): Map<String, String> { | ||
val translation = mutableMapOf<String, String>() | ||
while (xml.eventType != XmlResourceParser.END_DOCUMENT) { | ||
if (xml.eventType == XmlResourceParser.START_TAG && xml.name == "string") { | ||
val key = xml.getAttributeValue(null, "name") | ||
val value = xml.nextText() | ||
translation[key] = value | ||
} | ||
xml.next() | ||
} | ||
return translation.toMap() | ||
} | ||
|
||
private fun defaultConfiguration(): Configuration { | ||
val configuration = context.resources.configuration | ||
configuration.setLocale(Locale(DEFAULT_LANGUAGE)) | ||
return configuration | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_LANGUAGE = "en" | ||
} | ||
} |