how to change the locale in the middle of an compo...
# compose-android
v
how to change the locale in the middle of an compose ui test. I tried with the below approach. But it doesn't work
fun setLocale(locale: Locale) {
val resources = InstrumentationRegistry.getInstrumentation().targetContext.resources
val config = resources.configuration config.setLocale(locale)
resources.updateConfiguration(config, resources.displayMetrics)
}
and the test is
@Test
fun  testChangeLocaleInMiddle() {
composeTestRule.onNodeWithText("Hello").assertExists()
// Change the locale to Tamil
setLocale(Locale("ta"))
// Recompose the UI
composeTestRule.activityRule.scenario.onActivity { activity ->
activity.recreate()
}
// Allow time for the activity to recreate
Thread.sleep(1000)
// Perform actions in the new locale
composeTestRule.onNodeWithText("வணக்கம்").assertExists()
}
m
Use
DeviceConfigurationOverride
API, specifically
Locales
fun https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]ride&ss=androidx%2Fplatform%2Fframeworks%2Fsupport:compose%2F if you're on Compose 1.7. you can see this API in action in Now in android Or use Accompanist testharness in earlier compose versions