-
Notifications
You must be signed in to change notification settings - Fork 354
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
192 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
18 changes: 18 additions & 0 deletions
18
...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,18 @@ | ||
package net.mullvad.mullvadvpn.broadcastreceiver | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
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) { | ||
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
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
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() { | ||
_currentLocale.value = getLocale().also { Logger.d("New locale: $it") } | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...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,79 @@ | ||
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.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("Updating translations based $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 } | ||
} | ||
} | ||
|
||
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" | ||
} | ||
} |