66/// @docImport 'package:flutter_localizations/flutter_localizations.dart';
77///
88/// @docImport 'app.dart';
9+ /// @docImport 'editable_text.dart';
910/// @docImport 'reorderable_list.dart';
1011library ;
1112
@@ -349,18 +350,20 @@ class _LocalizationsScope extends InheritedWidget {
349350 const _LocalizationsScope ({
350351 super .key,
351352 required this .locale,
353+ required this .isDefaultLocale,
352354 required this .localizationsState,
353355 required this .typeToResources,
354356 required super .child,
355357 });
356358
357359 final Locale locale;
360+ final bool isDefaultLocale;
358361 final _LocalizationsState localizationsState;
359362 final Map <Type , dynamic > typeToResources;
360363
361364 @override
362365 bool updateShouldNotify (_LocalizationsScope old) {
363- return typeToResources != old.typeToResources;
366+ return typeToResources != old.typeToResources || isDefaultLocale != old.isDefaultLocale ;
364367 }
365368}
366369
@@ -483,6 +486,7 @@ class Localizations extends StatefulWidget {
483486 required this .delegates,
484487 this .child,
485488 this .isApplicationLevel = false ,
489+ this .isDefaultLocale = false ,
486490 }) : assert (
487491 delegates.any (
488492 (LocalizationsDelegate <dynamic > delegate) =>
@@ -514,6 +518,10 @@ class Localizations extends StatefulWidget {
514518 /// The `locale` and `delegates` parameters default to the [Localizations.locale]
515519 /// and [Localizations.delegates] values from the nearest [Localizations] ancestor.
516520 ///
521+ /// If `locale` is null, [isDefaultLocale] is also inherited from the nearest
522+ /// [Localizations] ancestor. Otherwise [isDefaultLocale] is false, since the
523+ /// locale was chosen explicitly.
524+ ///
517525 /// To override the [Localizations.locale] or [Localizations.delegates] for an
518526 /// entire app, specify [WidgetsApp.locale] or [WidgetsApp.localizationsDelegates]
519527 /// (or specify the same parameters for [MaterialApp] ).
@@ -534,6 +542,7 @@ class Localizations extends StatefulWidget {
534542 key: key,
535543 locale: locale ?? Localizations .localeOf (context),
536544 delegates: mergedDelegates,
545+ isDefaultLocale: locale == null && Localizations .isDefaultLocaleOf (context),
537546 child: child,
538547 );
539548 }
@@ -554,6 +563,51 @@ class Localizations extends StatefulWidget {
554563 /// locale.
555564 final bool isApplicationLevel;
556565
566+ /// Whether [locale] is a default that the app did not choose, rather than a
567+ /// locale the app selected for its content.
568+ ///
569+ /// For example, [WidgetsApp] sets this to true when the app doesn't
570+ /// configure its locales (no [WidgetsApp.locale] , no locale resolution
571+ /// callbacks, and the default [WidgetsApp.supportedLocales] ). In that case
572+ /// [locale] is always `en_US` , regardless of the user's preferred locales.
573+ ///
574+ /// Text widgets such as [RichText] and [EditableText] don't pass a default
575+ /// locale to the text engine. The platform's default locale is used for
576+ /// font fallback and shaping instead, so that, for example, Han characters
577+ /// render with the glyphs the user expects.
578+ ///
579+ /// Defaults to false.
580+ ///
581+ /// See also:
582+ ///
583+ /// * [isDefaultLocaleOf] , which reads this value from a [BuildContext] .
584+ final bool isDefaultLocale;
585+
586+ /// Whether the locale of the nearest [Localizations] ancestor of `context`
587+ /// is a default that the app did not choose.
588+ ///
589+ /// Widgets that render text without [RichText] or [EditableText] , such as
590+ /// custom render objects that use [TextPainter] , can use this to pick
591+ /// the locale to give to the text engine:
592+ ///
593+ /// ```dart
594+ /// final Locale? textLocale = Localizations.isDefaultLocaleOf(context)
595+ /// ? null
596+ /// : Localizations.maybeLocaleOf(context);
597+ /// ```
598+ ///
599+ /// Returns false if there is no [Localizations] ancestor.
600+ ///
601+ /// See also:
602+ ///
603+ /// * [Localizations.isDefaultLocale] , which describes when the locale is a
604+ /// default.
605+ static bool isDefaultLocaleOf (BuildContext context) {
606+ final _LocalizationsScope ? scope = context
607+ .dependOnInheritedWidgetOfExactType <_LocalizationsScope >();
608+ return scope? .isDefaultLocale ?? false ;
609+ }
610+
557611 /// The locale of the Localizations widget for the widget tree that
558612 /// corresponds to [BuildContext] `context` .
559613 ///
@@ -629,6 +683,9 @@ class Localizations extends StatefulWidget {
629683 super .debugFillProperties (properties);
630684 properties.add (DiagnosticsProperty <Locale >('locale' , locale));
631685 properties.add (IterableProperty <LocalizationsDelegate <dynamic >>('delegates' , delegates));
686+ properties.add (
687+ FlagProperty ('isDefaultLocale' , value: isDefaultLocale, ifTrue: 'default locale' ),
688+ );
632689 }
633690}
634691
@@ -740,6 +797,7 @@ class _LocalizationsState extends State<Localizations> {
740797 child: _LocalizationsScope (
741798 key: _localizedResourcesScopeKey,
742799 locale: _locale! ,
800+ isDefaultLocale: widget.isDefaultLocale,
743801 localizationsState: this ,
744802 typeToResources: _typeToResources,
745803 child: Directionality (textDirection: _textDirection, child: widget.child! ),
0 commit comments