Files
keyboard/Shared/KBLocalizationManager.m

475 lines
22 KiB
Mathematica
Raw Normal View History

2025-11-03 16:37:28 +08:00
//
// KBLocalizationManager.m
//
//
#import "KBLocalizationManager.h"
#import <Security/Security.h>
#import "KBConfig.h"
// Darwin App <->
static NSString * const kKBDarwinLocalizationChanged = @"com.loveKey.nyx.loc.changed";
2025-12-02 20:33:17 +08:00
///
KBLanguageCode const KBLanguageCodeEnglish = @"en";
2026-03-02 09:19:06 +08:00
KBLanguageCode const KBLanguageCodeTraditionalChinese = @"zh-Hant";
KBLanguageCode const KBLanguageCodeSpanish = @"es";
2026-03-04 19:49:07 +08:00
KBLanguageCode const KBLanguageCodePortuguese = @"pt-PT";
2026-03-02 09:19:06 +08:00
KBLanguageCode const KBLanguageCodeIndonesian = @"id";
2025-12-02 20:33:17 +08:00
///
NSArray<KBLanguageCode> *KBDefaultSupportedLanguageCodes(void) {
static NSArray<KBLanguageCode> *codes;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
2026-03-02 09:19:06 +08:00
codes = @[
KBLanguageCodeEnglish,
KBLanguageCodeSpanish,
2026-03-04 21:16:21 +08:00
KBLanguageCodeIndonesian,
2026-03-02 09:19:06 +08:00
KBLanguageCodePortuguese,
2026-03-04 21:16:21 +08:00
KBLanguageCodeTraditionalChinese
2026-03-02 09:19:06 +08:00
];
2025-12-02 20:33:17 +08:00
});
return codes;
}
2025-11-03 16:37:28 +08:00
///
NSString * const KBLocalizationDidChangeNotification = @"KBLocalizationDidChangeNotification";
// Target
static NSString * const kKBLocService = @"com.loveKey.nyx.loc";
2025-11-03 16:37:28 +08:00
static NSString * const kKBLocAccount = @"lang"; // UTF8
// / App 使 UserDefaults Keychain
static NSString * const kKBLocDidLaunchKey = @"com.loveKey.nyx.loc.did_launch";
// App App Group Keychain
static NSString * const kKBLocAppBootstrappedKey = @"com.loveKey.nyx.loc.app_bootstrapped";
// App App Group
static NSString * const kKBLocUserSelectedLanguageKey = @"com.loveKey.nyx.loc.user_selected_language";
2025-11-03 16:37:28 +08:00
@interface KBLocalizationManager ()
2025-12-02 20:33:17 +08:00
@property (nonatomic, copy, readwrite) KBLanguageCode currentLanguageCode; //
2025-11-03 16:37:28 +08:00
@property (nonatomic, strong) NSBundle *langBundle; // .lproj
- (NSString *)normalizeLanguageCode:(NSString *)code;
- (NSString *)supportedLanguageCodeForCandidate:(nullable NSString *)code;
- (void)kb_onSystemLocaleMaybeChanged:(NSNotification *)note;
2025-11-03 16:37:28 +08:00
@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;
}
static void KBLocDarwinCallback(CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo) {
// reloadApp resolved==current short-circuit
dispatch_async(dispatch_get_main_queue(), ^{
KBLocalizationManager *m = [KBLocalizationManager shared];
[m reloadFromSharedStorageIfNeeded];
});
}
2025-11-03 16:37:28 +08:00
@implementation KBLocalizationManager
/// App Extension
/// App Keychain App Keychain
+ (BOOL)kb_isRunningInAppExtension {
return ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSExtension"] != nil);
}
+ (NSUserDefaults *)kb_appGroupUserDefaults {
NSUserDefaults *ud = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
return ud ?: [NSUserDefaults standardUserDefaults];
}
+ (BOOL)kb_isAppBootstrapped {
// App Group App
return [[self kb_appGroupUserDefaults] boolForKey:kKBLocAppBootstrappedKey];
}
+ (void)kb_markAppBootstrappedIfNeeded {
NSUserDefaults *ud = [self kb_appGroupUserDefaults];
if ([ud boolForKey:kKBLocAppBootstrappedKey]) { return; }
[ud setBool:YES forKey:kKBLocAppBootstrappedKey];
[ud synchronize];
}
+ (BOOL)kb_userDidSelectLanguage {
NSUserDefaults *ud = [self kb_appGroupUserDefaults];
return [ud boolForKey:kKBLocUserSelectedLanguageKey];
}
+ (void)kb_setUserDidSelectLanguage:(BOOL)selected {
NSUserDefaults *ud = [self kb_appGroupUserDefaults];
[ud setBool:selected forKey:kKBLocUserSelectedLanguageKey];
[ud synchronize];
}
2025-11-03 16:37:28 +08:00
+ (instancetype)shared {
static KBLocalizationManager *m; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
m = [KBLocalizationManager new];
//
2025-12-02 20:33:17 +08:00
m.supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
BOOL isAppExtension = [[self class] kb_isRunningInAppExtension];
BOOL wasAppBootstrapped = [[self class] kb_isAppBootstrapped];
BOOL isAppBootstrapped = wasAppBootstrapped;
if (!isAppExtension) {
// App
[[self class] kb_markAppBootstrappedIfNeeded];
isAppBootstrapped = YES;
2025-11-03 16:37:28 +08:00
}
// 使 App Group App Group Keychain
// App Keychain
BOOL isFreshInstall = (!isAppExtension && !wasAppBootstrapped);
if (isFreshInstall) {
[[self class] kc_write:nil];
// App Group
[[self class] kb_setUserDidSelectLanguage:NO];
}
// UserDefaults App
BOOL isFirstLaunch = (!isAppExtension) ? [[self class] kb_markLaunchedAndReturnIsFirstLaunch] : NO;
//
// 1) App -> Keychain Target
// 2) -> / Keychain
BOOL userSelected = [[self class] kb_userDidSelectLanguage];
NSString *saved = nil;
if (userSelected) {
// App Keychain
if (!(isAppExtension && !isAppBootstrapped)) {
saved = (isFreshInstall ? nil : [[self class] kc_read]);
}
} else {
// Keychain
if (!isAppExtension) {
NSString *legacy = [[self class] kc_read];
if (legacy.length > 0) {
[[self class] kc_write:nil];
}
}
}
#if DEBUG
NSLog(@"[KBLoc] init bundle=%@ isExt=%d appBoot=%d wasBoot=%d fresh=%d firstLaunch=%d userSelected=%d preferred=%@ keychainSaved=%@ supported=%@",
NSBundle.mainBundle.bundleIdentifier ?: @"",
isAppExtension, isAppBootstrapped, wasAppBootstrapped, isFreshInstall, isFirstLaunch,
userSelected,
[NSLocale preferredLanguages],
saved ?: @"(nil)",
m.supportedLanguageCodes ?: @[]);
#endif
// 5
NSString *resolved = [m supportedLanguageCodeForCandidate:saved];
if (saved.length > 0 && resolved.length == 0) {
// 退
[[self class] kc_write:nil];
[[self class] kb_setUserDidSelectLanguage:NO];
saved = nil;
resolved = @"";
}
if (resolved.length == 0) {
resolved = [m bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: KBLanguageCodeEnglish;
}
[m applyLanguage:resolved];
#if DEBUG
NSLog(@"[KBLoc] resolved=%@ current=%@ bundle=%@",
resolved ?: @"",
m.currentLanguageCode ?: @"",
m.langBundle.bundlePath ?: @"");
#endif
// Darwin App ->
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
(__bridge const void *)(m),
KBLocDarwinCallback,
(__bridge CFStringRef)kKBDarwinLocalizationChanged,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
// /
[[NSNotificationCenter defaultCenter] addObserver:m
selector:@selector(kb_onSystemLocaleMaybeChanged:)
name:NSCurrentLocaleDidChangeNotification
object:nil];
2025-11-03 16:37:28 +08:00
});
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]; }
}
2025-12-02 20:33:17 +08:00
_supportedLanguageCodes = set.array.count ? (NSArray<KBLanguageCode> *)set.array : @[ KBLanguageCodeEnglish ];
2025-11-03 16:37:28 +08:00
// 广
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; //
// zh-Hans退 en
NSString *resolved = [self supportedLanguageCodeForCandidate:code];
if (resolved.length == 0) {
resolved = [self bestSupportedLanguageForPreferred:@[code]] ?: (self.supportedLanguageCodes.firstObject ?: KBLanguageCodeEnglish);
}
if ([resolved isEqualToString:self.currentLanguageCode]) return; //
[self applyLanguage:resolved];
if (persist) {
[[self class] kb_setUserDidSelectLanguage:YES];
[[self class] kc_write:self.currentLanguageCode]; // App/
//
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(__bridge CFStringRef)kKBDarwinLocalizationChanged,
NULL, NULL, true);
}
2025-11-03 16:37:28 +08:00
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
}
- (void)resetToSystemLanguage {
[[self class] kb_setUserDidSelectLanguage:NO];
[[self class] kc_write:nil];
2025-12-02 20:33:17 +08:00
NSString *best = [self bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: KBLanguageCodeEnglish;
if ([best isEqualToString:self.currentLanguageCode]) {
return;
}
[self applyLanguage:best];
//
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(__bridge CFStringRef)kKBDarwinLocalizationChanged,
NULL, NULL, true);
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
2025-11-03 16:37:28 +08:00
}
- (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 {
2025-12-02 20:33:17 +08:00
if (self.supportedLanguageCodes.count == 0) return KBLanguageCodeEnglish;
// 使
// 5 退
NSString *primary = preferred.firstObject ?: @"";
if (primary.length == 0) {
for (NSString *s in self.supportedLanguageCodes) {
if ([s.lowercaseString isEqualToString:@"en"]) { return s; }
}
return self.supportedLanguageCodes.firstObject ?: KBLanguageCodeEnglish;
}
NSArray<NSString *> *primaryOnly = @[ primary ];
2025-11-03 16:37:28 +08:00
// 1)
for (NSString *p in primaryOnly) {
2025-11-03 16:37:28 +08:00
NSString *pLC = p.lowercaseString;
for (NSString *s in self.supportedLanguageCodes) {
if ([pLC isEqualToString:s.lowercaseString]) { return s; }
}
}
2026-03-04 21:16:21 +08:00
// 2) en-GB -> en, es-MX -> es
for (NSString *p in primaryOnly) {
2025-11-03 16:37:28 +08:00
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;
}
}
}
2026-03-04 21:16:21 +08:00
// 2.5) pt-BR / pt-PT / pt -> pt-PT
for (NSString *p in primaryOnly) {
2025-11-03 16:37:28 +08:00
NSString *pLC = p.lowercaseString;
2026-03-04 21:16:21 +08:00
if ([pLC isEqualToString:@"pt"] || [pLC hasPrefix:@"pt-"] || [pLC hasPrefix:@"pt_"]) {
2025-11-03 16:37:28 +08:00
for (NSString *s in self.supportedLanguageCodes) {
2026-03-04 21:16:21 +08:00
if ([s.lowercaseString isEqualToString:@"pt-pt"]) { return s; }
2025-11-03 16:37:28 +08:00
}
}
2026-03-04 21:16:21 +08:00
}
// 3) zh-Hant/zh-TW/zh-HK zh-Hant
for (NSString *p in primaryOnly) {
2026-03-04 21:16:21 +08:00
NSString *pLC = p.lowercaseString;
if ([pLC hasPrefix:@"zh-hant"] || [pLC hasPrefix:@"zh-tw"] || [pLC hasPrefix:@"zh-hk"]) {
2025-11-03 16:37:28 +08:00
for (NSString *s in self.supportedLanguageCodes) {
2026-03-04 21:16:21 +08:00
if ([s.lowercaseString isEqualToString:@"zh-hant"]) { return s; }
2025-11-03 16:37:28 +08:00
}
}
}
// 4) 退
for (NSString *s in self.supportedLanguageCodes) {
if ([s.lowercaseString isEqualToString:@"en"]) { return s; }
}
2025-12-02 20:33:17 +08:00
return self.supportedLanguageCodes.firstObject ?: KBLanguageCodeEnglish;
2025-11-03 16:37:28 +08:00
}
2026-03-04 21:27:51 +08:00
- (NSString *)currentLanguageHeaderValue {
return self.currentLanguageCode ?: KBLanguageCodeEnglish;
}
2025-11-03 16:37:28 +08:00
#pragma mark -
- (NSString *)normalizeLanguageCode:(NSString *)code {
if (code.length == 0) return @"";
2026-03-04 19:49:07 +08:00
NSString *normalizedCode = code;
2026-03-04 21:16:21 +08:00
NSString *lower = normalizedCode.lowercaseString;
if ([lower isEqualToString:@"pt"] || [lower hasPrefix:@"pt-"] || [lower hasPrefix:@"pt_"]) {
2026-03-04 19:49:07 +08:00
normalizedCode = @"pt-PT";
}
2026-03-04 21:16:21 +08:00
lower = normalizedCode.lowercaseString;
if ([lower hasPrefix:@"zh-hant"] || [lower hasPrefix:@"zh_hant"] || [lower hasPrefix:@"zh-tw"] || [lower hasPrefix:@"zh_hk"]) {
normalizedCode = KBLanguageCodeTraditionalChinese;
}
return normalizedCode;
}
- (NSString *)supportedLanguageCodeForCandidate:(NSString *)code {
NSString *normalized = [self normalizeLanguageCode:code];
if (normalized.length == 0) return @"";
for (NSString *s in self.supportedLanguageCodes) {
if ([s.lowercaseString isEqualToString:normalized.lowercaseString]) {
return s; // /
}
}
return @"";
}
- (void)applyLanguage:(NSString *)code {
NSString *normalizedCode = [self normalizeLanguageCode:code];
2026-03-04 19:49:07 +08:00
_currentLanguageCode = [normalizedCode copy];
2025-11-03 16:37:28 +08:00
// TargetApp bundle .lproj
2026-03-04 19:49:07 +08:00
NSString *path = [NSBundle.mainBundle pathForResource:normalizedCode ofType:@"lproj"];
2025-11-03 16:37:28 +08:00
if (!path) {
// en-GB -> en
2026-03-04 19:49:07 +08:00
NSString *shortCode = [[normalizedCode componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]] firstObject];
2025-11-03 16:37:28 +08:00
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)kb_markLaunchedAndReturnIsFirstLaunch {
// App UserDefaults
// App Group App Keychain
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([ud boolForKey:kKBLocDidLaunchKey]) { return NO; }
[ud setBool:YES forKey:kKBLocDidLaunchKey];
[ud synchronize];
return YES;
}
2025-11-03 16:37:28 +08:00
+ (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;
}
- (void)kb_onSystemLocaleMaybeChanged:(NSNotification *)note {
// reload
[self reloadFromSharedStorageIfNeeded];
}
2025-11-27 15:34:33 +08:00
- (void)reloadFromSharedStorageIfNeeded {
BOOL userSelected = [[self class] kb_userDidSelectLanguage];
BOOL isAppExtension = [[self class] kb_isRunningInAppExtension];
BOOL isAppBootstrapped = [[self class] kb_isAppBootstrapped];
2025-11-27 15:34:33 +08:00
NSString *target = @"";
if (userSelected) {
// App Keychain
if (isAppExtension && !isAppBootstrapped) {
#if DEBUG
NSLog(@"[KBLoc] reload skip (app not bootstrapped yet) bundle=%@ current=%@",
NSBundle.mainBundle.bundleIdentifier ?: @"",
self.currentLanguageCode ?: @"");
#endif
return;
}
NSString *saved = [[self class] kc_read];
if (saved.length == 0) {
//
[[self class] kb_setUserDidSelectLanguage:NO];
} else {
NSString *resolved = [self supportedLanguageCodeForCandidate:saved];
if (resolved.length == 0) {
//
[[self class] kc_write:nil];
[[self class] kb_setUserDidSelectLanguage:NO];
} else {
target = resolved;
}
}
}
if (target.length == 0) {
//
target = [self bestSupportedLanguageForPreferred:[NSLocale preferredLanguages]] ?: KBLanguageCodeEnglish;
}
if ([target isEqualToString:self.currentLanguageCode]) {
return;
}
[self applyLanguage:target];
// App
if (!userSelected && !isAppExtension) {
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(__bridge CFStringRef)kKBDarwinLocalizationChanged,
NULL, NULL, true);
}
[[NSNotificationCenter defaultCenter] postNotificationName:KBLocalizationDidChangeNotification object:nil];
2025-11-27 15:34:33 +08:00
}
2025-11-03 16:37:28 +08:00
@end