load method

Future<void> load(
  1. {bool cachable}
)

Asynchronously loads the JSON file associated with the configured locale. If cachable is not false, and the localized messages have previously been loaded into memory, then they'll be reused.

Implementation

Future<void> load({bool cachable}) async {
  if (false != cachable && _cache.containsKey(locale)) {
    _messages?.clear();
    _messages = _cache[locale];
  } else {
    final localeFile = _resolveLocaleFile(locale);
    var jsonString =
        await rootBundle.loadString('$_baseDirectory/$localeFile.json');

    assert(jsonString.isNotEmpty);
    jsonString = jsonString.trim();
    assert(8 < jsonString.length);
    assert('{' == jsonString[0] && '}' == jsonString[jsonString.length - 1]);

    final l10n = (json.decode(jsonString) as Map).cast<String, dynamic>();

    _messages?.clear();
    _messages = l10n;

    // We always cache the localizations.
    _cache[locale] = _messages;
  }
}