Skip to content

Commit

Permalink
fix: avoid double call
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Jul 10, 2024
1 parent ff55e8b commit 1d391d5
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/loaders/file_translation_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
/// Return the translation Map
Future<Map> load() async {
_decodedMap = Map();
final fileName = composeFileName();
await this._defineLocale();
await _loadCurrentTranslation();
await _loadFallback();
_decodedMap.addAll(await _loadTranslation(fileName, false));
if (fileName != fallbackFile) {
final Map fallbackMap = await _loadTranslation(fallbackFile, true);
_decodedMap = _deepMergeMaps(fallbackMap, _decodedMap);
MessagePrinter.debug('Fallback maps have been merged');
}
return _decodedMap;
}

Expand All @@ -61,11 +66,12 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
cache: false);
}

Future _loadCurrentTranslation() async {
Future _loadTranslation(String fileName, bool isFallback) async {
try {
_decodedMap.addAll(await loadFile(composeFileName()));
return await loadFile(fileName);
} catch (e) {
MessagePrinter.debug('Error loading translation $e');
MessagePrinter.debug(
'Error loading translation${isFallback ? " fallback " : " "}$e');
}
}

Expand All @@ -74,16 +80,6 @@ class FileTranslationLoader extends TranslationLoader implements IFileContent {
MessagePrinter.info("The current locale is ${this.locale}");
}

Future _loadFallback() async {
try {
final Map fallbackMap = await loadFile(fallbackFile);
_decodedMap = _deepMergeMaps(fallbackMap, _decodedMap);
MessagePrinter.debug('Fallback maps have been merged');
} catch (e) {
MessagePrinter.debug('Error loading translation fallback $e');
}
}

Map<K, V> _deepMergeMaps<K, V>(
Map<K, V> map1,
Map<K, V> map2,
Expand Down

0 comments on commit 1d391d5

Please sign in to comment.