Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ - (instancetype)initWithName:(NSString*)labelPrefix
name:UIApplicationWillResignActiveNotification
object:nil];

[center addObserver:self
selector:@selector(onLocaleUpdated:)
name:NSCurrentLocaleDidChangeNotification
object:nil];

return self;
}

Expand Down Expand Up @@ -494,6 +499,7 @@ - (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryURI {
<< entrypoint.UTF8String;
} else {
[self setupChannels];
[self onLocaleUpdated:nil];
if (!_platformViewsController) {
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
}
Expand Down Expand Up @@ -710,6 +716,48 @@ - (void)setIsGpuDisabled:(BOOL)value {
_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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,6 @@ - (void)setupNotificationCenterObservers {
name:UIKeyboardWillHideNotification
object:nil];

[center addObserver:self
selector:@selector(onLocaleUpdated:)
name:NSCurrentLocaleDidChangeNotification
object:nil];

[center addObserver:self
selector:@selector(onAccessibilityStatusChanged:)
name:UIAccessibilityVoiceOverStatusChanged
Expand Down Expand Up @@ -595,7 +590,6 @@ - (void)viewWillAppear:(BOOL)animated {

- (void)viewDidAppear:(BOOL)animated {
TRACE_EVENT0("flutter", "viewDidAppear");
[self onLocaleUpdated:nil];
[self onUserSettingsChanged:nil];
[self onAccessibilityStatusChanged:nil];
[[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"];
Expand Down Expand Up @@ -1054,48 +1048,6 @@ - (void)onAccessibilityStatusChanged:(NSNotification*)notification {
#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 {
Expand Down
1 change: 1 addition & 0 deletions testing/scenario_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down