Introduction
Modern mobile apps need to handle localization in two ways:
-
System-level Language Settings: Both iOS and newer versions of Android allow users to change app language directly from system settings. This is the preferred method for many users as it provides a centralized way to manage language preferences across all apps.
-
In-app Language Settings: Apps should also provide the ability to change language within the app itself. This is useful for:
- Allowing users to switch languages without leaving the app
- Providing language selection during onboarding
- Offering a more accessible way to change language for users who might not be familiar with system settings
The challenge is to keep these two methods in sync - when a user changes the language in system settings, the app should reflect that change, and vice versa. This guide shows how to implement a robust localization system that handles both scenarios while maintaining consistency across the app.
1. Project Structure
First, create the following directory structure in your project:
2. Define Supported Languages
Create an enum class to define supported languages:
3. Create String Resources
English (values/strings.xml)
Bangla (values-bn/strings.xml)
4. Platform-Specific Language Implementation
The and functions are implemented differently for each platform to get and set the current app language:
Common Interface (expect)
iOS Implementation (actual)
The iOS implementation uses to get the current system language code. For changing the language, it uses to set the preferred languages list, as the iOS system stores the app lanaguage in with a key: .
Android Implementation (actual)
Android-Specific Requirements
For the Android implementation () to work properly, you need to ensure:
- Your extends :
- Your app theme extends an AppCompat theme in :
- Apply the theme in your :
These requirements are necessary because is part of the AndroidX AppCompat library, and the localization features require the proper AppCompat integration to function correctly.
While we’re at it, let’s add this service so that it works on Android 12 or older:
Learn more about why this is needed.
5. Provide platform with the available languages
iOS
Add the available language codes to your file like so
Android
On Android, the available languages are generated automatically from the string resources, when you add
in the gradle file.
In of add:
makes sure no other locale resources (e.g. from a third-party library)
get added to the app bundle.
6. Using CompositionLocal for Language Management
To make the app language accessible throughout the app and allow changing it from the settings screen, we can use CompositionLocal with mutable state. Here’s how to implement it:
1. Define the CompositionLocal
First, create a CompositionLocal for the app language:
2. Provide the Language State at the Root
In your root composable (usually in your navigation setup), provide the language state:
3. Access and Change Language from Any Screen
You can now access and change the language from any screen in your app: