-
-
Notifications
You must be signed in to change notification settings - Fork 372
Feature/generator yaml support #772
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
854410e
cfa45f1
7505368
a5645ac
b82218d
a107608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import 'dart:io'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:args/args.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:path/path.dart' as path; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:yaml/yaml.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const _preservedKeywords = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'few', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,35 +44,45 @@ GenerateOptions _generateOption(List<String> args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArgParser _generateArgParser(GenerateOptions? generateOptions) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var parser = ArgParser(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption('source-dir', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'S', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'resources/langs', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.sourceDir = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Folder containing localization files'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption('source-file', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 's', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.sourceFile = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'File to use for localization'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption('output-dir', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'O', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'lib/generated', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.outputDir = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Output folder stores for the generated file'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption('output-file', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'o', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'codegen_loader.g.dart', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.outputFile = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Output file name'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption('format', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'f', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.format = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Support json or keys formats', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allowed: ['json', 'keys']); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'source-dir', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'S', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'resources/langs', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.sourceDir = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Folder containing localization files', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'source-file', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 's', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.sourceFile = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'File to use for localization', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'output-dir', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'O', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'lib/generated', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.outputDir = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Output folder stores for the generated file', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'output-file', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'o', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'codegen_loader.g.dart', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.outputFile = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Output file name', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addOption( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'format', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abbr: 'f', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultsTo: 'json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callback: (String? x) => generateOptions!.format = x, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help: 'Support json or keys formats', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allowed: ['json', 'keys'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.addFlag( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'skip-unnecessary-keys', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -104,8 +115,9 @@ void handleLangFiles(GenerateOptions options) async { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final source = Directory.fromUri(Uri.parse(options.sourceDir!)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final outputPath = Directory( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path.join(current.path, output.path, options.outputFile), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Treating a file path as a Directory. outputPath holds a file path (includes options.outputFile) but is typed/constructed as Directory. Use File to avoid semantic confusion and potential path issues. - final outputPath = Directory(
- path.join(current.path, output.path, options.outputFile),
- );
+ final outputFile = File(
+ path.join(current.path, output.path, options.outputFile),
+ );Also update the call site at Line 143 and the generateFile signature accordingly (see separate diffs). 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!await sourcePath.exists()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr.writeln('Source path does not exist'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -121,8 +133,10 @@ void handleLangFiles(GenerateOptions options) async { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files = [sourceFile]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //filtering format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files = files.where((f) => f.path.contains('.json')).toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Filter for .yml and .yaml files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files = files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .where((f) => f.path.endsWith('.yml') || f.path.endsWith('.yaml')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment on lines
125
to
127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Filter by selected format and ensure only files are processed. - //filtering format
- files = files.where((f) => f.path.contains(RegExp(r'\.(json|yaml|yml)$'))).toList();
+ // Filter files based on requested format; keep only regular files.
+ files = files
+ .whereType<File>()
+ .where((f) {
+ final p = f.path.toLowerCase();
+ switch (options.format) {
+ case 'json':
+ return p.endsWith('.json');
+ case 'yaml':
+ return p.endsWith('.yaml') || p.endsWith('.yml');
+ case 'keys':
+ return p.endsWith('.json') || p.endsWith('.yaml') || p.endsWith('.yml');
+ default:
+ return false;
+ }
+ })
+ .toList();📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (files.isNotEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -136,13 +150,18 @@ Future<List<FileSystemEntity>> dirContents(Directory dir) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var files = <FileSystemEntity>[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var completer = Completer<List<FileSystemEntity>>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var lister = dir.list(recursive: false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lister.listen((file) => files.add(file), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onDone: () => completer.complete(files)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lister.listen( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (file) => files.add(file), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onDone: () => completer.complete(files), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return completer.future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void generateFile(List<FileSystemEntity> files, Directory outputPath, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GenerateOptions options) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void generateFile( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<FileSystemEntity> files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Directory outputPath, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GenerateOptions options, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var generatedFile = File(outputPath.path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!generatedFile.existsSync()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Align generateFile API with a File destination. Update signature and usage to accept File instead of Directory. -void generateFile(
- List<FileSystemEntity> files,
- Directory outputPath,
- GenerateOptions options,
-) async {
- var generatedFile = File(outputPath.path);
+void generateFile(
+ List<FileSystemEntity> files,
+ File outputFile,
+ GenerateOptions options,
+) async {
+ var generatedFile = outputFile;
if (!generatedFile.existsSync()) {
generatedFile.createSync(recursive: true);
}And at Line 143: - generateFile(files, outputPath, options);
+ generateFile(files, outputFile, options);And at Line 186: - stdout.writeln('All done! File generated in ${outputPath.path}');
+ stdout.writeln('All done! File generated in ${outputFile.path}');📝 Committable suggestion
Suggested change
Suggested change
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| generatedFile.createSync(recursive: true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -157,9 +176,6 @@ void generateFile(List<FileSystemEntity> files, Directory outputPath, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'keys': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await _writeKeys(classBuilder, files, options.skipUnnecessaryKeys); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // case 'csv': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // await _writeCsv(classBuilder, files); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr.writeln('Format not supported'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -170,28 +186,36 @@ void generateFile(List<FileSystemEntity> files, Directory outputPath, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout.writeln('All done! File generated in ${outputPath.path}'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Future _writeKeys(StringBuffer classBuilder, List<FileSystemEntity> files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool? skipUnnecessaryKeys) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Future _writeKeys( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuffer classBuilder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<FileSystemEntity> files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool? skipUnnecessaryKeys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var file = ''' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ignore_for_file: constant_identifier_names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract class LocaleKeys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract class LocaleKeys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final fileData = File(files.first.path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic> translations = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json.decode(await fileData.readAsString()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Parse YAML file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final yamlString = await fileData.readAsString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final yamlData = loadYaml(yamlString); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic> translations = _yamlToMap(yamlData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Keys generation reads only YAML; restore JSON support and validate top-level structure. Use a shared parser that detects by extension and ensures a top-level map. - // Parse YAML file
- final yamlString = await fileData.readAsString();
- final yamlData = loadYaml(yamlString);
- Map<String, dynamic> translations = _yamlToMap(yamlData);
+ // Parse file (.json, .yml, .yaml)
+ final Map<String, dynamic> translations = await _parseTranslations(fileData);Additional helper to add (see snippet below in this comment thread). Add this helper outside the function (near _yamlToMap): // Parses .json, .yml, .yaml into a Map<String, dynamic>.
Future<Map<String, dynamic>> _parseTranslations(File file) async {
final ext = path.extension(file.path).toLowerCase();
final content = await file.readAsString();
if (ext == '.json') {
final decoded = json.decode(content);
if (decoded is! Map) {
throw FormatException('Top-level JSON must be an object');
}
return Map<String, dynamic>.from(decoded as Map);
}
final yamlRoot = loadYaml(content);
final map = _yamlToMap(yamlRoot);
if (map.isEmpty && yamlRoot is! YamlMap) {
throw FormatException('Top-level YAML must be a mapping');
}
return map;
}🤖 Prompt for AI Agents
Comment on lines
187
to
191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keys generation reads YAML only — add shared parser to support JSON and validate top-level. Without this, - Map<String, dynamic> translations =
- json.decode(json.encode(loadYaml(await fileData.readAsString())));
+ final Map<String, dynamic> translations = await _parseTranslations(fileData);Add this helper (place near // Parses .json, .yaml, .yml into Map<String, dynamic> and validates top-level map.
Future<Map<String, dynamic>> _parseTranslations(File file) async {
final ext = path.extension(file.path).toLowerCase();
final content = await file.readAsString();
if (ext == '.json') {
final decoded = json.decode(content);
if (decoded is! Map) {
throw const FormatException('Top-level JSON must be an object');
}
return Map<String, dynamic>.from(decoded as Map);
}
// YAML path
final yamlRoot = loadYaml(content);
// Normalize YamlMap/YamlList into JSON-friendly Map/List
final normalized = json.decode(json.encode(yamlRoot));
if (normalized is! Map) {
throw const FormatException('Top-level YAML must be a mapping');
}
return Map<String, dynamic>.from(normalized as Map);
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file += _resolve(translations, skipUnnecessaryKeys); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| classBuilder.writeln(file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String _resolve(Map<String, dynamic> translations, bool? skipUnnecessaryKeys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [String? accKey]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String _resolve( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic> translations, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool? skipUnnecessaryKeys, [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String? accKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var fileContent = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final sortedKeys = translations.keys.toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -205,34 +229,41 @@ String _resolve(Map<String, dynamic> translations, bool? skipUnnecessaryKeys, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var ignoreKey = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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>) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ignoreKey = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !containsPreservedKeywords( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| translations[key] as Map<String, dynamic>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| canIgnoreKeys; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var nextAccKey = key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (accKey != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nextAccKey = '$accKey.$key'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fileContent += | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _resolve(translations[key], skipUnnecessaryKeys, nextAccKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fileContent += _resolve( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| translations[key], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skipUnnecessaryKeys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nextAccKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fileContent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Future _writeJson( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuffer classBuilder, List<FileSystemEntity> files) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuffer classBuilder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<FileSystemEntity> files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var gFile = ''' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -242,7 +273,7 @@ import 'dart:ui'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:easy_localization/easy_localization.dart' show AssetLoader; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class CodegenLoader extends AssetLoader{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class CodegenLoader extends AssetLoader { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const CodegenLoader(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -255,12 +286,17 @@ class CodegenLoader extends AssetLoader{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final listLocales = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (var file in files) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final localeName = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path.basename(file.path).replaceFirst('.json', '').replaceAll('-', '_'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final localeName = path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .basename(file.path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .replaceFirst(RegExp(r'\.ya?ml$'), '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .replaceAll('-', '_'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| listLocales.add('"$localeName": _$localeName'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final fileData = File(file.path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic>? data = json.decode(await fileData.readAsString()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Parse YAML file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final yamlString = await fileData.readAsString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final yamlData = loadYaml(yamlString); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic> data = _yamlToMap(yamlData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final mapString = const JsonEncoder.withIndent(' ').convert(data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gFile += 'static const Map<String,dynamic> _$localeName = $mapString;\n'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -271,22 +307,17 @@ class CodegenLoader extends AssetLoader{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| classBuilder.writeln(gFile); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // _writeCsv(StringBuffer classBuilder, List<FileSystemEntity> files) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // List<String> listLocales = List(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // final fileData = File(files.first.path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // // CSVParser csvParser = CSVParser(await fileData.readAsString()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // // List listLangs = csvParser.getLanguages(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // for(String localeName in listLangs){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // listLocales.add('"$localeName": $localeName'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // String mapString = JsonEncoder.withIndent(" ").convert(csvParser.getLanguageMap(localeName)) ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // classBuilder.writeln( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ' static const Map<String,dynamic> $localeName = ${mapString};\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // classBuilder.writeln( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ' static const Map<String, Map<String,dynamic>> mapLocales = \{${listLocales.join(', ')}\};'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Helper function to convert YamlMap to Map<String, dynamic> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, dynamic> _yamlToMap(dynamic yaml) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (yaml is YamlMap) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return yaml.map((key, value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value is YamlMap || value is YamlList) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return MapEntry(key.toString(), _yamlToMap(value)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return MapEntry(key.toString(), value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (yaml is YamlList) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {'': yaml.map((e) => _yamlToMap(e)).toList()}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.