|
13 | 13 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
|
14 | 14 | #import "flutter/shell/platform/embedder/embedder.h"
|
15 | 15 |
|
| 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 | + |
16 | 30 | /**
|
17 | 31 | * Private interface declaration for FlutterEngine.
|
18 | 32 | */
|
19 | 33 | @interface FlutterEngine () <FlutterBinaryMessenger>
|
20 | 34 |
|
| 35 | +/** |
| 36 | + * Sends the list of user-preferred locales to the Flutter engine. |
| 37 | + */ |
| 38 | +- (void)sendUserLocales; |
| 39 | + |
21 | 40 | /**
|
22 | 41 | * Called by the engine to make the context the engine should draw into current.
|
23 | 42 | */
|
@@ -181,6 +200,12 @@ - (instancetype)initWithName:(NSString*)labelPrefix
|
181 | 200 | _textures = [[NSMutableDictionary alloc] init];
|
182 | 201 | _allowHeadlessExecution = allowHeadlessExecution;
|
183 | 202 |
|
| 203 | + NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter]; |
| 204 | + [notificationCenter addObserver:self |
| 205 | + selector:@selector(sendUserLocales) |
| 206 | + name:NSCurrentLocaleDidChangeNotification |
| 207 | + object:nil]; |
| 208 | + |
184 | 209 | return self;
|
185 | 210 | }
|
186 | 211 |
|
@@ -254,6 +279,7 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
|
254 | 279 | return NO;
|
255 | 280 | }
|
256 | 281 |
|
| 282 | + [self sendUserLocales]; |
257 | 283 | [self updateWindowMetrics];
|
258 | 284 | return YES;
|
259 | 285 | }
|
@@ -314,6 +340,29 @@ - (void)sendPointerEvent:(const FlutterPointerEvent&)event {
|
314 | 340 |
|
315 | 341 | #pragma mark - Private methods
|
316 | 342 |
|
| 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 | + |
317 | 366 | - (bool)engineCallbackOnMakeCurrent {
|
318 | 367 | if (!_mainOpenGLContext) {
|
319 | 368 | return false;
|
|
0 commit comments