Hi folks, I was wondering when should I use `andro...
# compose-android
a
Hi folks, I was wondering when should I use
androidx.compose.ui.text.intl.Locale
over
java.util.Locale
/
androidx.core.os.LocaleListCompat
. Best I can figure is that Compose Locale would be useful in multiplatform projects?
s
Not sure when intl.Locale is the right choice. I do know for sure that going with java.util.Locale and doing something like this https://stackoverflow.com/a/75172481/9440211 also gives you the correct locale when using the per-app language APIs which maybe intl.Locale does as well but I have never checked, and in general most of the other functions that will accept a Locale will expect it to be java.util.Locale. Have you used the intl one yourself yet? Has it been working for you well?
a
At the moment I'm using the per-app language API with fallbacks:
AppCompatDelegate.getApplicationLocales()[0] ?: LocaleListCompat.getDefault()[0] ?: Locale.getDefault()
, because I need it in non-Compose parts of the app too. intl.Locale feels easy to use, because it's just one call:
Locale.current
. It works, but you can't pass it into platform APIs or do anything else with it. It wraps
java.util.Locale
in (internal)
AndroidLocale
, neither of which can be accessed directly. I was wrong, there is an extension function
toJavaLocale()
. I initially went down this route because I thought
current
might mean it's a composable, so it would react to changes or can be "provided", but no. It's just a static getter.
s
Oh yeah it really is just a getter. So you’re far better off with just using this instead https://github.com/HedvigInsurance/android/blob/7e85f0db5781027e32700587c32c517454[…]core-ui/src/main/kotlin/com/hedvig/android/core/ui/GetLocale.kt This does react to config changes as it should
👍 1