语言逻辑处理
我的项目里有5个国家的语言,如果用户在app里手动切换了国家语言,只要不卸载app,用户在手机设置切换语言,app的语言不要变;如果app被删 除,重新安装,app语言要跟随手机设置的语言(如果语言对不上,app就显示英语) 用户没有在app里手动设置过国家语言,用户在手机设置界面切换国家,app要跟随手机设置的语言(如果语言对不上,app就显示英语)。
This commit is contained in:
@@ -25,8 +25,11 @@
|
||||
#import "KBUserSessionManager.h"
|
||||
#import "KBLoginVC.h"
|
||||
#import "KBConfig.h"
|
||||
#import "KBLocalizationManager.h"
|
||||
#import "KBSkinInstallBridge.h"
|
||||
#import "KBSkinManager.h"
|
||||
#import "KBAppUpdateView.h"
|
||||
#import "KBInputProfileManager.h"
|
||||
|
||||
static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
||||
|
||||
@@ -46,6 +49,10 @@ static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||
/// 1:配置国际化(统一使用集中管理的语言列表)
|
||||
[KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
|
||||
/// 1.1:首次启动为键盘写入默认输入配置(语言/布局/profile),保证“App 语言=键盘语言”
|
||||
[self kb_bootstrapDefaultKeyboardProfileIfNeeded];
|
||||
/// 1.2:未手动选择键盘输入配置时,让键盘配置随 App 语言变化
|
||||
[self kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded];
|
||||
/// 2 : 处理token问题(包括卸载重装场景下的 token 清理)
|
||||
[[KBUserSessionManager shared] bootstrapIfNeeded];
|
||||
|
||||
@@ -73,6 +80,12 @@ static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
||||
|
||||
// 安装默认皮肤(首次安装时)
|
||||
[self kb_installDefaultSkinIfNeeded];
|
||||
|
||||
// App 语言变化(系统跟随或手动切换)时,同步键盘配置并让键盘即时刷新
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(kb_onLocalizationChanged:)
|
||||
name:KBLocalizationDidChangeNotification
|
||||
object:nil];
|
||||
|
||||
[self kb_requestAppUpdateAndPresentIfNeeded];
|
||||
|
||||
@@ -87,6 +100,119 @@ static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)kb_defaultKeyboardLanguageCodeForAppLanguageCode:(NSString *)appLanguageCode {
|
||||
NSString *lc = appLanguageCode.lowercaseString ?: @"";
|
||||
if ([lc hasPrefix:@"es"]) { return @"es"; }
|
||||
if ([lc hasPrefix:@"pt"]) { return @"pt"; }
|
||||
if ([lc hasPrefix:@"id"]) { return @"id"; }
|
||||
if ([lc hasPrefix:@"zh-hant"]) { return @"zh-Hant-Pinyin"; }
|
||||
return @"en";
|
||||
}
|
||||
|
||||
- (void)kb_bootstrapDefaultKeyboardProfileIfNeeded {
|
||||
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
||||
NSString *savedProfileId = [shared stringForKey:AppGroup_SelectedKeyboardProfileId];
|
||||
if (savedProfileId.length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
||||
NSString *kbLanguageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
||||
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:kbLanguageCode];
|
||||
if (!profile) {
|
||||
profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:@"en"];
|
||||
}
|
||||
KBInputProfileLayout *layout = profile.layouts.firstObject;
|
||||
if (!layout) {
|
||||
return;
|
||||
}
|
||||
|
||||
[shared setObject:(profile.code ?: @"en") forKey:AppGroup_SelectedKeyboardLanguageCode];
|
||||
[shared setObject:(layout.variant ?: @"qwerty") forKey:AppGroup_SelectedKeyboardLayoutVariant];
|
||||
[shared setObject:(layout.profileId ?: @"en_US_qwerty") forKey:AppGroup_SelectedKeyboardProfileId];
|
||||
[shared synchronize];
|
||||
|
||||
NSLog(@"[AppDelegate] Bootstrap keyboard profile: appLang=%@ kbLang=%@ profileId=%@ variant=%@",
|
||||
appLanguageCode, profile.code, layout.profileId, layout.variant);
|
||||
}
|
||||
|
||||
- (void)kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded {
|
||||
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
||||
if ([shared boolForKey:AppGroup_DidUserSelectKeyboardProfile]) {
|
||||
return;
|
||||
}
|
||||
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
||||
NSString *kbLanguageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
||||
|
||||
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:kbLanguageCode];
|
||||
if (!profile) {
|
||||
profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:@"en"];
|
||||
}
|
||||
KBInputProfileLayout *layout = profile.layouts.firstObject;
|
||||
if (!layout) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *curProfileId = [shared stringForKey:AppGroup_SelectedKeyboardProfileId] ?: @"";
|
||||
NSString *curLang = [shared stringForKey:AppGroup_SelectedKeyboardLanguageCode] ?: @"";
|
||||
NSString *curVariant = [shared stringForKey:AppGroup_SelectedKeyboardLayoutVariant] ?: @"";
|
||||
if ([curProfileId isEqualToString:layout.profileId ?: @""] &&
|
||||
[curLang isEqualToString:profile.code ?: @""] &&
|
||||
[curVariant isEqualToString:layout.variant ?: @""]) {
|
||||
return;
|
||||
}
|
||||
|
||||
[shared setObject:(profile.code ?: @"en") forKey:AppGroup_SelectedKeyboardLanguageCode];
|
||||
[shared setObject:(layout.variant ?: @"qwerty") forKey:AppGroup_SelectedKeyboardLayoutVariant];
|
||||
[shared setObject:(layout.profileId ?: @"en_US_qwerty") forKey:AppGroup_SelectedKeyboardProfileId];
|
||||
[shared synchronize];
|
||||
|
||||
NSLog(@"[AppDelegate] Sync keyboard profile to appLang=%@ kbLang=%@ profileId=%@ variant=%@",
|
||||
appLanguageCode, profile.code, layout.profileId, layout.variant);
|
||||
|
||||
// 同步完键盘语言后,如当前是默认皮肤,则让皮肤也跟随(避免 key_icons 仍是旧语言导致看起来“还是繁体”)
|
||||
[self kb_applyDefaultSkinForKeyboardLanguageIfNeeded:profile.code ?: @"en"];
|
||||
}
|
||||
|
||||
- (void)kb_onLocalizationChanged:(NSNotification *)note {
|
||||
[self kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded];
|
||||
}
|
||||
|
||||
- (void)kb_applyDefaultSkinForKeyboardLanguageIfNeeded:(NSString *)languageCode {
|
||||
NSString *lc = languageCode.length > 0 ? languageCode : @"en";
|
||||
NSString *targetSkinId = [NSString stringWithFormat:@"bundle_skin_default_%@", lc];
|
||||
NSString *currentSkinId = [KBSkinManager shared].current.skinId ?: @"";
|
||||
BOOL isDefaultLike = (currentSkinId.length == 0 ||
|
||||
[currentSkinId isEqualToString:@"default"] ||
|
||||
[currentSkinId hasPrefix:@"bundle_skin_default_"]);
|
||||
if (!isDefaultLike) {
|
||||
return;
|
||||
}
|
||||
if ([currentSkinId isEqualToString:targetSkinId] && [KBSkinManager kb_hasAssetsForSkinId:targetSkinId]) {
|
||||
return;
|
||||
}
|
||||
|
||||
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:lc];
|
||||
NSString *zipName = profile.defaultSkinZip.length > 0 ? profile.defaultSkinZip : @"normal_them.zip";
|
||||
NSDictionary<NSString *, NSString *> *iconShortNames = [KBSkinInstallBridge iconShortNamesForLanguageCode:lc];
|
||||
|
||||
NSLog(@"[AppDelegate] Applying default skin for keyboard language=%@ currentSkin=%@ targetSkin=%@ zip=%@",
|
||||
lc, currentSkinId, targetSkinId, zipName);
|
||||
|
||||
[KBSkinInstallBridge publishBundleSkinRequestWithId:targetSkinId
|
||||
name:targetSkinId
|
||||
zipName:zipName
|
||||
iconShortNames:iconShortNames];
|
||||
[KBSkinInstallBridge consumePendingRequestFromBundle:[NSBundle mainBundle]
|
||||
completion:^(BOOL success, NSError * _Nullable error) {
|
||||
if (!success) {
|
||||
NSLog(@"[AppDelegate] Apply default skin failed: %@", error);
|
||||
} else {
|
||||
NSLog(@"[AppDelegate] Apply default skin success: %@", targetSkinId);
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)kb_requestAppUpdateAndPresentIfNeeded {
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[[KBLoginVM shared] checkAppUpdateWithCompletion:^(KBAppUpdateInfo * _Nullable info, NSError * _Nullable error) {
|
||||
@@ -545,36 +671,14 @@ static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
||||
}
|
||||
|
||||
- (void)kb_installDefaultSkinIfNeeded {
|
||||
static NSString *const kKBDefaultSkinInstalledKey = @"KBDefaultSkinInstalled";
|
||||
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
|
||||
|
||||
if ([ud boolForKey:kKBDefaultSkinInstalledKey]) {
|
||||
NSLog(@"[AppDelegate] Default skin already installed, skip");
|
||||
return;
|
||||
NSLog(@"[AppDelegate] Installing/applying default skin if needed...");
|
||||
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
||||
NSString *languageCode = [shared stringForKey:AppGroup_SelectedKeyboardLanguageCode];
|
||||
if (languageCode.length == 0) {
|
||||
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
||||
languageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
||||
}
|
||||
|
||||
NSLog(@"[AppDelegate] Installing default skin for first launch...");
|
||||
|
||||
NSString *skinId = @"bundle_skin_default_en";
|
||||
NSString *zipName = @"normal_them.zip";
|
||||
|
||||
NSDictionary<NSString *, NSString *> *iconShortNames = [KBSkinInstallBridge iconShortNamesForLanguageCode:@"en"];
|
||||
|
||||
[KBSkinInstallBridge publishBundleSkinRequestWithId:skinId
|
||||
name:skinId
|
||||
zipName:zipName
|
||||
iconShortNames:iconShortNames];
|
||||
|
||||
[KBSkinInstallBridge consumePendingRequestFromBundle:[NSBundle mainBundle]
|
||||
completion:^(BOOL success, NSError * _Nullable error) {
|
||||
if (success) {
|
||||
NSLog(@"[AppDelegate] Default skin installed successfully");
|
||||
[ud setBool:YES forKey:kKBDefaultSkinInstalledKey];
|
||||
[ud synchronize];
|
||||
} else {
|
||||
NSLog(@"[AppDelegate] Default skin install failed: %@", error);
|
||||
}
|
||||
}];
|
||||
[self kb_applyDefaultSkinForKeyboardLanguageIfNeeded:languageCode ?: @"en"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -630,6 +630,7 @@ typedef void(^KBInputProfileSelectHandler)(NSString *languageCode, NSString *lay
|
||||
[shared setObject:languageCode forKey:AppGroup_SelectedKeyboardLanguageCode];
|
||||
[shared setObject:layoutVariant forKey:AppGroup_SelectedKeyboardLayoutVariant];
|
||||
[shared setObject:profileId forKey:AppGroup_SelectedKeyboardProfileId];
|
||||
[shared setBool:YES forKey:AppGroup_DidUserSelectKeyboardProfile];
|
||||
[shared synchronize];
|
||||
|
||||
[[KBLocalizationManager shared] setCurrentLanguageCode:languageCode persist:YES];
|
||||
|
||||
Reference in New Issue
Block a user