Initialize locale from FlutterEngine (flutter/engine#17473)

* Initialize locale from FlutterEngine
This commit is contained in:
Dan Field
2020-04-02 10:06:06 -07:00
committed by GitHub
parent d7bbf2e70c
commit a04ba4f15e
3 changed files with 49 additions and 48 deletions

View File

@@ -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<NSString*>* preferredLocales = [NSLocale preferredLanguages];
NSMutableArray<NSString*>* 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 {

View File

@@ -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<NSString*>* preferredLocales = [NSLocale preferredLanguages];
NSMutableArray<NSString*>* 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 {

View File

@@ -43,6 +43,7 @@ Map<String, Scenario> _scenarios = <String, Scenario>{
Scenario _currentScenario = _scenarios['animated_color_square'];
void main() {
assert(window.locale != null);
window
..onPlatformMessage = _handlePlatformMessage
..onBeginFrame = _onBeginFrame