translateAll method

Future<void> translateAll(
  1. Locale locale, {
  2. bool changeLocale = true,
  3. ValueChanged<double>? onProgress,
})

Translates all cached keys into the given locale. Calls onProgress with values between 0.0 and 1.0 as translation progresses.

Implementation

Future<void> translateAll(
  Locale locale, {
  bool changeLocale = true,
  ValueChanged<double>? onProgress,
}) async {
  if (_delegate == null) return;
  final allKeys = _cache.values.expand((m) => m.keys).toSet();
  if (allKeys.isEmpty) return;

  final localeKey = locale.toString();
  final localeCache = _cache[localeKey] ?? {};

  int completed = 0;
  final total = allKeys.length;

  for (final key in allKeys) {
    if (localeCache.containsKey(key)) {
      completed++;
      onProgress?.call(completed / total);
      continue;
    }

    final translated = await _delegate!.translate(key, locale);
    if (translated.isNotEmpty && translated != key) {
      localeCache[key] = translated;
      _delegate!.translated(key, translated);
    }

    completed++;
    onProgress?.call(completed / total);
  }

  _cache[localeKey] = localeCache;
  if (_cachedMode) _delegate!.save(jsonEncode(_cache));
  if (changeLocale) _currentLocale = locale;
  notifyListeners();
}