From 3002e25eb41a749b7047144f75f1e278b1cef946 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 2 Apr 2020 10:06:06 -0700 Subject: [PATCH] Initialize locale from FlutterEngine (#17473) * Initialize locale from FlutterEngine --- .../ios/framework/Source/FlutterEngine.mm | 48 +++++++++++++++++++ .../framework/Source/FlutterViewController.mm | 48 ------------------- testing/scenario_app/lib/main.dart | 1 + 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index cf636aef8..f5fdebdf7 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -122,6 +122,11 @@ NSString* const FlutterDefaultDartEntrypoint = nil; name:UIApplicationWillResignActiveNotification object:nil]; + [center addObserver:self + selector:@selector(onLocaleUpdated:) + name:NSCurrentLocaleDidChangeNotification + object:nil]; + return self; } @@ -494,6 +499,7 @@ NSString* const FlutterDefaultDartEntrypoint = nil; << entrypoint.UTF8String; } else { [self setupChannels]; + [self onLocaleUpdated:nil]; if (!_platformViewsController) { _platformViewsController.reset(new flutter::FlutterPlatformViewsController()); } @@ -710,6 +716,48 @@ NSString* const FlutterDefaultDartEntrypoint = nil; _isGpuDisabled = value; } +#pragma mark - Locale updates + +- (void)onLocaleUpdated:(NSNotification*)notification { + NSArray* preferredLocales = [NSLocale preferredLanguages]; + 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 + // 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 = [[[NSLocale alloc] initWithLocaleIdentifier:localeID] autorelease]; + NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; + NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; + NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode]; + NSString* variantCode = [currentLocale objectForKey:NSLocaleVariantCode]; + if (!languageCode) { + continue; + } + [data addObject:languageCode]; + [data addObject:(countryCode ? countryCode : @"")]; + [data addObject:(scriptCode ? scriptCode : @"")]; + [data addObject:(variantCode ? variantCode : @"")]; + } + if (data.count == 0) { + return; + } + [self.localizationChannel invokeMethod:@"setLocale" arguments:data]; +} + @end @implementation FlutterEngineRegistrar { diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 7f8ba6b18..d3613713b 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -279,11 +279,6 @@ typedef enum UIAccessibilityContrast : NSInteger { name:UIKeyboardWillHideNotification object:nil]; - [center addObserver:self - selector:@selector(onLocaleUpdated:) - name:NSCurrentLocaleDidChangeNotification - object:nil]; - [center addObserver:self selector:@selector(onAccessibilityStatusChanged:) name:UIAccessibilityVoiceOverStatusChanged @@ -595,7 +590,6 @@ static void sendFakeTouchEvent(FlutterEngine* engine, - (void)viewDidAppear:(BOOL)animated { TRACE_EVENT0("flutter", "viewDidAppear"); - [self onLocaleUpdated:nil]; [self onUserSettingsChanged:nil]; [self onAccessibilityStatusChanged:nil]; [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"]; @@ -1054,48 +1048,6 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) #endif } -#pragma mark - Locale updates - -- (void)onLocaleUpdated:(NSNotification*)notification { - NSArray* preferredLocales = [NSLocale preferredLanguages]; - 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 - // 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 = [[[NSLocale alloc] initWithLocaleIdentifier:localeID] autorelease]; - NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode]; - NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; - NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode]; - NSString* variantCode = [currentLocale objectForKey:NSLocaleVariantCode]; - if (!languageCode) { - continue; - } - [data addObject:languageCode]; - [data addObject:(countryCode ? countryCode : @"")]; - [data addObject:(scriptCode ? scriptCode : @"")]; - [data addObject:(variantCode ? variantCode : @"")]; - } - if (data.count == 0) { - return; - } - [[_engine.get() localizationChannel] invokeMethod:@"setLocale" arguments:data]; -} - #pragma mark - Set user settings - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { diff --git a/testing/scenario_app/lib/main.dart b/testing/scenario_app/lib/main.dart index 0ab3f7c53..17e56934c 100644 --- a/testing/scenario_app/lib/main.dart +++ b/testing/scenario_app/lib/main.dart @@ -43,6 +43,7 @@ Map _scenarios = { Scenario _currentScenario = _scenarios['animated_color_square']; void main() { + assert(window.locale != null); window ..onPlatformMessage = _handlePlatformMessage ..onBeginFrame = _onBeginFrame -- GitLab