load method

Future<void> load(
  1. Locale locale
)

Load translations for the given locale.

This method:

  1. Loads the fallback catalogue, then locale's, and layers the second over the first so a key missing from locale is served by the fallback
  2. Flattens and caches the sentences
  3. Syncs DateManager locale for date formatting
  4. Notifies listeners of the locale change

Why the fallback is merged rather than chosen

get answers _sentences[key] ?? key, and the only fallback that existed was JsonAssetLoader's, which is whole-FILE: it reads the fallback catalogue when the requested one fails to load at all, and never when the requested one loads and is simply incomplete. A half-translated app therefore rendered raw dotted keys on screen rather than the English sentence sitting in its own en.json.

Merging here rather than in the loader is deliberate: this class owns fallbackLocale, and every TranslationLoader a host writes gets the behaviour without knowing about it.

Implementation

Future<void> load(Locale locale) async {
  if (_loaded && _locale == locale) return;

  // Both reads start before either is awaited, so a locale with a fallback
  // pays one round trip rather than two. The spread order is what decides
  // precedence, and it is unaffected by the order they complete in.
  final fallback = _loadFallbackFor(locale);
  final own = _loader.load(locale);

  final data = <String, dynamic>{...await fallback, ...await own};

  // Convert all values to strings
  _sentences = data.map((key, value) => MapEntry(key, value.toString()));
  _locale = locale;

  // Sync DateManager locale for date formatting (Carbon)
  await _syncDateManagerLocale(locale);

  _loaded = true;
  notifyListeners();
  await Event.dispatch(LocaleChanged(locale));
}