Multilingual support
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 itselfKey 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 |
Real-time notifications | After a language switch, a |
Language modes
Mode | Enum value | Description |
Follow system (default) |
| Automatically uses the system's preferred language. |
Manual |
| 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) |
|
|
English |
|
|
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 |
|
|
Bottom control bar |
|
|
Speed hint |
| UI copy indicating playback is at a non-default speed. |
Settings menu |
|
|
Mirror mode |
|
|
Scaling mode |
|
|
Log panel |
|
|
Landscape hint |
|
|
General |
| Generic "Unknown" UI copy. |
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"];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;
@endlocalizedStringForKey: 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];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 |
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 |
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 ( |
|
Language switching methods ( |
|
Notification dispatch |
|
Provider reference | Weak reference ( |
Notification mechanism
Notification name | When triggered | userInfo |
| When the language mode or language code changes. |
|
Trigger scenarios:
Call
setLanguageCode:to switch the language manuallyCall
resetLocaleToSystemLanguageto follow the system language.Call
setLocaleProvider:to inject or remove a Provider.
API reference
AliPlayerKit class methods
Method | Description |
| Manually specify the language code (such as |
| To revert to following the system language, set |
| Injects or removes a custom translation Provider (held by a weak reference). Pass |
| Gets the currently active language code. |
LocaleManager instance methods
Method/Property | Description |
| Gets the singleton instance. |
| Current language code ( |
| Current language mode ( |
| Currently injected Provider ( |
| Sets the language code for manual mode. |
| Injects a custom provider. |
| Resets the mode to follow the system language. |
| Looks up a localized string using the Resolution Chain. |
LocaleProvider protocol
Method | Required | Description |
|
| Returns a translation for a specific key. Return |
Macros for internal use
Macro | Usage | Description |
|
| Gets a localized string. |
|
| Gets a formatted localized string. |
Notification constants
Constant | Type | Description |
|
| The name of the notification for language changes. |
|
| The key for the new language code in the notification's userInfo dictionary. |
Related files
File | Description |
| The localization manager (singleton). |
| The protocol for a custom translation provider. |
| Definitions for all localization key constants. |
| Convenience macros for internal SDK use. |
| Aggregate header file. |
| The entry point for global APIs, including locale-related class methods. |
| The translation file for Simplified Chinese. |
| 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 |
| Implement the protocol and return translations for all keys. |
Overriding some UI copy |
| Handle only the keys you need to override and return |
Forcing a specific language |
| Lets your application control the language instead of following the system. |
Switching languages at runtime |
| 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 |
Language code standard | Use the Apple standard lproj naming convention (for example, |
Notification thread |
|
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.