Skip to content

Commit b872cf9

Browse files
kevmoosuryaKommana2662
authored andcommitted
Only call setApplicationLocale for application-level Localizations (flutter#194892)
Guard `PlatformDispatcher.setApplicationLocale` in [`_LocalizationsState.locale`](https://github.com/flutter/flutter/blob/115a15fbf1fd9c6ae4173fff892fd0075eef069d/packages/flutter/lib/src/widgets/localizations.dart#L641-L648) behind `widget.isApplicationLevel` so subtree `Localizations` and `Localizations.override` widgets do not overwrite the application-wide locale (`<html lang>` on Web and fallback accessibility locales on Android/iOS). Fixes flutter#194696 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 95dddd8 commit b872cf9

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎packages/flutter/lib/src/widgets/localizations.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,9 @@ class _LocalizationsState extends State<Localizations> {
643643
if (_locale == locale) {
644644
return;
645645
}
646-
WidgetsBinding.instance.platformDispatcher.setApplicationLocale(locale!);
646+
if (widget.isApplicationLevel) {
647+
WidgetsBinding.instance.platformDispatcher.setApplicationLocale(locale!);
648+
}
647649
_locale = locale;
648650
}
649651

‎packages/flutter/test/widgets/localizations_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ void main() {
7979
expect(tester.binding.platformDispatcher.applicationLocale, const Locale('fo'));
8080
});
8181

82+
testWidgets('Locale is not sent to engine if Localizations is not application level', (
83+
WidgetTester tester,
84+
) async {
85+
await tester.pumpWidget(
86+
WidgetsApp(
87+
color: const Color(0xFF000000),
88+
locale: const Locale('en', 'US'),
89+
builder: (BuildContext context, _) {
90+
return Localizations.override(
91+
context: context,
92+
locale: const Locale('es', 'US'),
93+
child: const Text('Hola'),
94+
);
95+
},
96+
),
97+
);
98+
expect(tester.binding.platformDispatcher.applicationLocale, const Locale('en', 'US'));
99+
});
100+
82101
testWidgets('Localizations.localeOf throws when no localizations exist', (
83102
WidgetTester tester,
84103
) async {

0 commit comments

Comments
 (0)