From 16252732780602e392fa7dd507cfda984112a7d1 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 16 Jan 2023 19:29:52 +0700 Subject: [PATCH 1/3] added fallback locale to fill empty values in csv loader --- lib/src/csv_asset_loader.dart | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/src/csv_asset_loader.dart b/lib/src/csv_asset_loader.dart index 7c2c483..8297609 100644 --- a/lib/src/csv_asset_loader.dart +++ b/lib/src/csv_asset_loader.dart @@ -14,14 +14,15 @@ class CsvAssetLoader extends AssetLoader { CSVParser? csvParser; @override - Future> load(String path, Locale locale) async { + Future> load(String path, Locale locale, + [Locale? localeForFillEmptyValues]) async { if (csvParser == null) { log('easy localization loader: load csv file $path'); csvParser = CSVParser(await rootBundle.loadString(path)); } else { log('easy localization loader: CSV parser already loaded, read cache'); } - return csvParser!.getLanguageMap(locale.toString()); + return csvParser!.getLanguageMap(locale.toString(), localeForFillEmptyValues?.toString()); } } @@ -39,6 +40,7 @@ class CSVParser { final List> lines; final String? fieldDelimiter; final String? eol; + /// Enables automatic detection of the following /// /// [eols]: '\r\n' '\n' @@ -70,12 +72,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; } From 66bc0612077f8289bf0367a1731a45561331b494 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 16 Jan 2023 19:33:19 +0700 Subject: [PATCH 2/3] added fallback locale to CsvAssetLoader constructor --- lib/src/csv_asset_loader.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/src/csv_asset_loader.dart b/lib/src/csv_asset_loader.dart index 8297609..ab85fe8 100644 --- a/lib/src/csv_asset_loader.dart +++ b/lib/src/csv_asset_loader.dart @@ -12,17 +12,19 @@ import 'asset_loader.dart'; // class CsvAssetLoader extends AssetLoader { CSVParser? csvParser; + final Locale? fallbackLocale; + + CsvAssetLoader(this.fallbackLocale); @override - Future> load(String path, Locale locale, - [Locale? localeForFillEmptyValues]) async { + Future> load(String path, Locale locale) async { if (csvParser == null) { log('easy localization loader: load csv file $path'); csvParser = CSVParser(await rootBundle.loadString(path)); } else { log('easy localization loader: CSV parser already loaded, read cache'); } - return csvParser!.getLanguageMap(locale.toString(), localeForFillEmptyValues?.toString()); + return csvParser!.getLanguageMap(locale.toString(), fallbackLocale?.toString()); } } From 012206062c71ddc4635bb8a6c8b4377002d4ec15 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 16 Jan 2023 19:36:01 +0700 Subject: [PATCH 3/3] make fallbackLocale parameter optional --- lib/src/csv_asset_loader.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/csv_asset_loader.dart b/lib/src/csv_asset_loader.dart index ab85fe8..9adb4ab 100644 --- a/lib/src/csv_asset_loader.dart +++ b/lib/src/csv_asset_loader.dart @@ -14,7 +14,7 @@ class CsvAssetLoader extends AssetLoader { CSVParser? csvParser; final Locale? fallbackLocale; - CsvAssetLoader(this.fallbackLocale); + CsvAssetLoader([this.fallbackLocale]); @override Future> load(String path, Locale locale) async {