diff --git a/lib/src/csv_asset_loader.dart b/lib/src/csv_asset_loader.dart index 7c2c483..9adb4ab 100644 --- a/lib/src/csv_asset_loader.dart +++ b/lib/src/csv_asset_loader.dart @@ -12,6 +12,9 @@ import 'asset_loader.dart'; // class CsvAssetLoader extends AssetLoader { CSVParser? csvParser; + final Locale? fallbackLocale; + + CsvAssetLoader([this.fallbackLocale]); @override Future> load(String path, Locale locale) async { @@ -21,7 +24,7 @@ class CsvAssetLoader extends AssetLoader { } else { log('easy localization loader: CSV parser already loaded, read cache'); } - return csvParser!.getLanguageMap(locale.toString()); + return csvParser!.getLanguageMap(locale.toString(), fallbackLocale?.toString()); } } @@ -39,6 +42,7 @@ class CSVParser { final List> lines; final String? fieldDelimiter; final String? eol; + /// Enables automatic detection of the following /// /// [eols]: '\r\n' '\n' @@ -70,12 +74,19 @@ class CSVParser { return lines.first.sublist(1, lines.first.length); } - Map getLanguageMap(String localeName) { + Map getLanguageMap( + String localeName, [String? fallbackLocale]) { final indexLocale = lines.first.indexOf(localeName); + final indexFallbackLocale = + fallbackLocale != null ? lines.first.indexOf(fallbackLocale) : -1; var translations = {}; for (var i = 1; i < lines.length; i++) { - translations.addAll({lines[i][0]: lines[i][indexLocale]}); + String localeValue = lines[i][indexLocale]; + if (localeValue.isEmpty && indexFallbackLocale != -1) { + localeValue = lines[i][indexFallbackLocale]; + } + translations.addAll({lines[i][0]: localeValue}); } return translations; }