load method

  1. @override
Future<void> load({
  1. List<LocalizationLoader> loaders = const [],
})
override

Load the translations for Research Package.

The translations is a combination of the static names in the package as provided in staticAssetName combined with translations of the any text provided by the loaders, which knows how to load translations.

Implementation

@override
Future<void> load({List<LocalizationLoader> loaders = const []}) async {
  print("$runtimeType - loading static translations from '$staticAssetName'");
  String jsonString = '{}';

  // first try to load the static translations as part of RP
  try {
    jsonString = await rootBundle.loadString(
      staticAssetName,
      cache: false,
    );
  } catch (_) {
    print(
        "WARNING - Failed to load RP translations for '$locale' and it seems like RP does not support this locale in the current version. "
        'If you are using this locale in your app, you should consider to make a pull request to RP so we can add this locale to the package for others to use as well. '
        'See https://carp.cachet.dk/localization for a description on how to do this. '
        'For now, translations provided in the app localization file(s) are also used for RP so you can provide translations for the RP terms there for now.');
  }

  final jsonMap = json.decode(jsonString) as Map<String, dynamic>;
  translations
      .addAll(jsonMap.map((key, value) => MapEntry(key, value.toString())));

  for (LocalizationLoader loader in loaders) {
    Map<String, String> loadedTranslations = await loader.load(locale);
    // Merge the maps.
    // Note that keys in [_translations] is overwritten with keys in [loadedTranslations].
    // Hence, it is possible to overwrite the default translations.
    translations.addAll(loadedTranslations);
  }
}