initAsync static method

Future<bool?> initAsync({
  1. String? csv,
  2. Map<String, Map<String, String>>? map,
})

Implementation

static Future<bool?> initAsync(
    {String? csv, Map<String, Map<String, String>>? map}) async {
  // Already been called
  if ((_allValues != null && _allValues!.isNotEmpty) || _I10n.file != null) {
    return false;
  }

  // Assign the csv file if not already assigned
  if (_csvFile == null && csv != null && csv.trim().isNotEmpty) {
    _csvFile = csv.trim();
    // If no path, set a path
    if (p.dirname(_csvFile!) == '.') {
      _csvFile = p.join('assets', 'i10n', p.basename(_csvFile!));
    } else if (_csvFile!.indexOf(p.separator) == 0) {
      _csvFile = _csvFile!.substring(1);
    }
    // If no file extension, add it.
    _csvFile = p.setExtension(_csvFile!, '.csv');
  }

  // Assign the map if not already assigned
  if (map != null && map.isNotEmpty) {
    _allValues ??= map;
  }

  bool? init = true;

  if (_allValues == null || _allValues!.isEmpty) {
    // Can't open a text file on the Web
    if (_csvFile != null &&
        _csvFile!.isNotEmpty &&
        !UniversalPlatform.isWeb) {
      // Open a csv file to place in entries.
      await _I10n.init(p.basename(_csvFile!));
    }
    try {
      // Create the 'default' csv file for the developer.
      if (I10n.csvFile == kDefaultCSV) {
        init = await _I10n.create(I10n.csvFile);
      }

      // Attempt to load the file
      if (init || UniversalPlatform.isWeb) {
        //
        init = await _I10n.load();

        final _locale = I10n.appLocale;

        if (_locale != null) {
          await I10n.load(_locale);
        }
      }
    } catch (ex) {
      init = false;
    }
  }

  if (_allValues != null) {
    _locales ??= _allValues!.keys.expand((e) => [e]).toList();
  }

  return init;
}