Skip to content

Commit 5d29668

Browse files
fix(web): guard parseBrowserLanguages against invalid locale tags
Skip language tags that Intl.Locale rejects (e.g. en-US@posix reported by Chromium on Linux with LANG unset) instead of throwing during engine bootstrap. This prevents a permanent blank page before main() runs. If all tags are invalid, fall back to the default en-US locale, preserving the existing never-empty contract without relying on assert. Adds unit coverage via debugOverrideBrowserLanguages for the invalid tag and default-locale fallback paths. Fixes #190510 Signed-off-by: Sankalp Thakur <[email protected]>
1 parent e26b384 commit 5d29668

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,17 +954,23 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
954954

955955
final locales = <ui.Locale>[];
956956
for (final String language in languages) {
957-
final domLocale = DomLocale(language);
958-
locales.add(
959-
ui.Locale.fromSubtags(
960-
languageCode: domLocale.language,
961-
scriptCode: domLocale.script,
962-
countryCode: domLocale.region,
963-
),
964-
);
957+
try {
958+
final domLocale = DomLocale(language);
959+
locales.add(
960+
ui.Locale.fromSubtags(
961+
languageCode: domLocale.language,
962+
scriptCode: domLocale.script,
963+
countryCode: domLocale.region,
964+
),
965+
);
966+
} catch (_) {
967+
// Skip tags Intl.Locale rejects (e.g. en-US@posix on Linux).
968+
}
965969
}
966970

967-
assert(locales.isNotEmpty);
971+
if (locales.isEmpty) {
972+
return const <ui.Locale>[_defaultLocale];
973+
}
968974
return locales;
969975
}
970976

‎engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher/platform_dispatcher_test.dart‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,29 @@ void testMain() {
715715
ui.Locale('de', 'DE'),
716716
]);
717717
});
718+
719+
test('skips invalid tags such as en-US@posix and falls back to default locale', () {
720+
// Chromium on Linux with LANG unset can report en-US@posix, which
721+
// Intl.Locale rejects. The parse path must not throw during bootstrap.
722+
EnginePlatformDispatcher.debugOverrideBrowserLanguages(['en-US@posix']);
723+
addTearDown(() => EnginePlatformDispatcher.debugOverrideBrowserLanguages(null));
724+
725+
expect(EnginePlatformDispatcher.parseBrowserLanguages(), const [ui.Locale('en', 'US')]);
726+
});
727+
728+
test('keeps valid locales when some browser language tags are invalid', () {
729+
EnginePlatformDispatcher.debugOverrideBrowserLanguages([
730+
'en-US@posix',
731+
'fr-FR',
732+
'de-DE',
733+
]);
734+
addTearDown(() => EnginePlatformDispatcher.debugOverrideBrowserLanguages(null));
735+
736+
expect(EnginePlatformDispatcher.parseBrowserLanguages(), const [
737+
ui.Locale('fr', 'FR'),
738+
ui.Locale('de', 'DE'),
739+
]);
740+
});
718741
});
719742

720743
group('AT Focus Handler Integration', () {

0 commit comments

Comments
 (0)