Skip to content

Commit d2ad441

Browse files
authored
Fix PlatformDispatcher.locale to return something meaningful when there are no locales. (flutter#22608)
Returns an "undefined" locale (language code "und") from PlatformDispatcher.locale when no locales are defined.
1 parent 40fa345 commit d2ad441

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 8 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
///
@@ -1318,6 +1319,10 @@ class Locale {
13181319
/// [region](https://github.com/unicode-org/cldr/blob/master/common/validity/region.xml) for
13191320
/// each of languageCode, scriptCode and countryCode respectively.
13201321
///
1322+
/// The [languageCode] subtag is optional. When there is no language subtag,
1323+
/// the parameter should be omitted or set to "und". When not supplied, the
1324+
/// [languageCode] defaults to "und", an undefined language code.
1325+
///
13211326
/// The [countryCode] subtag is optional. When there is no country subtag,
13221327
/// the parameter should be omitted or passed `null` instead of an empty-string.
13231328
///

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,17 @@ 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+
late 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+
expectEquals(locale.languageCode, 'und');
441+
});
429442
}

0 commit comments

Comments
 (0)