Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 67fcafb

Browse files
committed
Fix PlatformDispatcher.locale to return something meaningful when there are no locales.
1 parent f9bb4d1 commit 67fcafb

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,10 @@ class PlatformDispatcher {
549549
/// This is the first locale selected by the user and is the user's primary
550550
/// locale (the locale the device UI is displayed in)
551551
///
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;
555556

556557
/// The full system-reported supported locales of the device.
557558
///

lib/web_ui/lib/src/engine/platform_dispatcher.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
546546
EngineSemanticsOwner.instance.updateSemantics(update);
547547
}
548548

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+
///
549553
/// We use the first locale in the [locales] list instead of the browser's
550554
/// built-in `navigator.language` because browsers do not agree on the
551555
/// implementation.
@@ -554,7 +558,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
554558
///
555559
/// * https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/languages,
556560
/// 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;
558562

559563
/// The full system-reported supported locales of the device.
560564
///

lib/web_ui/test/engine/window_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ void testMain() {
292292
// that the list is populated again).
293293
EnginePlatformDispatcher.instance.debugResetLocales();
294294
expect(window.locales, isEmpty);
295+
expect(window.locale, equals(const ui.Locale.fromSubtags()));
295296
expect(localeChangedCount, 0);
296297
html.window.dispatchEvent(html.Event('languagechange'));
297298
expect(window.locales, isNotEmpty);

testing/dart/window_hooks_integration_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,16 @@ void main() {
426426
expectEquals(window.padding.bottom, 0.0);
427427
expectEquals(window.systemGestureInsets.bottom, 44.0);
428428
});
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+
});
429441
}

0 commit comments

Comments
 (0)