This repository was archived by the owner on Feb 25, 2025. It is now read-only.
File tree 4 files changed +22
-4
lines changed 4 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -549,9 +549,10 @@ class PlatformDispatcher {
549
549
/// This is the first locale selected by the user and is the user's primary
550
550
/// locale (the locale the device UI is displayed in)
551
551
///
552
- /// This is equivalent to `locales.first` and will provide an empty non-null
553
- /// locale if the [locales] list has not been set or is empty.
554
- Locale get locale => locales.first;
552
+ /// This is equivalent to `locales.first` , except that it will provide an
553
+ /// undefined (using the language tag "und") non-null locale if the [locales]
554
+ /// list has not been set or is empty.
555
+ Locale get locale => locales.isEmpty ? const Locale .fromSubtags () : locales.first;
555
556
556
557
/// The full system-reported supported locales of the device.
557
558
///
Original file line number Diff line number Diff line change @@ -546,6 +546,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
546
546
EngineSemanticsOwner .instance.updateSemantics (update);
547
547
}
548
548
549
+ /// This is equivalent to `locales.first` , except that it will provide an
550
+ /// undefined (using the language tag "und") non-null locale if the [locales]
551
+ /// list has not been set or is empty.
552
+ ///
549
553
/// We use the first locale in the [locales] list instead of the browser's
550
554
/// built-in `navigator.language` because browsers do not agree on the
551
555
/// implementation.
@@ -554,7 +558,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
554
558
///
555
559
/// * https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/languages,
556
560
/// which explains browser quirks in the implementation notes.
557
- ui.Locale get locale => locales.first;
561
+ ui.Locale get locale => locales.isEmpty ? const ui. Locale . fromSubtags () : locales. first;
558
562
559
563
/// The full system-reported supported locales of the device.
560
564
///
Original file line number Diff line number Diff line change @@ -292,6 +292,7 @@ void testMain() {
292
292
// that the list is populated again).
293
293
EnginePlatformDispatcher .instance.debugResetLocales ();
294
294
expect (window.locales, isEmpty);
295
+ expect (window.locale, equals (const ui.Locale .fromSubtags ()));
295
296
expect (localeChangedCount, 0 );
296
297
html.window.dispatchEvent (html.Event ('languagechange' ));
297
298
expect (window.locales, isNotEmpty);
Original file line number Diff line number Diff line change @@ -426,4 +426,16 @@ void main() {
426
426
expectEquals (window.padding.bottom, 0.0 );
427
427
expectEquals (window.systemGestureInsets.bottom, 44.0 );
428
428
});
429
+
430
+ test ('PlatformDispatcher.locale returns unknown locale when locales is set to empty list' , () {
431
+ Locale ? locale;
432
+ runZoned (() {
433
+ window.onLocaleChanged = () {
434
+ locale = PlatformDispatcher .instance.locale;
435
+ };
436
+ });
437
+
438
+ _updateLocales (< String > []);
439
+ expectEquals (locale, const Locale .fromSubtags ());
440
+ });
429
441
}
You can’t perform that action at this time.
0 commit comments