Abdalla Hassanin
05/29/2021, 9:58 AMZun
05/29/2021, 10:07 AMTin Tran
05/29/2021, 10:22 AMAbdalla Hassanin
05/29/2021, 10:27 AMAlbert Chang
05/29/2021, 10:58 AMLocalConfiguration.current.locales
.Abdalla Hassanin
05/29/2021, 11:31 AMAlbert Chang
05/29/2021, 11:57 AM// Strings.kt
private val EnUs = mapOf(
"hello" to "Hello"
)
private val FrFr = mapOf(
"hello" to "Bonjour"
)
@Composable
fun string(key: String): String {
val map = when(LocalConfiguration.current.locales.get(0)) {
Locale.forLanguageTag("fr-FR") -> FrFr,
else -> EnUs
}
return map.getValue(key)
}
// UI code
@Composable
fun Greeting() {
Text(text = string("hello"))
}
Note that I’ve simplified many things. You shouldn’t just use the first locale in the locale list, and locale matching is much more complicated than this. Also you probably want to cache the map for current locale as locale matching is rather expensive.Chachako
05/29/2021, 12:30 PMAbdalla Hassanin
05/29/2021, 12:48 PMAlbert Chang
05/29/2021, 1:11 PMLocale.current
won't trigger recomposition on locale change.Chachako
05/29/2021, 2:00 PMChachako
05/29/2021, 2:05 PMAlbert Chang
05/29/2021, 5:02 PMLocalConfiguration
as it is monitored by the runtime. It's a waste to recompose the whole tree when you can only recompose part of it. The whole point of compose compiler plugin is to enable compose runtime to only recompose what needs to be recomposed. And anyway you need to notify the runtime when locale changes.