Localizing strings
Localization is the process of adapting your app to different languages, regions, and cultural conventions. This guide explains how to set up translation directories, work with region-specific formats, handle right-to-left (RTL) languages, and test localization across platforms.
To localize strings in Compose Multiplatform, you need to provide translated texts for your application's user interface elements in all supported languages. Compose Multiplatform simplifies this process by offering a common resource management library and code generation for easy access to translations.
Set up translation directories
Store all string resources in a dedicated composeResources
directory within your common source set. Place the default texts in the values
directory, and create corresponding directories for each language. Use the following structure:
Within the values
directories and its localized variants, define string resources in strings.xml
files using key-value pairs. For example, add English texts to commonMain/composeResources/values/strings.xml
:
Then, create corresponding localized files for translations. For example, add Spanish translations to commonMain/composeResources/values-es/strings.xml
:
Generate class for static access
Once you've added all translations, build the project to generate a special class that provides access to resources. Compose Multiplatform processes the strings.xml
resource files in composeResources
and creates static accessor properties for each string resource.
The resulting Res.strings
object allows you to safely access localized strings from your shared code. To display strings in your app's UI, use the stringResource()
composable function. This function retrieves the correct text based on the user's current locale:
In the example above, the welcome_message
string includes a placeholder (%s
) for a dynamic value. Both the generated accessor and the stringResource()
function support passing such parameters.