Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Json List #686

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions bin/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -157,9 +157,9 @@ void generateFile(List<FileSystemEntity> 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');
}
Expand All @@ -180,8 +180,11 @@ abstract class LocaleKeys {

final fileData = File(files.first.path);

Map<String, dynamic> translations =
json.decode(await fileData.readAsString());
final result = json.decode(await fileData.readAsString());

Map<String, dynamic> translations = result is List<dynamic>
? convertJsonListToMap(result)
: result;

file += _resolve(translations, skipUnnecessaryKeys);

Expand All @@ -204,7 +207,7 @@ String _resolve(Map<String, dynamic> 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<String, dynamic>) &&
translations[key] as Map<String, dynamic>) &&
canIgnoreKeys;

var nextAccKey = key;
Expand All @@ -219,10 +222,10 @@ String _resolve(Map<String, dynamic> 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;
}
}

Expand Down Expand Up @@ -254,21 +257,38 @@ 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<String, dynamic>? data = json.decode(await fileData.readAsString());
final result = json.decode(await fileData.readAsString());



Map<String, dynamic>? data = result is List<dynamic>
? convertJsonListToMap(result)
: result;

final mapString = const JsonEncoder.withIndent(' ').convert(data);
gFile += 'static const Map<String,dynamic> $localeName = $mapString;\n';
}

gFile +=
'static const Map<String, Map<String,dynamic>> mapLocales = {${listLocales.join(', ')}};';
'static const Map<String, Map<String,dynamic>> mapLocales = {${listLocales.join(', ')}};';
classBuilder.writeln(gFile);
}

Map<String, dynamic> convertJsonListToMap(List<dynamic> data) {
final Map<String, dynamic> result = {};

for (dynamic rows in data) {
Map<String, dynamic> row = rows as Map<String, dynamic>;
result[row[row.keys.first]] = row;
}

return result;
}

// _writeCsv(StringBuffer classBuilder, List<FileSystemEntity> files) async {
// List<String> listLocales = List();
// final fileData = File(files.first.path);
Expand Down