CMP

•

Stop showing enum names to your users: localization in Compose Multiplatform

In the first version of my theme screen the label on each button was Text(mode.name). It worked, and it looked fine in English, but it meant the text users saw was a code identifier. It couldn’t be translated, and renaming the enum constant would silently change the UI.

This post fixes that properly. It’s a small localization setup for Compose Multiplatform: strings in one place, a second language, and a way to use strings from places that aren’t composables, like ViewModels.

What you’ll get

  • String resources shared by Android and iOS

  • A second language with automatic fallback to the default

  • Formatted strings

  • A clean way to label enums without putting text in the domain

  • A UiText helper for strings that come from ViewModels

Full source: Github This builds on the dark mode post and the BaseViewModel post

1. Setup

Compose Multiplatform generates a Res class from a composeResources folder in your shared module. Your project template probably has the dependency already. If not:

// composeApp/build.gradle.kts
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(compose.components.resources)
        }
    }
}

compose.resources {
    packageOfResClass = "com.app.edgekitcmp.resources"   // optional, but keeps imports predictable

// composeApp/build.gradle.kts
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(compose.components.resources)
        }
    }
}

compose.resources {
    packageOfResClass = "com.app.edgekitcmp.resources"   // optional, but keeps imports predictable

// composeApp/build.gradle.kts
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(compose.components.resources)
        }
    }
}

compose.resources {
    packageOfResClass = "com.app.edgekitcmp.resources"   // optional, but keeps imports predictable

2. Your first string

composeApp/src/commonMain/composeResources/values/strings.xml
composeApp/src/commonMain/composeResources/values/strings.xml
composeApp/src/commonMain/composeResources/values/strings.xml
<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Light</string>
    <string name="settings_theme_dark">Dark</string>
</resources>
<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Light</string>
    <string name="settings_theme_dark">Dark</string>
</resources>
<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Light</string>
    <string name="settings_theme_dark">Dark</string>
</resources>

The folder name must be exactly composeResources. Keys use snake_case and each one becomes a property on Res.string. I prefix keys with the feature (settings_...), so the list stays readable as it grows.

Sync Gradle and build once so Res is generated, then:

import com.app.edgekitcmp.resources.Res
import com.app.edgekitcmp.resources.settings_theme_dark
import org.jetbrains.compose.resources.stringResource

Text(stringResource(Res.string.settings_theme_dark

import com.app.edgekitcmp.resources.Res
import com.app.edgekitcmp.resources.settings_theme_dark
import org.jetbrains.compose.resources.stringResource

Text(stringResource(Res.string.settings_theme_dark

import com.app.edgekitcmp.resources.Res
import com.app.edgekitcmp.resources.settings_theme_dark
import org.jetbrains.compose.resources.stringResource

Text(stringResource(Res.string.settings_theme_dark

Let the IDE auto-import the generated names instead of typing them.

3. Add a language

Create a folder next to values with a language qualifier:

<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Ljust</string>
    <string name="settings_theme_dark">Mörkt</string>
</resources>
<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Ljust</string>
    <string name="settings_theme_dark">Mörkt</string>
</resources>
<resources>
    <string name="settings_theme_system">System</string>
    <string name="settings_theme_light">Ljust</string>
    <string name="settings_theme_dark">Mörkt</string>
</resources>

That’s all. The app picks the language from the device, and any key missing from values-sv falls back to values. A half-translated app shows English for the gaps and doesn’t crash.

4. Arguments

Formatting uses positional arguments:

<string name="welcome_user">Welcome back, %1$s</string>
<string name="welcome_user">Welcome back, %1$s</string>
<string name="welcome_user">Welcome back, %1$s</string>
stringResource(Res.string.welcome_user, userName
stringResource(Res.string.welcome_user, userName
stringResource(Res.string.welcome_user, userName

5. Labels for enums

The enum stays free of text, and the label lives in the UI layer:

// ui/settings/ThemeModeLabel.kt
@Composable
fun ThemeMode.label(): String = when (this) {
    ThemeMode.System -> stringResource(Res.string.settings_theme_system)
    ThemeMode.Light -> stringResource(Res.string.settings_theme_light)
    ThemeMode.Dark -> stringResource(Res.string.settings_theme_dark

// ui/settings/ThemeModeLabel.kt
@Composable
fun ThemeMode.label(): String = when (this) {
    ThemeMode.System -> stringResource(Res.string.settings_theme_system)
    ThemeMode.Light -> stringResource(Res.string.settings_theme_light)
    ThemeMode.Dark -> stringResource(Res.string.settings_theme_dark

// ui/settings/ThemeModeLabel.kt
@Composable
fun ThemeMode.label(): String = when (this) {
    ThemeMode.System -> stringResource(Res.string.settings_theme_system)
    ThemeMode.Light -> stringResource(Res.string.settings_theme_light)
    ThemeMode.Dark -> stringResource(Res.string.settings_theme_dark

The when is exhaustive, so adding a new mode breaks the build until the UI has a label for it. That’s the safety net mode.name never gave you.

6. Strings that don’t come from composables

stringResource only works inside @Composable functions. In a ViewModel you have two bad options (a suspend getString call, or passing a context-like object around) and one good one: don’t produce text there.

For events like errors, send a value and let the UI choose the words. That’s what ErrorKind did in the BaseViewModel post. When a ViewModel needs to carry a message that’s sometimes a resource and sometimes plain text, a small wrapper does the job:

sealed interface UiText {
    data class Plain(val value: String) : UiText
    class Resource(val res: StringResource, vararg val args: Any) : UiText
}

@Composable
fun UiText.asString(): String = when (this) {
    is UiText.Plain -> value
    is UiText.Resource -> stringResource(res, *args

sealed interface UiText {
    data class Plain(val value: String) : UiText
    class Resource(val res: StringResource, vararg val args: Any) : UiText
}

@Composable
fun UiText.asString(): String = when (this) {
    is UiText.Plain -> value
    is UiText.Resource -> stringResource(res, *args

sealed interface UiText {
    data class Plain(val value: String) : UiText
    class Resource(val res: StringResource, vararg val args: Any) : UiText
}

@Composable
fun UiText.asString(): String = when (this) {
    is UiText.Plain -> value
    is UiText.Resource -> stringResource(res, *args

// in a ViewModel or event
UiText.Resource(Res.string.welcome_user, name)

// in the UI
Text(uiText.asString

// in a ViewModel or event
UiText.Resource(Res.string.welcome_user, name)

// in the UI
Text(uiText.asString

// in a ViewModel or event
UiText.Resource(Res.string.welcome_user, name)

// in the UI
Text(uiText.asString

The ViewModel stays free of platform and UI types, and the translation happens when the text is drawn, so it follows the current language.

7. Trade-offs and gotchas

  • Resources follow the device language. Both Android 13+ and iOS have a per-app language setting in the system settings, so you may not need a language picker in your app. An in-app picker needs platform-specific work, so treat it as a separate task.

  • UiText is optional. If your ViewModels only emit enums and flags, you don’t need it. Add it when you catch yourself wanting to put a string in state.

  • Translating is only as good as your keys. Put the full sentence in one key. Never build sentences by joining pieces, because word order changes between languages.

  • Special characters. Test strings that contain apostrophes, quotes and % on both platforms before shipping.

  • Missing translations fall back silently. That’s friendly for users, but you won’t notice a gap. A short checklist item before each release (“every new key exists in every language folder”) is enough for a small project.

Wrapping up

The change is small: strings in one folder, a language per folder, labels in the UI layer, and no text in the domain or ViewModels. In return you get a UI that can be translated by adding a file, and enum renames that can’t change what users read.

Next in the series: Language picker that works for both iOS and Android.


Create a free website with Framer, the website builder loved by startups, designers and agencies.