添加多语言
This commit is contained in:
57
Shared/KBLocalizationManager.h
Normal file
57
Shared/KBLocalizationManager.h
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// KBLocalizationManager.h
|
||||
// 多语言管理(App 与键盘扩展共用)
|
||||
// 功能:
|
||||
// - 运行时切换语言(不依赖系统设置)
|
||||
// - 可选跨 Target 同步(共享钥匙串),让 App 与扩展语言一致
|
||||
// - 提供便捷宏 KBLocalized(key)
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// 当前语言变更通知(不附带 userInfo)
|
||||
extern NSString * const KBLocalizationDidChangeNotification;
|
||||
|
||||
/// 轻量多语言管理器:支持运行时切换、跨 Target 同步
|
||||
@interface KBLocalizationManager : NSObject
|
||||
|
||||
/// 单例
|
||||
+ (instancetype)shared;
|
||||
|
||||
/// 当前语言代码(如:"en"、"zh-Hans"、"ja")。
|
||||
/// 默认会在受支持语言中,按系统首选语言择优匹配。
|
||||
@property (nonatomic, copy, readonly) NSString *currentLanguageCode;
|
||||
|
||||
/// 支持的语言代码列表。默认 @[@"en", @"zh-Hans"]。
|
||||
/// 建议在启动早期设置;或设置后再调用 `-setCurrentLanguageCode:persist:` 以刷新。
|
||||
@property (nonatomic, copy) NSArray<NSString *> *supportedLanguageCodes;
|
||||
|
||||
/// 设置当前语言。
|
||||
/// @param code 语言代码
|
||||
/// @param persist 是否持久化到共享钥匙串(以便 App 与扩展共享该选择)
|
||||
- (void)setCurrentLanguageCode:(NSString *)code persist:(BOOL)persist;
|
||||
|
||||
/// 清除用户选择,恢复为系统最佳匹配。
|
||||
- (void)resetToSystemLanguage;
|
||||
|
||||
/// 从默认表(Localizable.strings)取文案。
|
||||
- (NSString *)localizedStringForKey:(NSString *)key;
|
||||
|
||||
/// 指定表名(不含扩展名)取文案。
|
||||
- (NSString *)localizedStringForKey:(NSString *)key
|
||||
table:(nullable NSString *)table
|
||||
value:(nullable NSString *)value;
|
||||
|
||||
/// 基于一组“偏好语言”计算最佳支持语言。
|
||||
- (NSString *)bestSupportedLanguageForPreferred:(NSArray<NSString *> *)preferred;
|
||||
|
||||
@end
|
||||
|
||||
/// 便捷宏:与 NSLocalizedString 类似,但遵循 KBLocalizationManager 当前语言
|
||||
#ifndef KBLocalized
|
||||
#define KBLocalized(key) [[KBLocalizationManager shared] localizedStringForKey:(key)]
|
||||
#endif
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
180
Shared/KBLocalizationManager.m
Normal file
180
Shared/KBLocalizationManager.m
Normal file
@@ -0,0 +1,180 @@
|
||||
//
|
||||
// KBLocalizationManager.m
|
||||
// 多语言管理实现
|
||||
//
|
||||
|
||||
#import "KBLocalizationManager.h"
|
||||
#import <Security/Security.h>
|
||||
#import "KBConfig.h"
|
||||
|
||||
/// 语言变更通知名称
|
||||
NSString * const KBLocalizationDidChangeNotification = @"KBLocalizationDidChangeNotification";
|
||||
|
||||
// 通过共享钥匙串跨 Target 持久化语言选择
|
||||
static NSString * const kKBLocService = @"com.keyBoardst.loc";
|
||||
static NSString * const kKBLocAccount = @"lang"; // 保存 UTF8 的语言代码
|
||||
|
||||
@interface KBLocalizationManager ()
|
||||
@property (nonatomic, copy, readwrite) NSString *currentLanguageCode; // 当前语言代码
|
||||
@property (nonatomic, strong) NSBundle *langBundle; // 对应语言的 .lproj 资源包
|
||||
@end
|
||||
|
||||
// 避免 +shared 初始化阶段递归触发自身:
|
||||
// 这里提供一个 C 级别的工具函数,构建钥匙串查询,不依赖实例或 +shared。
|
||||
static inline NSMutableDictionary *KBLocBaseKCQuery(void) {
|
||||
NSMutableDictionary *q = [@{ (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
|
||||
(__bridge id)kSecAttrService: kKBLocService,
|
||||
(__bridge id)kSecAttrAccount: kKBLocAccount } mutableCopy];
|
||||
if (KB_KEYCHAIN_ACCESS_GROUP.length > 0) {
|
||||
q[(__bridge id)kSecAttrAccessGroup] = KB_KEYCHAIN_ACCESS_GROUP;
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
||||
@implementation KBLocalizationManager
|
||||
|
||||
+ (instancetype)shared {
|
||||
static KBLocalizationManager *m; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
|
||||
m = [KBLocalizationManager new];
|
||||
// 默认支持语言;可在启动时由外部重置
|
||||
m.supportedLanguageCodes = @[ @"en", @"zh-Hans" ];
|
||||
// 启动读取:先取共享钥匙串,再按系统偏好回退
|
||||
NSString *saved = [[self class] kc_read];
|
||||
if (saved.length == 0) {
|
||||
saved = [m bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: @"en";
|
||||
}
|
||||
[m applyLanguage:saved];
|
||||
});
|
||||
return m;
|
||||
}
|
||||
|
||||
#pragma mark - 对外 API
|
||||
|
||||
- (void)setSupportedLanguageCodes:(NSArray<NSString *> *)supportedLanguageCodes {
|
||||
// 归一化:去重、去空
|
||||
NSMutableOrderedSet *set = [NSMutableOrderedSet orderedSet];
|
||||
for (NSString *c in supportedLanguageCodes) {
|
||||
if (c.length) { [set addObject:c]; }
|
||||
}
|
||||
_supportedLanguageCodes = set.array.count ? set.array : @[ @"en" ];
|
||||
// 若当前语言不再受支持,则按最佳匹配切回(不持久化,仅内存),并广播变更
|
||||
if (self.currentLanguageCode.length && ![set containsObject:self.currentLanguageCode]) {
|
||||
NSString *best = [self bestSupportedLanguageForPreferred:@[self.currentLanguageCode]];
|
||||
[self applyLanguage:best ?: _supportedLanguageCodes.firstObject];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setCurrentLanguageCode:(NSString *)code persist:(BOOL)persist {
|
||||
if (code.length == 0) return; // 忽略空值
|
||||
if ([code isEqualToString:self.currentLanguageCode]) return; // 无变更
|
||||
[self applyLanguage:code];
|
||||
if (persist) { [[self class] kc_write:code]; } // 需同步到 App/扩展
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
|
||||
}
|
||||
|
||||
- (void)resetToSystemLanguage {
|
||||
NSString *best = [self bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: @"en";
|
||||
[self setCurrentLanguageCode:best persist:NO];
|
||||
}
|
||||
|
||||
- (NSString *)localizedStringForKey:(NSString *)key {
|
||||
return [self localizedStringForKey:key table:nil value:key];
|
||||
}
|
||||
|
||||
- (NSString *)localizedStringForKey:(NSString *)key table:(NSString *)table value:(NSString *)value {
|
||||
if (key.length == 0) return @"";
|
||||
NSBundle *bundle = self.langBundle ?: NSBundle.mainBundle;
|
||||
NSString *tbl = table ?: @"Localizable";
|
||||
// 使用 bundle API,避免 NSLocalizedString 被系统语言钉死
|
||||
NSString *str = [bundle localizedStringForKey:key value:value table:tbl];
|
||||
return str ?: (value ?: key);
|
||||
}
|
||||
|
||||
- (NSString *)bestSupportedLanguageForPreferred:(NSArray<NSString *> *)preferred {
|
||||
if (self.supportedLanguageCodes.count == 0) return @"en";
|
||||
// 1) 完全匹配
|
||||
for (NSString *p in preferred) {
|
||||
NSString *pLC = p.lowercaseString;
|
||||
for (NSString *s in self.supportedLanguageCodes) {
|
||||
if ([pLC isEqualToString:s.lowercaseString]) { return s; }
|
||||
}
|
||||
}
|
||||
// 2) 前缀匹配:如 zh-Hans-CN -> zh-Hans, en-GB -> en
|
||||
for (NSString *p in preferred) {
|
||||
NSString *pLC = p.lowercaseString;
|
||||
for (NSString *s in self.supportedLanguageCodes) {
|
||||
NSString *sLC = s.lowercaseString;
|
||||
if ([pLC hasPrefix:[sLC stringByAppendingString:@"-"]] || [pLC hasPrefix:[sLC stringByAppendingString:@"_"]]) {
|
||||
return s;
|
||||
}
|
||||
// also allow reverse: when supported is regional (rare)
|
||||
if ([sLC hasPrefix:[pLC stringByAppendingString:@"-"]] || [sLC hasPrefix:[pLC stringByAppendingString:@"_"]]) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3) 特殊处理中文:将 zh-Hant/zh-TW/zh-HK 映射到 zh-Hant(若受支持)
|
||||
for (NSString *p in preferred) {
|
||||
NSString *pLC = p.lowercaseString;
|
||||
if ([pLC hasPrefix:@"zh-hant"] || [pLC hasPrefix:@"zh-tw"] || [pLC hasPrefix:@"zh-hk"]) {
|
||||
for (NSString *s in self.supportedLanguageCodes) {
|
||||
if ([s.lowercaseString isEqualToString:@"zh-hant"]) { return s; }
|
||||
}
|
||||
}
|
||||
if ([pLC hasPrefix:@"zh-hans"] || [pLC hasPrefix:@"zh-cn"]) {
|
||||
for (NSString *s in self.supportedLanguageCodes) {
|
||||
if ([s.lowercaseString isEqualToString:@"zh-hans"]) { return s; }
|
||||
}
|
||||
}
|
||||
}
|
||||
// 4) 兜底:取第一个受支持语言
|
||||
return self.supportedLanguageCodes.firstObject ?: @"en";
|
||||
}
|
||||
|
||||
#pragma mark - 内部实现
|
||||
|
||||
- (void)applyLanguage:(NSString *)code {
|
||||
_currentLanguageCode = [code copy];
|
||||
// 基于当前 Target(App 或扩展)的主 bundle 加载 .lproj 资源
|
||||
NSString *path = [NSBundle.mainBundle pathForResource:code ofType:@"lproj"];
|
||||
if (!path) {
|
||||
// 尝试去区域后缀:如 en-GB -> en
|
||||
NSString *shortCode = [[code componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]] firstObject];
|
||||
if (shortCode.length > 0) {
|
||||
path = [NSBundle.mainBundle pathForResource:shortCode ofType:@"lproj"];
|
||||
}
|
||||
}
|
||||
if (path) {
|
||||
self.langBundle = [NSBundle bundleWithPath:path];
|
||||
} else {
|
||||
self.langBundle = NSBundle.mainBundle; // 兜底
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - 钥匙串读写(App/扩展共享)
|
||||
|
||||
+ (BOOL)kc_write:(NSString *)lang {
|
||||
NSMutableDictionary *q = KBLocBaseKCQuery();
|
||||
SecItemDelete((__bridge CFDictionaryRef)q);
|
||||
if (lang.length == 0) return YES; // 等价于删除
|
||||
NSData *data = [lang dataUsingEncoding:NSUTF8StringEncoding];
|
||||
q[(__bridge id)kSecValueData] = data ?: [NSData data];
|
||||
q[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
|
||||
OSStatus st = SecItemAdd((__bridge CFDictionaryRef)q, NULL);
|
||||
return (st == errSecSuccess);
|
||||
}
|
||||
|
||||
+ (NSString *)kc_read {
|
||||
NSMutableDictionary *q = KBLocBaseKCQuery();
|
||||
q[(__bridge id)kSecReturnData] = @YES;
|
||||
q[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
|
||||
CFTypeRef dataRef = NULL; OSStatus st = SecItemCopyMatching((__bridge CFDictionaryRef)q, &dataRef);
|
||||
if (st != errSecSuccess || !dataRef) return nil;
|
||||
NSData *data = (__bridge_transfer NSData *)dataRef;
|
||||
if (data.length == 0) return nil;
|
||||
NSString *lang = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
return lang;
|
||||
}
|
||||
|
||||
@end
|
||||
18
Shared/Localization/en.lproj/Localizable.strings
Normal file
18
Shared/Localization/en.lproj/Localizable.strings
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
Localizable.strings (English)
|
||||
Keys shared by App and Keyboard extension
|
||||
*/
|
||||
|
||||
"perm_title_enable" = "Enable Keyboard";
|
||||
"perm_steps" = "1 Enable Keyboard > 2 Allow Full Access";
|
||||
"perm_open_settings" = "Open in Settings";
|
||||
"perm_help" = "Can't find the keyboard? Go to Settings > General > Keyboard > Keyboards > Add New Keyboard";
|
||||
|
||||
// Home page & language test
|
||||
"home_title" = "Home";
|
||||
"home_input_placeholder" = "Type here to test the keyboard";
|
||||
"home_item_lang_test" = "Language Test";
|
||||
|
||||
"lang_test_title" = "Language Test";
|
||||
"lang_toggle" = "Toggle Language";
|
||||
"current_lang" = "Current: %@";
|
||||
18
Shared/Localization/zh-Hans.lproj/Localizable.strings
Normal file
18
Shared/Localization/zh-Hans.lproj/Localizable.strings
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
Localizable.strings (简体中文)
|
||||
App 与键盘扩展共用的文案 Key
|
||||
*/
|
||||
|
||||
"perm_title_enable" = "启用输入法";
|
||||
"perm_steps" = "1 开启键盘 > 2 允许完全访问";
|
||||
"perm_open_settings" = "去设置中开启";
|
||||
"perm_help" = "没有找到键盘? 请前往 设置 > 通用 > 键盘 > 键盘 > 添加新键盘";
|
||||
|
||||
// 首页与多语言测试
|
||||
"home_title" = "首页";
|
||||
"home_input_placeholder" = "在此输入,测试键盘";
|
||||
"home_item_lang_test" = "多语言测试";
|
||||
|
||||
"lang_test_title" = "多语言测试";
|
||||
"lang_toggle" = "切换语言";
|
||||
"current_lang" = "当前:%@";
|
||||
Reference in New Issue
Block a user