Skip to content

Commit

Permalink
bugfix: app language not follow language updates in system settings (#…
Browse files Browse the repository at this point in the history
…306)

* perf: use NSLocale.preferredLanguages instead of NSUserDefaults AppleLanguages

* bugfix: #305 reset AppleLanguages values

---------

Co-authored-by: tisfeng <[email protected]>
  • Loading branch information
phlpsong and tisfeng committed Jan 6, 2024
1 parent 8a0df85 commit 1fc1e32
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
2 changes: 0 additions & 2 deletions Easydict/App/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[EZLog setupCrashLogService];
[EZLog logAppInfo];

[self setupAppLanguage];

if (!EasydictNewAppManager.shared.enable) {
[EZMenuItemManager.shared setup];
}
Expand Down
41 changes: 29 additions & 12 deletions Easydict/Feature/Service/Language/EZLanguageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ @implementation EZLanguageManager
static EZLanguageManager *_instance;

+ (instancetype)shared {
@synchronized (self) {
@synchronized(self) {
if (!_instance) {
_instance = [[super allocWithZone:NULL] init];
[_instance setup];
Expand All @@ -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) {
Expand All @@ -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<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
NSArray<NSString *> *preferredLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
// NSArray<NSString *> *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;
}

Expand Down Expand Up @@ -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]) {
Expand All @@ -148,7 +165,7 @@ - (EZLanguage)systemSecondLanguage {


- (NSArray<EZLanguage> *)userPreferredTwoLanguages {
NSArray *twoLanguages = @[self.userFirstLanguage, self.userSecondLanguage];
NSArray *twoLanguages = @[ self.userFirstLanguage, self.userSecondLanguage ];
return twoLanguages;
}

Expand Down

0 comments on commit 1fc1e32

Please sign in to comment.