The multilingual (Locale) module is the internationalization component of AliPlayerKit. It uses a unified language manager to provide core capabilities, such as automatically following the system language, full language replacement, and runtime language switching. This enables the player UI text to adapt flexibly to multilingual scenarios.
Default behavior (zero configuration)
AliPlayerKit provides out-of-the-box multilingual support without requiring you to write any code:
Built-in support for Chinese and English through
values/strings.xmlandvalues-en/strings.xml.The
player UI textautomatically switches to match thesystem languagewithout requiring initialization or API calls.When a user changes the language in system settings, the
player UI textautomatically updates to the new language.If the
system languageis not one of the built-in supported languages, the player falls back to thedefault language(Chinese).
Language | Resource directory |
Chinese (default) |
|
English |
|
For applications that only require Chinese and English support, no API calls are necessary; simply integrate AliPlayerKit.
Custom scenarios
Only use the following APIs if you need to override the default behavior of following the system language.
Force a specific language
When you need to provide a language switching option in your app that does not follow the system language, use setLanguage() to explicitly set the language:
// Force the language to English (no longer follows the system language)
PlayerLocale.setLanguage("en");
// Force the language back to Chinese
PlayerLocale.setLanguage("zh");
When setLanguage() is called, the current Activity is automatically recreated for the new language to take effect.
Add a new language
You can add support for new languages, like Japanese or Korean, by using the standard Android resource mechanism. After you add the new language resources, AliPlayerKit will automatically follow the system language for that language.
Create the corresponding
resource directoryin your app module.your-app/src/main/res/ ├── values/strings.xml ← Chinese (default) ├── values-en/strings.xml ← English ├── values-ja/strings.xml ← New: Japanese └── values-ko/strings.xml ← New: KoreanCopy the default
values/strings.xmlfile and translate all its entries.<!-- res/values-ko/strings.xml --><?xml version="1.0" encoding="utf-8"?><resources><string name="setting_item_speed">재생 속도</string><string name="setting_item_quality">화질</string><string name="setting_item_loop">반복 재생</string><string name="setting_item_mute">음소거</string><string name="player_brightness">밝기</string><string name="player_volume">볼륨</string><!-- ... translate all entries ... --></resources>Apply the new language.
Automatic System Follow: When a user sets their
system languageto Korean, theplayer UI textswitches automatically without any API calls.Force Language: To switch the language manually within your app, call
PlayerLocale.setLanguage("ko").
We recommend translating all entries in values/strings.xml to the new language file. This practice ensures a full translation and prevents a mixed-language display caused by some text falling back to the default language.
Listen for language changes
Use a listener to detect language changes. This is useful for refreshing a custom UI or for logging purposes:
// Create the listener
PlayerLocale.OnLanguageChangedListener listener = (oldLanguage, newLanguage) -> {
Log.i(TAG, "Language changed: " + oldLanguage + " → " + newLanguage);
// Refresh your custom UI
refreshMyCustomUI();
};
// Register the listener
PlayerLocale.addOnLanguageChangedListener(listener);
// Remove the listener when it is no longer needed (e.g., in Activity.onDestroy())
PlayerLocale.removeOnLanguageChangedListener(listener);API reference
PlayerLocale methods
Method | Description |
| Retrieves text from a |
| Retrieves formatted text, replacing |
| Sets the application language, triggering an |
| Retrieves the current |
| Registers a language change |
| Unregisters a language change |
How it works
Text lookup mechanism
PlayerLocale.get() obtains the text for the current language through the standard Android resource mechanism:
If a
values-xx/strings.xmlfile exists for the current language, the system returns the translation from that file.If no corresponding
resource directoryexists for the current language, the system falls back to thedefault language(Chinese, fromvalues/strings.xml).
Thread safety
Component | Thread safety mechanism |
Listener list |
|
Language switching mechanism
The internal implementation of setLanguage() uses AppCompatDelegate.setApplicationLocales():
On Android 13+, the system natively supports
per-app languagepreferences, so no special handling is required.On Android 12 and earlier, a
Contextwith the correct locale is created usingcreateConfigurationContext.This call recreates the current
Activity, which automatically refreshes the UI.
Version compatibility
Android version | Behavior |
Android 13+ (API 33+) | Uses the native |
Android 12 and earlier | Compatibility is achieved by using |
Best practice
Choosing the right approach
Scenario | Recommended approach | Description |
Chinese and English only | No action required | Built-in support for Chinese and English. Automatically follows the |
Add a new language set |
| Translate all strings. This uses the native Android mechanism and automatically follows the |
Force a specific language |
| Overrides the |
Important notes
Item | Description |
Full translation | When adding a new language, translate all entries to avoid a mixed-language display. |
Listener lifecycle | To prevent a |
Language code format | Use the ISO 639-1 standard two-letter lowercase format (e.g., |
Example
You can find a complete example in playerkit-examples/example-locale.
Example features
Feature | Description |
Language switching | Demonstrates real-time switching between Chinese, English, and Japanese (Japanese is added via |
Change listening | Demonstrates how to register a language change |
Run the example
In the Demo App, select the Locale example to see it in action.