load method

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

Load the translations for Mobility Package.

The translations consists of the static names in the package as provided in staticAssetName.

Implementation

@override
Future<void> load({List<LocalizationLoader> loaders = const []}) async {
  if (kDebugMode) {
    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 (_) {
    if (kDebugMode) {
      print(
        "WARNING - Failed to load MP 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 MP 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 MP so you can provide translations for the MP 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);
  }
}