From db2b2be6ec09ecdbd87da021f69698476b6f09a2 Mon Sep 17 00:00:00 2001 From: GaryQian Date: Wed, 28 Nov 2018 11:11:02 -0800 Subject: [PATCH 1/4] Use NSLocale::currentLocale for first/default locale to avoid iOS stripping of countryCode. --- .../ios/framework/Source/FlutterViewController.mm | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 544bad0365d96..7a8b1a3cd3511 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -749,17 +749,24 @@ - (void)onMemoryWarning:(NSNotification*)notification { - (void)onLocaleUpdated:(NSNotification*)notification { NSArray* preferredLocales = [NSLocale preferredLanguages]; NSMutableArray* data = [NSMutableArray new]; + bool first = true; for (NSString* localeID in preferredLocales) { - NSLocale* currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeID]; + NSLocale* currentLocale; + if (first) { + currentLocale = [NSLocale currentLocale]; + first = false; + } else { + currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeID]; + } NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode]; NSString* variantCode = [currentLocale objectForKey:NSLocaleVariantCode]; - if (!languageCode || !countryCode) { + if (!languageCode) { continue; } [data addObject:languageCode]; - [data addObject:countryCode]; + [data addObject:(countryCode ? countryCode : @"")]; [data addObject:(scriptCode ? scriptCode : @"")]; [data addObject:(variantCode ? variantCode : @"")]; } From 789f8ee3746bc2b39f489d7c96f786ccd3c0fba0 Mon Sep 17 00:00:00 2001 From: garyqian Date: Wed, 28 Nov 2018 11:22:55 -0800 Subject: [PATCH 2/4] Add explanation of why use a different API for first locale --- .../darwin/ios/framework/Source/FlutterViewController.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 7a8b1a3cd3511..afa882a881b02 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -753,6 +753,8 @@ - (void)onLocaleUpdated:(NSNotification*)notification { for (NSString* localeID in preferredLocales) { NSLocale* currentLocale; if (first) { + // Use a different API for first/default locale as preferredLanguages + // may strip the region/country code from the locales. currentLocale = [NSLocale currentLocale]; first = false; } else { From 2e58639e92138873531abf015c609919df2bb1a0 Mon Sep 17 00:00:00 2001 From: garyqian Date: Wed, 28 Nov 2018 12:39:19 -0800 Subject: [PATCH 3/4] Change currentLocale to prepend to preferredLocales list --- .../framework/Source/FlutterViewController.mm | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index afa882a881b02..164cc19efc419 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -749,17 +749,25 @@ - (void)onMemoryWarning:(NSNotification*)notification { - (void)onLocaleUpdated:(NSNotification*)notification { NSArray* preferredLocales = [NSLocale preferredLanguages]; NSMutableArray* data = [NSMutableArray new]; - bool first = true; + + // Force prepend the [NSLocale currentLocale] to the front of the list + // to ensure we are including the full default locale. preferredLocales + // is not guaranteed to include anything beyond the languageCode. + NSLocale* currentLocale = [NSLocale currentLocale]; + NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; + NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; + NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode]; + NSString* variantCode = [currentLocale objectForKey:NSLocaleVariantCode]; + if (languageCode) { + [data addObject:languageCode]; + [data addObject:(countryCode ? countryCode : @"")]; + [data addObject:(scriptCode ? scriptCode : @"")]; + [data addObject:(variantCode ? variantCode : @"")]; + } + + // Add any secondary locales/languages to the list. for (NSString* localeID in preferredLocales) { - NSLocale* currentLocale; - if (first) { - // Use a different API for first/default locale as preferredLanguages - // may strip the region/country code from the locales. - currentLocale = [NSLocale currentLocale]; - first = false; - } else { - currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeID]; - } + NSLocale* currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeID]; NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode]; From 81eaa79ad172fde34877a5d75cc3ab0b0fd9377e Mon Sep 17 00:00:00 2001 From: garyqian Date: Wed, 28 Nov 2018 12:53:58 -0800 Subject: [PATCH 4/4] Autorelease the allocations --- .../darwin/ios/framework/Source/FlutterViewController.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 164cc19efc419..5a367bdeae6e1 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -748,7 +748,7 @@ - (void)onMemoryWarning:(NSNotification*)notification { - (void)onLocaleUpdated:(NSNotification*)notification { NSArray* preferredLocales = [NSLocale preferredLanguages]; - NSMutableArray* data = [NSMutableArray new]; + NSMutableArray* data = [[NSMutableArray new] autorelease]; // Force prepend the [NSLocale currentLocale] to the front of the list // to ensure we are including the full default locale. preferredLocales @@ -767,7 +767,7 @@ - (void)onLocaleUpdated:(NSNotification*)notification { // Add any secondary locales/languages to the list. for (NSString* localeID in preferredLocales) { - NSLocale* currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeID]; + NSLocale* currentLocale = [[[NSLocale alloc] initWithLocaleIdentifier:localeID] autorelease]; NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode];