Multilingual support

Updated at:

The localization system (Locale) is the core internationalization module in AliPlayerKit. Its unified language manager, LocaleManager, provides key features like following the system language, manual specification, and custom provider injection. This enables the player's UI copy to adapt to multilingual scenarios.

Key concepts

What is LocaleManager?

LocaleManager is the built-in internationalization singleton in AliPlayerKit. It looks up localized strings for all player UI copy, such as button text, error messages, and setting titles. It uses a Resolution Chain mechanism to find translations by checking sources in order of priority:

custom provider → activeBundle (manual/system) → sdkBundle fallback → key itself

Key benefits

Benefit

Description

Zero-configuration

Built-in support for Simplified Chinese and English. Automatically follows the system language and requires no initialization.

Flexible switching

Supports both following the system language and manual specification to meet various business needs.

Extensible

Injecting custom translations via the LocaleProvider protocol has the highest priority.

Real-time notifications

After a language switch, a LocaleDidChangeNotification is sent to refresh the entire UI.

Language modes

Mode

Enum value

Description

Follow system (default)

LocaleModeSystem

Automatically uses the system's preferred language.

Manual

LocaleModeManual

Uses the language code that you specify.

Features

Default behavior (zero-configuration)

AliPlayerKit provides multilingual support out of the box, with no additional code required:

  • Built-in support for Simplified Chinese and English.

  • The player's UI copy automatically follows the system language. No initialization or API calls are needed.

  • When a user changes their language in Settings > General > Language & Region, the player's UI copy updates automatically.

  • If the system language is not supported, the player falls back to the default language (Simplified Chinese).

Built-in languages

Language

Language code

Resource directory

Simplified Chinese (default)

zh-Hans

PlayerKit.bundle/en.lproj/Localizable.strings

English

en

PlayerKit.bundle/en.lproj/Localizable.strings

Note

For applications that only require Simplified Chinese and English support, no API calls are needed; you just need to integrate AliPlayerKit.

Built-in UI copy key categories

Category

Key constant prefix

Example keys

Playback state

LocaleKeyPlayState*

LocaleKeyPlayStateLoading, LocaleKeyPlayStateErrorCodeLabel

Bottom control bar

LocaleKeyBottomBar*

LocaleKeyBottomBarSpeed, LocaleKeyBottomBarQuality

Speed hint

LocaleKeySpeedPlayingHint

UI copy indicating playback is at a non-default speed.

Settings menu

LocaleKeySetting*

LocaleKeySettingMenuTitle, LocaleKeySettingSpeed

Mirror mode

LocaleKeyMirror*

LocaleKeyMirrorNone, LocaleKeyMirrorHorizontal

Scaling mode

LocaleKeyScale*

LocaleKeyScaleFitXY, LocaleKeyScaleCenterCrop

Log panel

LocaleKeyLogPanel*

LocaleKeyLogPanelTitleHide, LocaleKeyLogPanelClear

Landscape hint

LocaleKeyLandscapeHint*

LocaleKeyLandscapeHintFullscreen

General

LocaleKeyUnknown

Generic "Unknown" UI copy.

Note

All Key constants are defined in LocaleKeys.h , using typed constants to ensure compile-time checking and IDE auto-completion.

How to use

Follow system language (default, no code required)

After you integrate AliPlayerKit, the player UI copy automatically displays translations based on the system language:

// No configuration is needed. The player UI copy automatically follows the system language.
AliPlayerModel *model = [[AliPlayerModel alloc] initWithVideoSource:source];
[controller configure:model];
// If the system language is English, all UI copy automatically displays in English.

Manually specify a language

Use this method when you want to provide an in-app language switcher instead of following the system language:

// Force switch to English
[AliPlayerKit setLanguageCode:@"en"];

// Force switch to Simplified Chinese
[AliPlayerKit setLanguageCode:@"zh-Hans"];
Note

After the setLanguageCode: method is called, localeMode is automatically switched to LocaleModeManual, a LocaleDidChangeNotification is sent, and the UI copy for bound slots is automatically refreshed.

Revert to following system language

To revert to following the system language after specifying a language manually:

// Clear the manual setting and restore localeMode to LocaleModeSystem.
[AliPlayerKit resetLocaleToSystemLanguage];

Get the current language

// Get the currently active language code.
NSString *currentLang = [AliPlayerKit currentLanguageCode];
NSLog(@"Current language: %@", currentLang); // Example: "zh-Hans" or "en"

Listen for language changes

Use NSNotification to listen for language change events:

// Register the observer
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(localeDidChange:)
                                             name:LocaleDidChangeNotification
                                           object:nil];

// Handle the language change
- (void)localeDidChange:(NSNotification *)notification {
    NSString *newLanguage = notification.userInfo[LocaleLanguageCodeKey];
    NSLog(@"Language changed to: %@", newLanguage);
    // Refresh your custom UI copy
    [self refreshCustomUI];
}

// Remove the observer when it is no longer needed
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:LocaleDidChangeNotification
                                                  object:nil];
}

Custom development

LocaleProvider protocol

LocaleProvider defines the injection interface for custom translations:

@protocol LocaleProvider <NSObject>

@optional

/// Returns a localized string for a specific key.
/// Return nil to indicate that this key is not handled, allowing the SDK's internal fallback to proceed.
- (nullable NSString *)localizedStringForKey:(NSString *)key;

@end
Note

localizedStringForKey: is an @optional method. LocaleManager checks with respondsToSelector: before calling it.

Inject a custom provider

By implementing the LocaleProvider protocol, you can customize any translation without modifying resource files:

// 1. Define your custom provider.
@interface MyLocaleProvider : NSObject <LocaleProvider>
@end

@implementation MyLocaleProvider

- (nullable NSString *)localizedStringForKey:(NSString *)key {
    // Provide custom translations for specific keys.
    if ([key isEqualToString:LocaleKeyPlayStateLoading]) {
        return @"Video buffer, please wait...";
    }
    if ([key isEqualToString:LocaleKeySettingSpeed]) {
        return @"Playback magnification";
    }
    // Return nil for other keys to let the SDK's fallback chain handle them.
    return nil;
}

@end

// 2. Inject the provider.
MyLocaleProvider *provider = [[MyLocaleProvider alloc] init];
[AliPlayerKit setLocaleProvider:provider];
Note

LocaleManager holds a weak reference to the Provider, so you must maintain the lifecycle of the Provider instance.

Add a new language

To add a language not built into AliPlayerKit, such as Japanese, implement a custom provider that supplies a full set of translations:

@interface JapaneseLocaleProvider : NSObject <LocaleProvider>
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *translations;
@end

@implementation JapaneseLocaleProvider

- (instancetype)init {
    self = [super init];
    if (self) {
        _translations = @{
            LocaleKeyPlayStateLoading: @"読み込み中...",
            LocaleKeyPlayStateErrorCodeLabel: @"エラーコード",
            LocaleKeyPlayStateErrorMessageLabel: @"エラーメッセージ",
            LocaleKeyBottomBarSpeed: @"速度",
            LocaleKeyBottomBarQuality: @"画質",
            LocaleKeySettingMenuTitle: @"設定",
            LocaleKeySettingSpeed: @"再生速度",
            LocaleKeySettingLoop: @"ループ再生",
            LocaleKeySettingMute: @"ミュート",
            LocaleKeySettingQuality: @"画質",
            // ... provide translations for all keys from LocaleKeys.h
        };
    }
    return self;
}

- (nullable NSString *)localizedStringForKey:(NSString *)key {
    return self.translations[key];
}

@end

// Inject the Japanese provider. You must maintain a strong reference to the instance.
self.jaProvider = [[JapaneseLocaleProvider alloc] init];
[AliPlayerKit setLocaleProvider:self.jaProvider];

Override existing translations

To override only specific translations, make the provider return a value only for the keys you want to change. Return nil for all other keys to let the SDK handle them:

- (nullable NSString *)localizedStringForKey:(NSString *)key {
    // Override only the "Loading" message.
    if ([key isEqualToString:LocaleKeyPlayStateLoading]) {
        return @"Wonderful about to present...";
    }
    return nil; // Use the SDK's built-in translation for all other keys.
}

Remove a custom provider

// Remove the provider to revert to the SDK's built-in translations.
[AliPlayerKit setLocaleProvider:nil];

Design details

String resolution chain

LocaleManager internally searches for translations in the following order of priority:

Priority

Source

Description

1 (Highest)

Custom provider

An object that implements the LocaleProvider protocol is called after being checked with respondsToSelector:.

2

activeBundle

The currently active language bundle (the manually specified language in Manual mode, or the system language in System mode).

3

sdkBundle fallback

If a translation is not found in the activeBundle, the search proceeds to the main SDK bundle.

4 (Lowest)

Key fallback

Returns the key itself.

Thread safety

LocaleManager internally uses a serial queue (dispatch_queue_t) to protect all state read and write operations:

Operation

Thread safety mechanism

Reading properties (currentLanguageCode, localeMode)

dispatch_sync serial queue

Language switching methods (setLanguageCode: etc.)

dispatch_sync serial queue

Notification dispatch

dispatch_async(dispatch_get_main_queue(), ...) always runs on the main thread.

Provider reference

Weak reference (weak)

Notification mechanism

Notification name

When triggered

userInfo

LocaleDidChangeNotification

When the language mode or language code changes.

LocaleLanguageCodeKey → new language code

Trigger scenarios:

  • Call setLanguageCode: to switch the language manually

  • Call resetLocaleToSystemLanguage to follow the system language.

  • Call setLocaleProvider: to inject or remove a Provider.

API reference

AliPlayerKit class methods

Method

Description

+setLanguageCode:

Manually specify the language code (such as @"en" and @"zh-Hans"), and set localeMode to LocaleModeManual.

+resetLocaleToSystemLanguage

To revert to following the system language, set localeMode to LocaleModeSystem.

+setLocaleProvider:

Injects or removes a custom translation Provider (held by a weak reference). Pass nil to remove.

+currentLanguageCode

Gets the currently active language code.

LocaleManager instance methods

Method/Property

Description

+sharedManager

Gets the singleton instance.

currentLanguageCode

Current language code (readonly)

localeMode

Current language mode (readonly, LocaleModeSystem / LocaleModeManual)

localeProvider

Currently injected Provider (readonly, weak reference)

-setLanguageCode:

Sets the language code for manual mode.

-setLocaleProvider:

Injects a custom provider.

-resetToSystemLanguage

Resets the mode to follow the system language.

-localizedStringForKey:

Looks up a localized string using the Resolution Chain.

LocaleProvider protocol

Method

Required

Description

-localizedStringForKey:

@optional

Returns a translation for a specific key. Return nil to fall back to the SDK's built-in translation.

Macros for internal use

Macro

Usage

Description

LocalizedString(key)

LocalizedString(LocaleKeyPlayStateLoading)

Gets a localized string.

LocalizedFormat(key, ...)

LocalizedFormat(LocaleKeyBottomBarTimeFormat, min, sec, totalMin, totalSec)

Gets a formatted localized string.

Notification constants

Constant

Type

Description

LocaleDidChangeNotification

NSNotificationName

The name of the notification for language changes.

LocaleLanguageCodeKey

NSString *

The key for the new language code in the notification's userInfo dictionary.

Related files

File

Description

PlayerKit/Source/Locale/LocaleManager.h

The localization manager (singleton).

PlayerKit/Source/Locale/LocaleProvider.h

The protocol for a custom translation provider.

PlayerKit/Source/Locale/LocaleKeys.h

Definitions for all localization key constants.

PlayerKit/Source/Locale/LocaleMacros.h

Convenience macros for internal SDK use.

PlayerKit/Source/Locale/Localized.h

Aggregate header file.

PlayerKit/Source/AliPlayerKit.h

The entry point for global APIs, including locale-related class methods.

PlayerKit.bundle/en.lproj/Localizable.strings

The translation file for Simplified Chinese.

PlayerKit.bundle/en.lproj/Localizable.strings

The translation file for English.

For a complete list of APIs, see the API reference.

Best practices

Approaches

Scenario

Recommended approach

Notes

Only need Simplified Chinese and English

No action required

Built-in languages automatically follow the system language.

Adding a new language

LocaleProvider full translation

Implement the protocol and return translations for all keys.

Overriding some UI copy

LocaleProvider Partial translation

Handle only the keys you need to override and return nil for the rest.

Forcing a specific language

setLanguageCode:

Lets your application control the language instead of following the system.

Switching languages at runtime

setLanguageCode: + Notification

After switching, use notifications to refresh any custom UI.

Usage notes

Item

Description

Provider lifecycle

The provider is held by a weak reference. You must manage the lifecycle of the provider instance to prevent it from being deallocated.

Full translation

When you add a new language, you should translate all LocaleKeys.h keys to avoid displaying a mix of languages.

Language code standard

Use the Apple standard lproj naming convention (for example, zh-Hans, en, and ja).

Notification thread

LocaleDidChangeNotification is always sent on the main thread, so you can safely update the UI.

Automatic slot refresh

Built-in slots listen for language change notifications and automatically refresh their UI copy. You must implement this for custom slots.

Example

The PlayerKitUsages/UsageLocaleSystem module provides a complete example of using the localization system, including language switching, custom provider injection, and listening for change notifications.