load method

  1. @override
Future<bool> 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<bool> load({List<LocalizationLoader> loaders = const []}) async {
  print("$runtimeType - loading '$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.');
  }

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

  for (LocalizationLoader loader in loaders) {
    print(
        "$runtimeType - loading from a loader of type '${loader.runtimeType}'");
    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);
  }

  return true;
}