Localizations.override constructor

Localizations.override({
  1. Key? key,
  2. required BuildContext context,
  3. Locale? locale,
  4. List<LocalizationsDelegate>? delegates,
  5. Widget? child,
})

Overrides the inherited Locale or LocalizationsDelegates for child.

This factory constructor is used for the (usually rare) situation where part of an app should be localized for a different locale than the one defined for the device, or if its localizations should come from a different list of LocalizationsDelegates than the list defined by WidgetsApp.localizationsDelegates.

For example you could specify that myWidget was only to be localized for the US English locale:

Widget build(BuildContext context) {
  return Localizations.override(
    context: context,
    locale: const Locale('en', 'US'),
    child: myWidget,
  );
}

The locale and delegates parameters default to the Localizations.locale and Localizations.delegates values from the nearest Localizations ancestor.

To override the Localizations.locale or Localizations.delegates for an entire app, specify WidgetsApp.locale or WidgetsApp.localizationsDelegates (or specify the same parameters for MaterialApp).

Implementation

factory Localizations.override({
  Key? key,
  required BuildContext context,
  Locale? locale,
  List<LocalizationsDelegate<dynamic>>? delegates,
  Widget? child,
}) {
  final List<LocalizationsDelegate<dynamic>> mergedDelegates = Localizations._delegatesOf(context);
  if (delegates != null) {
    mergedDelegates.insertAll(0, delegates);
  }
  return Localizations(
    key: key,
    locale: locale ?? Localizations.localeOf(context),
    delegates: mergedDelegates,
    child: child,
  );
}