Send locale information in the macOS embedding (flutter/engine#20461)
Queries the system list of user-preferred languages, and sends it to the engine just after starting it up, as well as after any OS locale change. macOS portion of https://github.com/flutter/flutter/issues/45152
This commit is contained in:
@@ -13,11 +13,30 @@
|
||||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
|
||||
#import "flutter/shell/platform/embedder/embedder.h"
|
||||
|
||||
/**
|
||||
* Constructs and returns a FlutterLocale struct corresponding to |locale|, which must outlive
|
||||
* the returned struct.
|
||||
*/
|
||||
static FlutterLocale FlutterLocaleFromNSLocale(NSLocale* locale) {
|
||||
FlutterLocale flutterLocale = {};
|
||||
flutterLocale.struct_size = sizeof(FlutterLocale);
|
||||
flutterLocale.language_code = [[locale objectForKey:NSLocaleLanguageCode] UTF8String];
|
||||
flutterLocale.country_code = [[locale objectForKey:NSLocaleCountryCode] UTF8String];
|
||||
flutterLocale.script_code = [[locale objectForKey:NSLocaleScriptCode] UTF8String];
|
||||
flutterLocale.variant_code = [[locale objectForKey:NSLocaleVariantCode] UTF8String];
|
||||
return flutterLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private interface declaration for FlutterEngine.
|
||||
*/
|
||||
@interface FlutterEngine () <FlutterBinaryMessenger>
|
||||
|
||||
/**
|
||||
* Sends the list of user-preferred locales to the Flutter engine.
|
||||
*/
|
||||
- (void)sendUserLocales;
|
||||
|
||||
/**
|
||||
* Called by the engine to make the context the engine should draw into current.
|
||||
*/
|
||||
@@ -181,6 +200,12 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
|
||||
_textures = [[NSMutableDictionary alloc] init];
|
||||
_allowHeadlessExecution = allowHeadlessExecution;
|
||||
|
||||
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter addObserver:self
|
||||
selector:@selector(sendUserLocales)
|
||||
name:NSCurrentLocaleDidChangeNotification
|
||||
object:nil];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -254,6 +279,7 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
|
||||
return NO;
|
||||
}
|
||||
|
||||
[self sendUserLocales];
|
||||
[self updateWindowMetrics];
|
||||
return YES;
|
||||
}
|
||||
@@ -314,6 +340,29 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
|
||||
|
||||
#pragma mark - Private methods
|
||||
|
||||
- (void)sendUserLocales {
|
||||
if (!self.running) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a list of FlutterLocales corresponding to the preferred languages.
|
||||
NSMutableArray<NSLocale*>* locales = [NSMutableArray array];
|
||||
std::vector<FlutterLocale> flutterLocales;
|
||||
flutterLocales.reserve(locales.count);
|
||||
for (NSString* localeID in [NSLocale preferredLanguages]) {
|
||||
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:localeID];
|
||||
[locales addObject:locale];
|
||||
flutterLocales.push_back(FlutterLocaleFromNSLocale(locale));
|
||||
}
|
||||
// Convert to a list of pointers, and send to the engine.
|
||||
std::vector<const FlutterLocale*> flutterLocaleList;
|
||||
flutterLocaleList.reserve(flutterLocales.size());
|
||||
std::transform(
|
||||
flutterLocales.begin(), flutterLocales.end(), std::back_inserter(flutterLocaleList),
|
||||
[](const auto& arg) -> const auto* { return &arg; });
|
||||
FlutterEngineUpdateLocales(_engine, flutterLocaleList.data(), flutterLocaleList.size());
|
||||
}
|
||||
|
||||
- (bool)engineCallbackOnMakeCurrent {
|
||||
if (!_mainOpenGLContext) {
|
||||
return false;
|
||||
|
||||
@@ -303,6 +303,7 @@ static void CommonInit(FlutterViewController* controller) {
|
||||
}
|
||||
// Send the initial user settings such as brightness and text scale factor
|
||||
// to the engine.
|
||||
// TODO(stuartmorgan): Move this logic to FlutterEngine.
|
||||
[self sendInitialSettings];
|
||||
return YES;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user