https://kotlinlang.org logo
#compose
Title
# compose
b

brabo-hi

02/12/2022, 9:37 PM
Hi all, how could we change the configuration locale ?
Copy code
val configuration = LocalConfiguration.current
configuration.setLocale(locale)
Doesn’t look to have any effect unless we add
resources.updateConfiguration(configuration, resources.displayMetrics)
but this method looks deprecated
n

Nathaniel Rowe

02/12/2022, 10:14 PM
So, LocalConfiguration is a composition local. It's only intended for reading values that are supplied higher up in the composition hierarchy. What you can write is this:
Copy code
val configuration = LocalConfiguration.current
    configuration.setLocale(locale)
    CompositionLocalProvider(LocalConfiguration provides configuration) {
        // everything here "sees" the updated configuration
    }
This will only use the updated configuration for composables that are in that composition local provider. Everything outside will still see the normal configuration. I think that method is deprecated because you really shouldn't be setting the configuration app-wide. If you want that updated locale for all of your stuff, then just make sure to include that composition local provider super high up in your composition hierarchy, so that everything in your app is "underneath" it.
Actually, you probably wanna do something like
val updatedConfiguration = remember { configuration.setLocale(locale) }
. Sorry, I'm not super good with the whole "remember" concept.
b

brabo-hi

02/13/2022, 1:53 AM
Copy code
val configuration = LocalConfiguration.current
            configuration.setLocale(Locale.ITALIAN)
            CompositionLocalProvider(LocalConfiguration provides configuration) {
Text(text = stringResource(id = R.string.label_phone))
}
this is what i have, but the language is still the default one
what am i missing
u

Utkarsh Tiwari

02/09/2023, 8:34 AM
I had the same requirement and I followed what Nathaniel suggested and it seems to work for me.
2 Views