Simplified Chinese | Traditional Chinese | English | Japanese | Korean | Arabic |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
LocaleProvider and AtomicLocalizations. Wrap your app's main entry point with MultiProvider, and configure the language settings in MaterialApp as shown below. For a complete example, see the demo:@overrideWidget build(BuildContext context) {return ComponentTheme(child: MultiProvider(providers: [// Language provider for dynamic language switching; notifies components when the language changesChangeNotifierProvider.value(value: LocaleProvider()),],child: Builder(builder: (context) {final themeState = BaseThemeProvider.of(context);final isDarkMode = themeState.isDarkMode;// Access the language providerfinal localeProvider = Provider.of<LocaleProvider>(context);return MaterialApp(// ...... other configurations omittedlocalizationsDelegates: const [AtomicLocalizations.delegate,// ...... you can add other localization delegates for additional components],supportedLocales: AtomicLocalizations.supportedLocales, // Supported app languageslocale: localeProvider.locale, // Current language setting);}),),);
LocaleProvider offers a changeLanguage method for switching languages at runtime. The selected language is cached locally, so your app will remember the user's choice after restart by reading the value from the locale field.LocaleProvider instance and call changeLanguage:final localeProvider = Provider.of<LocaleProvider>(context, listen: false);// Switch to EnglishlocaleProvider.changeLanguage('en');
State subclass of a StatefulWidget will trigger the didChangeDependencies lifecycle method. In this method, initialize the AtomicLocalizations object and use the built-in localized entries.class ContactList extends StatefulWidget {const ContactList({super.key,});@overrideState<ContactList> createState() => _ContactListState();}class _ContactListState extends State<ContactList> {late AtomicLocalizations atomicLocale;// Other code omitted@overridevoid didChangeDependencies() {super.didChangeDependencies();// Initialize localization dynamicallyatomicLocale = AtomicLocalizations.of(context);}@overrideWidget build(BuildContext context) {// Display the addFriend label in the current languagereturn Text(atomicLocale.addFriend);}}
.arb files in the l10n directory. To add a new language, create a new .arb file with the appropriate language code and translate all entries.l10n_xx.arb (for example, Spanish: l10n_es.arb).
flutter gen-l10n command in the component's root directory. This will update the localizations directory with your new language file.
locale_provider.dart to cache Spanish locally, then call changeLanguage('es') to switch to Spanish.
flutter gen-l10n command in the component's root directory to update the localization files.Feedback