Closed iLoveDocs closed 1 year ago
Thanks for pointing that out. Indeed, setting the locale before localizationsDelegates
will not yield the desired result.
To start your app with a specific locale, you might find the following snippet helpful:
void main() async {
runApp(
MaterialApp(
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
locale: const Locale('de'), // Explicit setting of the locale
home: FooPage(),
),
);
}
For implementing a language switcher in Flutter app, maybe this approach might be of help.
Issue:
When attempting to change the locale using S.load(...) from the main function, the S.current reference still points to the old locale.
Minimal reproducible code:
When running the app, the button displays
"Hello"
. Even if a hot reload is performed, it continues to display"Hello"
. However, pressing the FloatingActionButton (FAB) displays"Hallo"
.Putting
S.load(...)
in the_FooPageState.initState
callback results in the expected behavior.