load method
Start loading the resources for locale. The returned future completes
when the resources have finished loading.
It's assumed that this method will return an object that contains a collection of related string resources (typically defined with one method per resource). The object will be retrieved with Localizations.of.
Implementation
@override
Future<CustomLocalization> load(Locale locale) async {
try {
String language = locale.languageCode;
developer.log(
'Loading localization for language: $language',
name: 'CustomLocalization',
);
String json = await rootBundle.loadString("lib/l10n/$filePrefix$language.arb");
Map<String, dynamic> decoded;
try {
decoded = jsonDecode(json);
} catch (e) {
developer.log(
'Failed to parse JSON for locale $language',
name: 'CustomLocalization',
error: e,
);
rethrow;
}
// Filter out metadata entries (keys starting with @) and ensure string values
Map<String, String> entries = {};
int skippedEntries = 0;
decoded.forEach((key, value) {
if (key.startsWith('@')) {
// Skip metadata entries
return;
}
if (value is String) {
entries[key] = value;
} else {
skippedEntries++;
developer.log(
'Skipped non-string value for key "$key" (type: ${value.runtimeType})',
name: 'CustomLocalization',
);
}
});
developer.log(
'Loaded ${entries.length} translations for $language${skippedEntries > 0 ? " ($skippedEntries entries skipped)" : ""}',
name: 'CustomLocalization',
);
return CustomLocalization(entries);
} catch (e, stackTrace) {
developer.log(
'Failed to load custom localization for locale ${locale.languageCode}',
name: 'CustomLocalization',
error: e,
stackTrace: stackTrace,
);
// Return empty localization as fallback to prevent app crash
developer.log(
'Returning empty localization as fallback',
name: 'CustomLocalization',
);
return CustomLocalization({});
}
}