Skip to content

Commit 2bc0bfd

Browse files
Send locale information in the macOS embedding (flutter#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 flutter#45152
1 parent debb30e commit 2bc0bfd

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

+49
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,30 @@
1313
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
1414
#import "flutter/shell/platform/embedder/embedder.h"
1515

16+
/**
17+
* Constructs and returns a FlutterLocale struct corresponding to |locale|, which must outlive
18+
* the returned struct.
19+
*/
20+
static FlutterLocale FlutterLocaleFromNSLocale(NSLocale* locale) {
21+
FlutterLocale flutterLocale = {};
22+
flutterLocale.struct_size = sizeof(FlutterLocale);
23+
flutterLocale.language_code = [[locale objectForKey:NSLocaleLanguageCode] UTF8String];
24+
flutterLocale.country_code = [[locale objectForKey:NSLocaleCountryCode] UTF8String];
25+
flutterLocale.script_code = [[locale objectForKey:NSLocaleScriptCode] UTF8String];
26+
flutterLocale.variant_code = [[locale objectForKey:NSLocaleVariantCode] UTF8String];
27+
return flutterLocale;
28+
}
29+
1630
/**
1731
* Private interface declaration for FlutterEngine.
1832
*/
1933
@interface FlutterEngine () <FlutterBinaryMessenger>
2034

35+
/**
36+
* Sends the list of user-preferred locales to the Flutter engine.
37+
*/
38+
- (void)sendUserLocales;
39+
2140
/**
2241
* Called by the engine to make the context the engine should draw into current.
2342
*/
@@ -181,6 +200,12 @@ - (instancetype)initWithName:(NSString*)labelPrefix
181200
_textures = [[NSMutableDictionary alloc] init];
182201
_allowHeadlessExecution = allowHeadlessExecution;
183202

203+
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
204+
[notificationCenter addObserver:self
205+
selector:@selector(sendUserLocales)
206+
name:NSCurrentLocaleDidChangeNotification
207+
object:nil];
208+
184209
return self;
185210
}
186211

@@ -254,6 +279,7 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
254279
return NO;
255280
}
256281

282+
[self sendUserLocales];
257283
[self updateWindowMetrics];
258284
return YES;
259285
}
@@ -314,6 +340,29 @@ - (void)sendPointerEvent:(const FlutterPointerEvent&)event {
314340

315341
#pragma mark - Private methods
316342

343+
- (void)sendUserLocales {
344+
if (!self.running) {
345+
return;
346+
}
347+
348+
// Create a list of FlutterLocales corresponding to the preferred languages.
349+
NSMutableArray<NSLocale*>* locales = [NSMutableArray array];
350+
std::vector<FlutterLocale> flutterLocales;
351+
flutterLocales.reserve(locales.count);
352+
for (NSString* localeID in [NSLocale preferredLanguages]) {
353+
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:localeID];
354+
[locales addObject:locale];
355+
flutterLocales.push_back(FlutterLocaleFromNSLocale(locale));
356+
}
357+
// Convert to a list of pointers, and send to the engine.
358+
std::vector<const FlutterLocale*> flutterLocaleList;
359+
flutterLocaleList.reserve(flutterLocales.size());
360+
std::transform(
361+
flutterLocales.begin(), flutterLocales.end(), std::back_inserter(flutterLocaleList),
362+
[](const auto& arg) -> const auto* { return &arg; });
363+
FlutterEngineUpdateLocales(_engine, flutterLocaleList.data(), flutterLocaleList.size());
364+
}
365+
317366
- (bool)engineCallbackOnMakeCurrent {
318367
if (!_mainOpenGLContext) {
319368
return false;

shell/platform/darwin/macos/framework/Source/FlutterViewController.mm

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ - (BOOL)launchEngine {
303303
}
304304
// Send the initial user settings such as brightness and text scale factor
305305
// to the engine.
306+
// TODO(stuartmorgan): Move this logic to FlutterEngine.
306307
[self sendInitialSettings];
307308
return YES;
308309
}

0 commit comments

Comments
 (0)