From 1fc1e3247bfdd1f8d350425c1a1990b0ad31ef7c Mon Sep 17 00:00:00 2001 From: phlpsong <103433299+phlpsong@users.noreply.github.com> Date: Fri, 5 Jan 2024 19:48:33 +0800 Subject: [PATCH] bugfix: app language not follow language updates in system settings (#306) * perf: use NSLocale.preferredLanguages instead of NSUserDefaults AppleLanguages * bugfix: #305 reset AppleLanguages values --------- Co-authored-by: tisfeng --- Easydict/App/AppDelegate.m | 2 - .../Service/Language/EZLanguageManager.m | 41 +++++++++++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Easydict/App/AppDelegate.m b/Easydict/App/AppDelegate.m index df4b5ae8a..8aa9e5f27 100644 --- a/Easydict/App/AppDelegate.m +++ b/Easydict/App/AppDelegate.m @@ -34,8 +34,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [EZLog setupCrashLogService]; [EZLog logAppInfo]; - [self setupAppLanguage]; - if (!EasydictNewAppManager.shared.enable) { [EZMenuItemManager.shared setup]; } diff --git a/Easydict/Feature/Service/Language/EZLanguageManager.m b/Easydict/Feature/Service/Language/EZLanguageManager.m index 4d365d972..f2e430349 100644 --- a/Easydict/Feature/Service/Language/EZLanguageManager.m +++ b/Easydict/Feature/Service/Language/EZLanguageManager.m @@ -28,7 +28,7 @@ @implementation EZLanguageManager static EZLanguageManager *_instance; + (instancetype)shared { - @synchronized (self) { + @synchronized(self) { if (!_instance) { _instance = [[super allocWithZone:NULL] init]; [_instance setup]; @@ -46,6 +46,9 @@ + (void)destroySharedInstance { } - (void)setup { + // Ref: https://stackoverflow.com/a/25011408 + // A workaround of `AppleLanguages` not refresh once inserted object + [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"]; NSArray *showingLanguages = [EZLanguageManager.shared allLanguages]; self.allLanguageFlagDict = [[MMOrderedDictionary alloc] init]; for (EZLanguage language in showingLanguages) { @@ -66,23 +69,37 @@ - (void)setup { [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] is the same with [NSLocale preferredLanguages] generally, but it can be modified. Changing the system language does not seem to take effect immediately and may require a reboot of the computer. + + Ref https://stackoverflow.com/a/4221416/8378840 + !!!: For Canadian English, AppleLanguages returns "en_CA", while [NSLocale preferredLanguages] returns "en-CA" */ - // NSArray *preferredLanguages = [NSLocale preferredLanguages]; - NSArray *preferredLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; + NSArray *preferredLanguages = [NSLocale preferredLanguages]; + // NSArray *preferredLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; + + // Chinese (Hong kong) --> zh-HK + // Chinese, Traditional --> zh-Hant MMLogInfo(@"AppleLanguages: %@", preferredLanguages); NSMutableArray *languages = [NSMutableArray array]; for (NSString *language in preferredLanguages) { - NSMutableArray *array = [NSMutableArray arrayWithArray:[language componentsSeparatedByString:@"-"]]; - // Remove country code - [array removeLastObject]; - NSString *languageCode = [array componentsJoinedByString:@"-"]; + // "zh-Hans-CN" + NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language]; + NSString *languageCode = [languageDic objectForKey:NSLocaleLanguageCode]; // zh + NSString *scriptCode = [languageDic objectForKey:NSLocaleScriptCode]; // Hans + // NSString *countryCode = [languageDic objectForKey:NSLocaleCountryCode]; // CN + + NSString *languageScriptCode = languageCode; + if (scriptCode) { + // Only some special languages have script code, such as zh-Hans, zh-Hant. + languageScriptCode = [NSString stringWithFormat:@"%@-%@", languageCode, scriptCode]; + } + // Convert to EZLanguage - EZLanguage ezLanguage = [EZAppleService.shared languageEnumFromAppleLanguage:languageCode]; + EZLanguage ezLanguage = [EZAppleService.shared languageEnumFromAppleLanguage:languageScriptCode]; - // handle "zh-CN" - if ([languageCode hasPrefix:@"zh"] && [ezLanguage isEqualToString:EZLanguageAuto]) { + // Handle "zh-CN" + if ([languageScriptCode hasPrefix:@"zh"] && [ezLanguage isEqualToString:EZLanguageAuto]) { ezLanguage = EZLanguageSimplifiedChinese; } @@ -126,7 +143,7 @@ - (void)setup { NSMutableArray *preferredLanguages = [NSMutableArray array]; [preferredLanguages addObjectsFromArray:self.userPreferredTwoLanguages]; [preferredLanguages addObjectsFromArray:self.systemPreferredLanguages]; - + NSMutableArray *languages = [NSMutableArray array]; for (EZLanguage language in preferredLanguages) { if (![languages containsObject:language]) { @@ -148,7 +165,7 @@ - (EZLanguage)systemSecondLanguage { - (NSArray *)userPreferredTwoLanguages { - NSArray *twoLanguages = @[self.userFirstLanguage, self.userSecondLanguage]; + NSArray *twoLanguages = @[ self.userFirstLanguage, self.userSecondLanguage ]; return twoLanguages; }