From 0e735e4bca539c24cf22b8e4cd6d5340b9ae389a Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 30 Jun 2020 18:54:02 -0700 Subject: [PATCH] Expose ComputePlatformResolvedLocale (#58482) --- packages/flutter/lib/src/widgets/binding.dart | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index 871c36ae17..8ed120487d 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -944,6 +944,46 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB buildOwner.reassemble(renderViewElement); return super.performReassemble(); } + + /// Computes the locale the current platform would resolve to. + /// + /// This method is meant to be used as part of a [localeListResolutionCallback]. + /// Since this method may return null, a Flutter/dart algorithm should still be + /// provided as a fallback in case a native resolved locale cannot be determined + /// or if the native resolved locale is undesirable. + /// + /// This method may return a null [Locale] if the platform does not support + /// native locale resolution, or if the resolution failed. + /// + /// The first [supportedLocale] is treated as the default locale and will be returned + /// if no better match is found. + /// + /// Android and iOS are currently supported. + /// + /// On Android, the algorithm described in + /// https://developer.android.com/guide/topics/resources/multilingual-support + /// is used to determine the resolved locale. Depending on the android version + /// of the device, either the modern (>= API 24) or legacy (< API 24) algorithm + /// will be used. + /// + /// On iOS, the result of `preferredLocalizationsFromArray` method of `NSBundle` + /// is returned. See: + /// https://developer.apple.com/documentation/foundation/nsbundle/1417249-preferredlocalizationsfromarray?language=objc + /// for details on the used method. + /// + /// iOS treats script code as necessary for a match, so a user preferred locale of + /// `zh_Hans_CN` will not resolve to a supported locale of `zh_CN`. + /// + /// Since implementation may vary by platform and has potential to be heavy, + /// it is recommended to cache the results of this method if the value is + /// used multiple times. + /// + /// Second-best (and n-best) matching locales should be obtained by calling this + /// method again with the matched locale of the first call omitted from + /// [supportedLocales]. + Locale computePlatformResolvedLocale(List supportedLocales) { + return window.computePlatformResolvedLocale(supportedLocales); + } } /// Inflate the given widget and attach it to the screen.