File tree Expand file tree Collapse file tree 6 files changed +51
-0
lines changed
engine/src/flutter/lib/web_ui
view_embedder/embedding_strategy
test/engine/platform_dispatcher Expand file tree Collapse file tree 6 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -799,6 +799,13 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
799799 implicitView? .semantics.updateSemantics (update);
800800 }
801801
802+ @override
803+ void setApplicationLocale (ui.Locale locale) {
804+ for (final EngineFlutterView view in views) {
805+ view.setLocale (locale);
806+ }
807+ }
808+
802809 /// This is equivalent to `locales.first` , except that it will provide an
803810 /// undefined (using the language tag "und") non-null locale if the [locales]
804811 /// list has not been set or is empty.
Original file line number Diff line number Diff line change 33// found in the LICENSE file.
44
55import 'package:ui/src/engine/dom.dart' ;
6+ import 'package:ui/ui.dart' as ui;
67
78import '../hot_restart_cache_handler.dart' show registerElementForCleanup;
89import 'embedding_strategy.dart' ;
@@ -27,6 +28,11 @@ class CustomElementEmbeddingStrategy implements EmbeddingStrategy {
2728 /// The root element of the Flutter view.
2829 late final DomElement _rootElement;
2930
31+ @override
32+ void setLocale (ui.Locale locale) {
33+ hostElement.setAttribute ('lang' , locale.toLanguageTag ());
34+ }
35+
3036 @override
3137 void attachViewRoot (DomElement rootElement) {
3238 rootElement
Original file line number Diff line number Diff line change 33// found in the LICENSE file.
44
55import 'package:ui/src/engine/dom.dart' ;
6+ import 'package:ui/ui.dart' as ui;
67
78import 'custom_element_embedding_strategy.dart' ;
89import 'full_page_embedding_strategy.dart' ;
@@ -26,6 +27,14 @@ abstract class EmbeddingStrategy {
2627 }
2728 }
2829
30+ /// Sets the locale for the embedded view.
31+ ///
32+ /// This method is typically called by the Flutter framework after it has
33+ /// resolved the application's locale. It configures the embedded view to
34+ /// reflect the given locale, which is important for accessibility and for
35+ /// the browser to select appropriate fonts and other locale-specific resources.
36+ void setLocale (ui.Locale locale);
37+
2938 /// The DOM element in which the Flutter view is embedded.
3039 /// This element is the direct parent element of the <flutter-view> element.
3140 DomElement get hostElement;
Original file line number Diff line number Diff line change 44
55import 'package:ui/src/engine/dom.dart' ;
66import 'package:ui/src/engine/util.dart' ;
7+ import 'package:ui/ui.dart' as ui;
78
89import '../hot_restart_cache_handler.dart' show registerElementForCleanup;
910import 'embedding_strategy.dart' ;
@@ -25,6 +26,11 @@ class FullPageEmbeddingStrategy implements EmbeddingStrategy {
2526 @override
2627 DomEventTarget get globalEventTarget => domWindow;
2728
29+ @override
30+ void setLocale (ui.Locale locale) {
31+ domDocument.documentElement! .setAttribute ('lang' , locale.toLanguageTag ());
32+ }
33+
2834 @override
2935 void attachViewRoot (DomElement rootElement) {
3036 /// Tweaks style so the rootElement works well with the hostElement.
Original file line number Diff line number Diff line change @@ -131,6 +131,16 @@ class EngineFlutterView implements ui.FlutterView {
131131 semantics.updateSemantics (update);
132132 }
133133
134+ /// Sets the locale for this view.
135+ ///
136+ /// This method is typically called by the Flutter framework after it has
137+ /// resolved the application's locale. It configures the view to reflect
138+ /// the given locale, which is important for accessibility and for the
139+ /// browser.
140+ void setLocale (ui.Locale locale) {
141+ embeddingStrategy.setLocale (locale);
142+ }
143+
134144 late final GlobalHtmlAttributes _globalHtmlAttributes = GlobalHtmlAttributes (
135145 rootElement: dom.rootElement,
136146 hostElement: embeddingStrategy.hostElement,
Original file line number Diff line number Diff line change @@ -229,6 +229,19 @@ void testMain() {
229229 expect (codec.decodeEnvelope (response! ), true );
230230 });
231231
232+ test ('can set application locale' , () async {
233+ final DomElement host1 = createDomHTMLDivElement ();
234+ final EngineFlutterView view1 = EngineFlutterView (dispatcher, host1);
235+ final EngineFlutterView view2 = EngineFlutterView .implicit (dispatcher, null );
236+ dispatcher.viewManager
237+ ..registerView (view1)
238+ ..registerView (view2);
239+
240+ dispatcher.setApplicationLocale (const ui.Locale ('es' , 'MX' ));
241+ expect (host1.getAttribute ('lang' ), 'es-MX' );
242+ expect (domDocument.querySelector ('html' )! .getAttribute ('lang' ), 'es-MX' );
243+ });
244+
232245 test ('can find text scale factor' , () async {
233246 const double deltaTolerance = 1e-5 ;
234247
You can’t perform that action at this time.
0 commit comments