Multilingual

更新时间:
复制 MD 格式

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.xml and values-en/strings.xml.

  • The player UI text automatically switches to match the system language without requiring initialization or API calls.

  • When a user changes the language in system settings, the player UI text automatically updates to the new language.

  • If the system language is not one of the built-in supported languages, the player falls back to the default language (Chinese).

Language

Resource directory

Chinese (default)

res/values/strings.xml

English

res/values-en/strings.xml

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.

  1. Create the corresponding resource directory in 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: Korean
  2. Copy the default values/strings.xml file 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>
  3. Apply the new language.

    • Automatic System Follow: When a user sets their system language to Korean, the player UI text switches automatically without any API calls.

    • Force Language: To switch the language manually within your app, call PlayerLocale.setLanguage("ko").

Note

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

get(resId)

Retrieves text from a strings.xml resource.

get(resId, formatArgs)

Retrieves formatted text, replacing %s and %d placeholders.

setLanguage(languageCode)

Sets the application language, triggering an Activity recreation.

getLanguage()

Retrieves the current language code.

addOnLanguageChangedListener(listener)

Registers a language change listener.

removeOnLanguageChangedListener(listener)

Unregisters a language change listener.

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.xml file exists for the current language, the system returns the translation from that file.

  • If no corresponding resource directory exists for the current language, the system falls back to the default language (Chinese, from values/strings.xml).

Thread safety

Component

Thread safety mechanism

Listener list

CopyOnWriteArrayList provides excellent performance in read-heavy and write-light scenarios.

Language switching mechanism

The internal implementation of setLanguage() uses AppCompatDelegate.setApplicationLocales():

  • On Android 13+, the system natively supports per-app language preferences, so no special handling is required.

  • On Android 12 and earlier, a Context with the correct locale is created using createConfigurationContext.

  • This call recreates the current Activity, which automatically refreshes the UI.

Version compatibility

Android version

Behavior

Android 13+ (API 33+)

Uses the native per-app language feature. The application context automatically syncs the locale.

Android 12 and earlier

Compatibility is achieved by using AppCompatDelegate and createConfigurationContext.

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 system language.

Add a new language set

values-xx/strings.xml

Translate all strings. This uses the native Android mechanism and automatically follows the system language once added.

Force a specific language

PlayerLocale.setLanguage()

Overrides the system language. The language is controlled by the application.

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 memory leak, remove the listener when the Activity or Fragment is destroyed.

Language code format

Use the ISO 639-1 standard two-letter lowercase format (e.g., "zh", "en", "ja").

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 values-ja/strings.xml).

Change listening

Demonstrates how to register a language change listener and receive callbacks.

Run the example

In the Demo App, select the Locale example to see it in action.