My language switch worked in the code but not on screen. The setting changed, the locale was applied, every function was called with the right arguments, and the UI stayed in English. The cause was timing, not logic, and it took the platform’s own behavior to explain it.
This post builds an in-app language picker for Compose Multiplatform: English, Swedish, or follow the system. It’s stored in the same settings object as the theme, applied at the app root, and works on Android and iOS. It also covers what it costs, because the platform work is where the surprises are.
What you’ll get
A persisted AppLanguage setting with a “follow the system” option
A small expect/actual that applies the language on Android and iOS
The bug that made the first Android version do nothing, and the fix
A picker that shows each language in its own name
A list of the gotchas I ran into
Full source: Github This builds on the localization post and dark mode post
1. The design
Compose Multiplatform follows the device language by default. A picker overrides that, which means three things:
Store the choice where the app can read it at startup.
Apply it before any string is read.
Rebuild the UI when it changes.
For the first, I didn’t add anything new. The theme already lives in a SettingsItem saved as one JSON blob, and a language is the same kind of data: a user preference that has to persist. Adding it means two models and a mapper line. The repository, the data source and the mutex don’t change.
2. The models
enum class AppLanguage(val tag: String?) {
System(""),
English("en"),
Swedish("sv"),
}
data class SettingsItem(
val themeMode: ThemeMode = ThemeMode.System,
val appLanguage: AppLanguage = AppLanguage
enum class AppLanguage(val tag: String?) {
System(""),
English("en"),
Swedish("sv"),
}
data class SettingsItem(
val themeMode: ThemeMode = ThemeMode.System,
val appLanguage: AppLanguage = AppLanguage
enum class AppLanguage(val tag: String?) {
System(""),
English("en"),
Swedish("sv"),
}
data class SettingsItem(
val themeMode: ThemeMode = ThemeMode.System,
val appLanguage: AppLanguage = AppLanguage
System has a "" tag, which means “don’t override anything”. The tag is a language code, so it’s data and not UI text, which is why it can live in the domain.
@Serializable
data class SettingsLocalItem(
val themeMode: String = ThemeMode.System.name,
val appLanguage: String = AppLanguage.System.name,
)
fun SettingsItem.asLocalModel() = SettingsLocalItem(
themeMode = themeMode.name,
appLanguage = appLanguage.name,
)
fun SettingsLocalItem.asDomainModel() = SettingsItem(
themeMode = ThemeMode.entries.firstOrNull { it.name == themeMode } ?: ThemeMode.System,
appLanguage = AppLanguage.entries.firstOrNull { it.name == appLanguage } ?: AppLanguage
@Serializable
data class SettingsLocalItem(
val themeMode: String = ThemeMode.System.name,
val appLanguage: String = AppLanguage.System.name,
)
fun SettingsItem.asLocalModel() = SettingsLocalItem(
themeMode = themeMode.name,
appLanguage = appLanguage.name,
)
fun SettingsLocalItem.asDomainModel() = SettingsItem(
themeMode = ThemeMode.entries.firstOrNull { it.name == themeMode } ?: ThemeMode.System,
appLanguage = AppLanguage.entries.firstOrNull { it.name == appLanguage } ?: AppLanguage
@Serializable
data class SettingsLocalItem(
val themeMode: String = ThemeMode.System.name,
val appLanguage: String = AppLanguage.System.name,
)
fun SettingsItem.asLocalModel() = SettingsLocalItem(
themeMode = themeMode.name,
appLanguage = appLanguage.name,
)
fun SettingsLocalItem.asDomainModel() = SettingsItem(
themeMode = ThemeMode.entries.firstOrNull { it.name == themeMode } ?: ThemeMode.System,
appLanguage = AppLanguage.entries.firstOrNull { it.name == appLanguage } ?: AppLanguage
This is where the single JSON blob from the first post pays off. Users who already have the app have stored JSON without an appLanguage field, and it still decodes, because missing fields fall back to their defaults. No migration is needed.
3. Applying the language
Strings in Compose Multiplatform are resolved from the device locale, so there has to be a place where the app can override it. I use a small expect in core/ui/locale:
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
expect object LocalAppLocale {
@Composable
infix fun provides(value: String?): ProvidedValue
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
expect object LocalAppLocale {
@Composable
infix fun provides(value: String?): ProvidedValue
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
expect object LocalAppLocale {
@Composable
infix fun provides(value: String?): ProvidedValue
It goes in core because it knows nothing about any feature. The only place that connects settings to it is the app root, next to the theme:
CompositionLocalProvider(LocalAppLocale provides state.appLanguage.tag) {
key(state.appLanguage.tag) {
AppTheme(darkTheme) {
NavigationComponent(navHostController
CompositionLocalProvider(LocalAppLocale provides state.appLanguage.tag) {
key(state.appLanguage.tag) {
AppTheme(darkTheme) {
NavigationComponent(navHostController
CompositionLocalProvider(LocalAppLocale provides state.appLanguage.tag) {
key(state.appLanguage.tag) {
AppTheme(darkTheme) {
NavigationComponent(navHostController
The key rebuilds the UI when the language changes, so every string is read again. The nav controller is created outside it, which matters later.
4. Android: the version that did nothing
Android has an official API for per-app languages, AppCompatDelegate.setApplicationLocales. It recreates the activity, uses the system’s per-app language feature on Android 13 and up, and is backported to older versions. So my first version used it:
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
SideEffect {
val wanted = value?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides LocalConfiguration.current
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
SideEffect {
val wanted = value?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides LocalConfiguration.current
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
SideEffect {
val wanted = value?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides LocalConfiguration.current
Every line ran, with the correct arguments. The strings stayed in English.
The cause, as best I can tell: order of events. When the language state changes, the key rebuilds the UI right away, and the strings are resolved using the locale that’s still active. The SideEffect runs after that composition, and the system applies the new configuration later still. Compose Multiplatform builds its resource environment (which includes the language) once and remembers it, so when the new locale finally arrives, nothing makes the strings resolve again.
The fix is to set the default locale during composition, before the content below reads any string, and still tell AppCompat afterwards for persistence and for Android’s own resources:
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val configuration = LocalConfiguration.current
val wanted = value
?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
Locale.setDefault(wanted.get(0) ?: Resources.getSystem().configuration.locales[0])
SideEffect {
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides configuration
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val configuration = LocalConfiguration.current
val wanted = value
?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
Locale.setDefault(wanted.get(0) ?: Resources.getSystem().configuration.locales[0])
SideEffect {
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides configuration
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val configuration = LocalConfiguration.current
val wanted = value
?.let(LocaleListCompat::forLanguageTags)
?: LocaleListCompat.getEmptyLocaleList()
Locale.setDefault(wanted.get(0) ?: Resources.getSystem().configuration.locales[0])
SideEffect {
if (AppCompatDelegate.getApplicationLocales().toLanguageTags() != wanted.toLanguageTags()) {
AppCompatDelegate.setApplicationLocales(wanted)
}
}
return LocalConfiguration provides configuration
Resources.getSystem() gives the real system language, which is what “System” should go back to. Setting a default locale while composing is a side effect, but it’s idempotent, and the order it enforces is the whole point.
5. iOS
iOS has no activity to recreate. The per-app language is read from the AppleLanguages user default, so the implementation writes it:
actual object LocalAppLocale {
private val LocalLocale = staticCompositionLocalOf { NSLocale.currentLocale.localeIdentifier }
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val defaults = NSUserDefaults.standardUserDefaults
if (value == null) {
defaults.removeObjectForKey("AppleLanguages")
} else {
defaults.setObject(listOf(value), forKey = "AppleLanguages")
}
return LocalLocale provides (value ?: NSLocale.currentLocale.localeIdentifier
actual object LocalAppLocale {
private val LocalLocale = staticCompositionLocalOf { NSLocale.currentLocale.localeIdentifier }
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val defaults = NSUserDefaults.standardUserDefaults
if (value == null) {
defaults.removeObjectForKey("AppleLanguages")
} else {
defaults.setObject(listOf(value), forKey = "AppleLanguages")
}
return LocalLocale provides (value ?: NSLocale.currentLocale.localeIdentifier
actual object LocalAppLocale {
private val LocalLocale = staticCompositionLocalOf { NSLocale.currentLocale.localeIdentifier }
@Composable
actual infix fun provides(value: String?): ProvidedValue<*> {
val defaults = NSUserDefaults.standardUserDefaults
if (value == null) {
defaults.removeObjectForKey("AppleLanguages")
} else {
defaults.setObject(listOf(value), forKey = "AppleLanguages")
}
return LocalLocale provides (value ?: NSLocale.currentLocale.localeIdentifier
The composition local isn’t there to carry the locale. It exists so that a change makes the subtree recompose.
6. The picker
Language names are the one thing you shouldn’t translate. Each language is shown in its own name, so someone on a phone set to the wrong language can still find “Svenska”. Only “System” goes through resources:
@Composable
fun AppLanguage.label(): String = when (this) {
AppLanguage.System -> stringResource(Res.string.settings_language_system)
AppLanguage.English -> "English"
AppLanguage.Swedish -> "Svenska"
@Composable
fun AppLanguage.label(): String = when (this) {
AppLanguage.System -> stringResource(Res.string.settings_language_system)
AppLanguage.English -> "English"
AppLanguage.Swedish -> "Svenska"
@Composable
fun AppLanguage.label(): String = when (this) {
AppLanguage.System -> stringResource(Res.string.settings_language_system)
AppLanguage.English -> "English"
AppLanguage.Swedish -> "Svenska"
There is many ways to display the picker. I chose a regular column that can grow vertically if we add more languages later:
Column(
verticalArrangement = Arrangement.spacedBy(AppTheme.spacings.xxs)
) {
AppLanguage.entries.forEach { language ->
Row(
modifier = Modifier
.clickable(
enabled = true
) {
onChangeLanguage(language)
}
.background(MaterialTheme.colorScheme.surfaceContainer)
.padding(AppTheme.spacings.m),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier.weight(1f),
text = language.label(),
style = MaterialTheme.typography.bodyLarge
)
if (appLanguage == language) {
Icon(PhosphorIcons.Regular.Check, null
Column(
verticalArrangement = Arrangement.spacedBy(AppTheme.spacings.xxs)
) {
AppLanguage.entries.forEach { language ->
Row(
modifier = Modifier
.clickable(
enabled = true
) {
onChangeLanguage(language)
}
.background(MaterialTheme.colorScheme.surfaceContainer)
.padding(AppTheme.spacings.m),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier.weight(1f),
text = language.label(),
style = MaterialTheme.typography.bodyLarge
)
if (appLanguage == language) {
Icon(PhosphorIcons.Regular.Check, null
Column(
verticalArrangement = Arrangement.spacedBy(AppTheme.spacings.xxs)
) {
AppLanguage.entries.forEach { language ->
Row(
modifier = Modifier
.clickable(
enabled = true
) {
onChangeLanguage(language)
}
.background(MaterialTheme.colorScheme.surfaceContainer)
.padding(AppTheme.spacings.m),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier.weight(1f),
text = language.label(),
style = MaterialTheme.typography.bodyLarge
)
if (appLanguage == language) {
Icon(PhosphorIcons.Regular.Check, null
The ViewModel is the same pattern as the theme:
fun onChangeLanguage(language: AppLanguage) = launch {
settingsRepository.update { it.copy(appLanguage = language
fun onChangeLanguage(language: AppLanguage) = launch {
settingsRepository.update { it.copy(appLanguage = language
fun onChangeLanguage(language: AppLanguage) = launch {
settingsRepository.update { it.copy(appLanguage = language
7. Test the part that breaks
@Test
fun `old stored JSON without a language decodes to System`() {
val storage = MapSettings().apply { putString("settings", """{"themeMode":"Dark"}""") }
val item = SettingsLocalDataSourceImpl(storage).settingsItem.value
assertEquals(ThemeMode.Dark, item.themeMode)
assertEquals(AppLanguage.System, item.appLanguage)
}
@Test
fun `unknown stored language falls back to System`() {
val storage = MapSettings().apply { putString("settings", """{"appLanguage":"Klingon"}""") }
assertEquals(AppLanguage.System, SettingsLocalDataSourceImpl(storage).settingsItem.value.appLanguage
@Test
fun `old stored JSON without a language decodes to System`() {
val storage = MapSettings().apply { putString("settings", """{"themeMode":"Dark"}""") }
val item = SettingsLocalDataSourceImpl(storage).settingsItem.value
assertEquals(ThemeMode.Dark, item.themeMode)
assertEquals(AppLanguage.System, item.appLanguage)
}
@Test
fun `unknown stored language falls back to System`() {
val storage = MapSettings().apply { putString("settings", """{"appLanguage":"Klingon"}""") }
assertEquals(AppLanguage.System, SettingsLocalDataSourceImpl(storage).settingsItem.value.appLanguage
@Test
fun `old stored JSON without a language decodes to System`() {
val storage = MapSettings().apply { putString("settings", """{"themeMode":"Dark"}""") }
val item = SettingsLocalDataSourceImpl(storage).settingsItem.value
assertEquals(ThemeMode.Dark, item.themeMode)
assertEquals(AppLanguage.System, item.appLanguage)
}
@Test
fun `unknown stored language falls back to System`() {
val storage = MapSettings().apply { putString("settings", """{"appLanguage":"Klingon"}""") }
assertEquals(AppLanguage.System, SettingsLocalDataSourceImpl(storage).settingsItem.value.appLanguage
The first one is the migration test. It proves that adding a setting doesn’t break users who already have stored data.
8. What it costs
AppCompatActivity below Android 13. The Compose Multiplatform template uses ComponentActivity. On Android 12 and older, setApplicationLocales only takes effect with an AppCompatActivity, which also needs an AppCompat theme. On Android 13 and up it works either way.
Two sources of truth. The system remembers the language, and so does your setting. If the user changes it in the system settings, your stored value is stale. I treat my setting as the truth and apply it at startup, which is simple but not perfect.
The back stack. Because the nav controller is created outside the key, the back stack survives a language change. If you create it inside the key, the user is thrown back to the start screen.
You might not need a picker. Android 13+ and iOS both have a per-app language setting. Many production apps just use that. A picker is still a reasonable feature, especially for users who don’t know the system option exists.
Wrapping up
Adding the setting was easy because the settings layer was built to grow. The language switch needed a small platform layer, and most of the work was in the order things happen. If you remember one thing: when a platform applies a locale asynchronously, make sure your strings aren’t read until it has.
