getLocaleData method
All mappings of locale (with the default locale's underneath when
fallbackDefaultLocale is on), or null if locale isn't defined.
Throws NoLocalizedTextException if, with the default-locale
fallback, locale has keys the default locale lacks.
Implementation
Map<String, String>? getLocaleData(String? locale) {
if (locale == null || !_locales.contains(locale)) return null;
final defaultKeys = <String>{};
final data = <String, String>{};
final defaultLocale = _defaultLocale;
final useDefault = fallbackDefaultLocale && defaultLocale != null;
if (useDefault) {
for (final resource in _localeResources[defaultLocale]!) {
data.addAll(resource.localizedText);
}
defaultKeys.addAll(data.keys);
}
for (final resource in _localeResources[locale]!) {
data.addAll(resource.localizedText);
}
if (useDefault) {
final missing = data.keys.where((k) => !defaultKeys.contains(k)).toList();
if (missing.isNotEmpty) {
final missingKeys = missing.map((k) => '$k,').join();
throw NoLocalizedTextException(
'Error loading locale $locale. There were ${missing.length} keys '
'which were contained in this locale, but were not properly '
'registered in the default Locale. Any keys which are added to a '
'locale should always be added to the default locale to ensure '
'appropriate functioning.\n'
'The missing translations were for the keys: $missingKeys',
missingKeys,
defaultLocale,
);
}
}
return data;
}