From 8e3271e7046a5d64f3a8ef97b2d4b4762243df16 Mon Sep 17 00:00:00 2001 From: levinyu Date: Sun, 9 Jun 2024 12:52:22 +0900 Subject: [PATCH 1/2] =?UTF-8?q?update=20>=20Json=20list=20=EB=8C=80?= =?UTF-8?q?=EC=9D=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/generate.dart | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index cb006022..6d5af8dd 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -105,7 +105,7 @@ void handleLangFiles(GenerateOptions options) async { final output = Directory.fromUri(Uri.parse(options.outputDir!)); final sourcePath = Directory(path.join(current.path, source.path)); final outputPath = - Directory(path.join(current.path, output.path, options.outputFile)); + Directory(path.join(current.path, output.path, options.outputFile)); if (!await sourcePath.exists()) { stderr.writeln('Source path does not exist'); @@ -157,9 +157,9 @@ void generateFile(List files, Directory outputPath, case 'keys': await _writeKeys(classBuilder, files, options.skipUnnecessaryKeys); break; - // case 'csv': - // await _writeCsv(classBuilder, files); - // break; + // case 'csv': + // await _writeCsv(classBuilder, files); + // break; default: stderr.writeln('Format not supported'); } @@ -180,8 +180,11 @@ abstract class LocaleKeys { final fileData = File(files.first.path); - Map translations = - json.decode(await fileData.readAsString()); + final result = json.decode(await fileData.readAsString()); + + Map translations = result is List + ? convertJsonListToMap(result) + : result; file += _resolve(translations, skipUnnecessaryKeys); @@ -204,7 +207,7 @@ String _resolve(Map translations, bool? skipUnnecessaryKeys, if (translations[key] is Map) { // If key does not contain keys for plural(), gender() etc. and option is enabled -> ignore it ignoreKey = !containsPreservedKeywords( - translations[key] as Map) && + translations[key] as Map) && canIgnoreKeys; var nextAccKey = key; @@ -219,10 +222,10 @@ String _resolve(Map translations, bool? skipUnnecessaryKeys, if (!_preservedKeywords.contains(key)) { accKey != null && !ignoreKey ? fileContent += - ' static const ${accKey.replaceAll('.', '_')}_$key = \'$accKey.$key\';\n' + ' static const ${accKey.replaceAll('.', '_')}_$key = \'$accKey.$key\';\n' : !ignoreKey - ? fileContent += ' static const $key = \'$key\';\n' - : null; + ? fileContent += ' static const $key = \'$key\';\n' + : null; } } @@ -254,21 +257,36 @@ class CodegenLoader extends AssetLoader{ for (var file in files) { final localeName = - path.basename(file.path).replaceFirst('.json', '').replaceAll('-', '_'); + path.basename(file.path).replaceFirst('.json', '').replaceAll('-', '_'); listLocales.add('"$localeName": $localeName'); final fileData = File(file.path); - Map? data = json.decode(await fileData.readAsString()); + final result = json.decode(await fileData.readAsString()); + + Map? data = result is List + ? convertJsonListToMap(result) + : result; final mapString = const JsonEncoder.withIndent(' ').convert(data); gFile += 'static const Map $localeName = $mapString;\n'; } gFile += - 'static const Map> mapLocales = {${listLocales.join(', ')}};'; + 'static const Map> mapLocales = {${listLocales.join(', ')}};'; classBuilder.writeln(gFile); } +Map convertJsonListToMap(List data) { + final Map result = {}; + + for (dynamic rows in data) { + Map row = rows as Map; + result[row[row.keys.first]] = row; + } + + return result; +} + // _writeCsv(StringBuffer classBuilder, List files) async { // List listLocales = List(); // final fileData = File(files.first.path); From b21e88cfcafa584eece920e31828b68a8c72649a Mon Sep 17 00:00:00 2001 From: levinyu Date: Sun, 9 Jun 2024 13:53:54 +0900 Subject: [PATCH 2/2] =?UTF-8?q?update=20>=20Json=20list=20=EB=8C=80?= =?UTF-8?q?=EC=9D=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/generate.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/generate.dart b/bin/generate.dart index 6d5af8dd..8cb54e1b 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -263,6 +263,8 @@ class CodegenLoader extends AssetLoader{ final result = json.decode(await fileData.readAsString()); + + Map? data = result is List ? convertJsonListToMap(result) : result;