getFallbackLocales method

List<Locale> getFallbackLocales(
  1. Locale locale
)

Get the fallback locale chain for the given locale.

This method returns the ordered list of locales to try when resolving messages, implementing the fallback strategy.

Example for Locale('fr', 'CA', 'Quebec'):

  1. fr-CA-Quebec
  2. fr-CA
  3. fr
  4. Default locale (if different)

Implementation

List<Locale> getFallbackLocales(Locale locale) {
  // If locale is the default, return only that
  if (locale.matches(_defaultLocale)) {
    return [locale];
  }

  final fallbacks = <Locale>[locale];

  // Add variant fallback
  if (locale.hasVariant()) {
    fallbacks.add(Locale(locale.getLanguage(), locale.getCountry()));
  }

  // Add country fallback
  if (locale.hasCountry()) {
    fallbacks.add(Locale(locale.getLanguage()));
  }

  // Add default locale fallback
  if (!locale.matches(_defaultLocale)) {
    fallbacks.add(_defaultLocale);
  }

  return fallbacks;
}