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

Stylianos Gakis

01/17/2023, 9:18 AM
There doesn’t seem to exist a CompositionLocal for
Locale
in compose right? I can see Locale is there, which only gives you a way to fetch the locale, not to update it, and looking at the implementation it doesn’t seem to use the
AppCompatDelegate.setApplicationLocales
aka the Per-App Language Preferences API. Oh and also it doesn’t give a java.util.Locale so there’s that. Am I missing something here? How do you folks typically fetch the proper locale in your composables aside from just passing it down the chain all the way through?
e

ephemient

01/17/2023, 9:31 AM
Copy code
LocalContext.current.resources.configuration.locales
although that doesn't really help if you want to change it or react to changes
a

Albert Chang

01/17/2023, 9:32 AM
You should use
LocalConfiguration.current.locales
.
And by using that you can subscribe to configuration changes.
s

Stylianos Gakis

01/17/2023, 9:41 AM
Right, so I should use
LocalConfiguration.current.locales
instead of LocalContext… right? Not in addition to it I assume. Also with that said, I assume both of these do not interact with the per-app language APIs, do they? Like if I use those APIs to manage my locale, do you know if that will update the locales inside LocalConfiguration?
Oh uhmm, lint says
LocalConfiguration.current.locales
requires API 24 or newer though 😵 Is this fixable in some way, with
coreLibraryDesugaring
or something like that? If not I guess I need to make my own LocalLocale and make sure I am also subscribing to
LocalConfiguration.current
myself to react to such config changes.
Okay this was silly by me, apparently
.locale
works fine but is deprecated over
.locales
which is >API24 only. Anyway, gonna use the deprecated one then.
a

Albert Chang

01/17/2023, 10:15 AM
ConfigurationCompat.getLocales(LocalConfiguration.current)
And I believe per-app language API should also update the configuration.
s

Stylianos Gakis

01/17/2023, 10:19 AM
Quite a verbose thing to write, but yeah the solution sounds like it will be this
Copy code
val locale = ConfigurationCompat.getLocales(LocalConfiguration.current)[0]
This seems to cover all Android versions and I will just double check that it follows the per-app language APIs too.
209 Views