Usage (Compose Module)
This guide shows you how to use the Tolgee Android SDK Compose module in Jetpack Compose and Compose Multiplatform applications. For traditional Android Views usage, see Android Views Usage.
Prerequisites
Make sure you've completed the quickstart setup and initialized Tolgee for Compose. See: Installation (Compose Module)
Basic Usage
Tolgee's Compose extensions let you fetch localized keys directly inside your composables. Unlike traditional Android Views which require manual UI updates or Activity recreation, Compose integration automatically recomposes UI elements when translations change.
If you are unsure of which translation type to choose, notice a “Use for...” section under every explanation.
Getting translations
Simple Strings
- Jetpack Compose
@Composable
fun SimpleText() {
Text(text = stringResource(R.string.welcome_message))
}
- Compose Multiplatform
@Composable
fun SimpleText() {
Text(text = stringResource(Res.string.welcome_message))
}
In the Compose Multiplatform use stringResource function with Res.
Use stringResource for simple strings (e. g., error messages, titles), retrieve a translation by its resource ID.
With Parameters
Pass parameters to translations, supply arguments for placeholders in your keys.
- Jetpack Compose
@Composable
fun TextWithParameters(name: String) {
Text(text = stringResource(R.string.welcome_user, name))
}
- Compose Multiplatform
@Composable
fun TextWithParameters(name: String) {
Text(text = stringResource(Res.string.welcome_user, name))
}
Use for translation with dynamic values. Add data that changes during the app's runtime (e.g., dates, numbers, usernames).
Plurals
Handle plurals with pluralStringResource - provide the count and any formatting arguments.
- Jetpack Compose
@Composable
fun PluralText(count: Int) {
// First 'count' parameter determines which plural form to use
// Second 'count' parameter is passed as a value to the translation
Text(text = pluralStringResource(R.plurals.items_count, count, count))
}
- Compose Multiplatform
@Composable
fun PluralText(count: Int) {
Text(text = pluralStringResource(Res.plurals.items_count, count, count))
}
Example plural resource:
<!-- In res/values/plurals.xml -->
<plurals name="items_count">
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
</plurals>
The pluralStringResource function will:
- Select the correct plural form based on the count value
- Replace the placeholder with the provided value
- Automatically update the UI if the count changes or if translations are updated
Use for lists, counters, notifications — any situation where the grammar changes with quantity.
Explicit Tolgee Instance
@Composable
fun ExplicitInstance() {
val tolgee = remember { myCustomTolgee }
Text(text = stringResource(tolgee, R.string.welcome_message))
}
Handy when you need more than one instance (multiple projects/CDNs), when following dependency injection patterns (dependencies are “injected”, not created inside classes), and or during testing/previews.
Locale Switching
You can create a locale switcher component:
@Composable
fun LocaleSwitcher() {
val tolgee = Tolgee.instance
val currentLocale = tolgee.changeFlow.mapLatest {
tolgee.getLocale()
}.collectAsState(initial = tolgee.getLocale())
Row {
Text(text = stringResource(tolgee, R.string.selected_locale, currentLocale.language))
Button(onClick = { tolgee.setLocale("en") }) {
Text("English")
}
Button(onClick = { tolgee.setLocale("fr") }) {
Text("Français")
}
Button(onClick = { tolgee.setLocale("cs") }) {
Text("Čeština")
}
}
}
Handy for apps, where “choose language” option is available, you want to use Over‑the‑Air (OTA) translation updates (translations refresh automatically when locale or content changes), and during testing/debugging.
Most often used in settings screens, onboarding flows, or debug menus - anytime you want to allow runtime language switching.
Observing Locale Changes
Collecting Tolgee.changeFlow as state ensures your composable recomposes whenever the locale changes,
keeping the UI in sync with the active language.
@Composable
fun LocaleAwareComponent() {
val tolgee = Tolgee.instance
val locale by tolgee.changeFlow
.map { tolgee.getLocale() }
.collectAsState(initial = tolgee.getLocale())
Text(text = locale.language)
}
Handy for observing locale changes in real time, keeping track of current locale, showing chosen language in app, reactively updating translations whenever user switches the language.