语言逻辑处理

我的项目里有5个国家的语言,如果用户在app里手动切换了国家语言,只要不卸载app,用户在手机设置切换语言,app的语言不要变;如果app被删
  除,重新安装,app语言要跟随手机设置的语言(如果语言对不上,app就显示英语)
  用户没有在app里手动设置过国家语言,用户在手机设置界面切换国家,app要跟随手机设置的语言(如果语言对不上,app就显示英语)。
This commit is contained in:
2026-03-05 17:42:50 +08:00
parent d8a84dc478
commit eaf512be7f
9 changed files with 530 additions and 55 deletions

View File

@@ -16,6 +16,7 @@
#import "KBKeyBoardMainView.h"
#import "KBKeyboardSubscriptionView.h"
#import "KBLocalizationManager.h"
#import "KBSettingView.h"
#import "KBSkinManager.h"
#import "KBSkinInstallBridge.h"
#import "KBSuggestionEngine.h"
@@ -96,6 +97,19 @@ static NSString *KBFormatMB(uint64_t bytes) {
}
[self kb_applyTheme];
}];
// UIApp = App
self.kb_localizationObserverToken = [[NSNotificationCenter defaultCenter]
addObserverForName:KBLocalizationDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(__unused NSNotification *_Nonnull note) {
__strong typeof(weakSelf) self = weakSelf;
if (!self) {
return;
}
[self kb_reloadUIForLocalizationChange];
}];
[self kb_applyTheme];
[self kb_registerDarwinSkinInstallObserver];
[self kb_consumePendingShopSkin];
@@ -239,6 +253,11 @@ static NSString *KBFormatMB(uint64_t bytes) {
removeObserver:self.kb_skinObserverToken];
self.kb_skinObserverToken = nil;
}
if (self.kb_localizationObserverToken) {
[[NSNotificationCenter defaultCenter]
removeObserver:self.kb_localizationObserverToken];
self.kb_localizationObserverToken = nil;
}
[self kb_stopObservingAppGroupChanges];
[self kb_unregisterDarwinSkinInstallObserver];
#if DEBUG
@@ -250,6 +269,57 @@ static NSString *KBFormatMB(uint64_t bytes) {
#endif
}
#pragma mark - Localization
- (void)kb_reloadUIForLocalizationChange {
if (![NSThread isMainThread]) {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf kb_reloadUIForLocalizationChange];
});
return;
}
//
KBKeyboardPanelMode targetMode = self.kb_panelMode;
// 使 profileId profile
_kb_lastLoadedProfileId = nil;
// /init
if (_keyBoardMainView) {
[_keyBoardMainView removeFromSuperview];
_keyBoardMainView = nil;
}
self.keyBoardMainHeightConstraint = nil;
if (_functionView) {
[_functionView removeFromSuperview];
_functionView = nil;
}
if (_settingView) {
[_settingView removeFromSuperview];
_settingView = nil;
}
if (_subscriptionView) {
[_subscriptionView removeFromSuperview];
_subscriptionView = nil;
}
if (_chatPanelView) {
[_chatPanelView removeFromSuperview];
_chatPanelView = nil;
}
self.chatPanelVisible = NO;
self.chatPanelHeightConstraint = nil;
// Main kb_setPanelMode return
self.kb_panelMode = KBKeyboardPanelModeMain;
[self kb_setPanelMode:targetMode animated:NO];
// /profile App
[self kb_checkAndApplyLayoutIfNeeded];
[KBHUD setContainerView:self.view];
[self kb_applyTheme];
}
#pragma mark - Layout Switching
- (void)kb_checkAndApplyLayoutIfNeeded {