load method

Future<YamlLocalizations> load(
  1. Locale locale
)

Load and cache a yaml file per language / country code.

Implementation

Future<YamlLocalizations> load(Locale locale) async {
  // get the key from languageCode and countryCode
  _langTag = locale.toLanguageTag();

  // in cache already
  if (_translations.containsKey(_langTag)) {
    return this;
  }

  // try to load combination of languageCode and countryCode
  try {
    final text = await assetBundle.loadString('$assetPath/$_langTag.yaml');
    _translations[_langTag] = loadYaml(text);
    return this;
  } catch (e) {}

  // try to load only language code
  if (_langTag != locale.languageCode) {
    _langTag = locale.languageCode;
    try {
      final text = await assetBundle.loadString('$assetPath/$_langTag.yaml');
      _translations[_langTag] = loadYaml(text);
      return this;
    } catch (e) {}
  }

  assert(false, 'Translation file not found for code \'$_langTag\'');

  return this;
}