From 08f37bb44b727a791f3fd0fc0ac1d2e64962bb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ali=20Dikc=CC=A7=C4=B1nar?= Date: Fri, 21 Jun 2024 17:41:43 +0300 Subject: [PATCH 1/5] feat: custom code gen class name --- bin/generate.dart | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index cb006022..35d3905b 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -81,6 +81,14 @@ ArgParser _generateArgParser(GenerateOptions? generateOptions) { help: 'If true - Skip unnecessary keys of nested objects.', ); + parser.addOption( + 'class-name', + abbr: 'c', + defaultsTo: 'LocaleKeys', + callback: (String? x) => generateOptions!.className = x!, + help: 'Custom class name for generated file', + ); + return parser; } @@ -91,6 +99,7 @@ class GenerateOptions { String? outputDir; String? outputFile; String? format; + String? className; bool? skipUnnecessaryKeys; @override @@ -106,6 +115,7 @@ void handleLangFiles(GenerateOptions options) async { final sourcePath = Directory(path.join(current.path, source.path)); final outputPath = Directory(path.join(current.path, output.path, options.outputFile)); + final className = options.className ?? 'LocaleKeys'; if (!await sourcePath.exists()) { stderr.writeln('Source path does not exist'); @@ -126,7 +136,7 @@ void handleLangFiles(GenerateOptions options) async { } if (files.isNotEmpty) { - generateFile(files, outputPath, options); + generateFile(files, outputPath, options, className); } else { stderr.writeln('Source path empty'); } @@ -141,8 +151,12 @@ Future> dirContents(Directory dir) { return completer.future; } -void generateFile(List files, Directory outputPath, - GenerateOptions options) async { +void generateFile( + List files, + Directory outputPath, + GenerateOptions options, + String className, +) async { var generatedFile = File(outputPath.path); if (!generatedFile.existsSync()) { generatedFile.createSync(recursive: true); @@ -155,7 +169,8 @@ void generateFile(List files, Directory outputPath, await _writeJson(classBuilder, files); break; case 'keys': - await _writeKeys(classBuilder, files, options.skipUnnecessaryKeys); + await _writeKeys( + classBuilder, files, options.skipUnnecessaryKeys, className); break; // case 'csv': // await _writeCsv(classBuilder, files); @@ -170,12 +185,16 @@ void generateFile(List files, Directory outputPath, stdout.writeln('All done! File generated in ${outputPath.path}'); } -Future _writeKeys(StringBuffer classBuilder, List files, - bool? skipUnnecessaryKeys) async { +Future _writeKeys( + StringBuffer classBuilder, + List files, + bool? skipUnnecessaryKeys, + String className, +) async { var file = ''' // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart -abstract class LocaleKeys { +abstract class $className { '''; final fileData = File(files.first.path); From 5c07b8e05c3bb0c46c8385fdd0ca248d9576f963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ali=20Dikc=CC=A7=C4=B1nar?= Date: Fri, 21 Jun 2024 18:09:53 +0300 Subject: [PATCH 2/5] refactor: unneccesary parametters removed --- bin/generate.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index 35d3905b..06f539a6 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -115,8 +115,6 @@ void handleLangFiles(GenerateOptions options) async { final sourcePath = Directory(path.join(current.path, source.path)); final outputPath = Directory(path.join(current.path, output.path, options.outputFile)); - final className = options.className ?? 'LocaleKeys'; - if (!await sourcePath.exists()) { stderr.writeln('Source path does not exist'); return; @@ -136,7 +134,7 @@ void handleLangFiles(GenerateOptions options) async { } if (files.isNotEmpty) { - generateFile(files, outputPath, options, className); + generateFile(files, outputPath, options); } else { stderr.writeln('Source path empty'); } @@ -155,8 +153,8 @@ void generateFile( List files, Directory outputPath, GenerateOptions options, - String className, ) async { + final className = options.className ?? 'LocaleKeys'; var generatedFile = File(outputPath.path); if (!generatedFile.existsSync()) { generatedFile.createSync(recursive: true); From 5a71dc284bb558a5a3cd172673209ae36fb49f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ali=20Dik=C3=A7inar?= Date: Mon, 18 Aug 2025 22:51:03 +0300 Subject: [PATCH 3/5] fix: conflicts fixed --- bin/generate.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index 06f539a6..8691a93f 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -192,6 +192,8 @@ Future _writeKeys( var file = ''' // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart +// ignore_for_file: constant_identifier_names + abstract class $className { '''; @@ -251,7 +253,7 @@ Future _writeJson( var gFile = ''' // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart -// ignore_for_file: prefer_single_quotes, avoid_renaming_method_parameters +// ignore_for_file: prefer_single_quotes, avoid_renaming_method_parameters, constant_identifier_names import 'dart:ui'; @@ -272,13 +274,13 @@ class CodegenLoader extends AssetLoader{ for (var file in files) { final localeName = path.basename(file.path).replaceFirst('.json', '').replaceAll('-', '_'); - listLocales.add('"$localeName": $localeName'); + listLocales.add('"$localeName": _$localeName'); final fileData = File(file.path); Map? data = json.decode(await fileData.readAsString()); final mapString = const JsonEncoder.withIndent(' ').convert(data); - gFile += 'static const Map $localeName = $mapString;\n'; + gFile += 'static const Map _$localeName = $mapString;\n'; } gFile += From 650db22943c0815bfc8427ce7bdba736d197dff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ali=20Dikc=CC=A7inar?= Date: Sat, 30 Aug 2025 14:30:29 +0300 Subject: [PATCH 4/5] refactor: - Updated the class name callback to allow null values. - Enhanced the toString method to include className in GenerateOptions. - Trimmed whitespace from className before use. --- bin/generate.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index 8691a93f..d6a33c7b 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -85,8 +85,8 @@ ArgParser _generateArgParser(GenerateOptions? generateOptions) { 'class-name', abbr: 'c', defaultsTo: 'LocaleKeys', - callback: (String? x) => generateOptions!.className = x!, - help: 'Custom class name for generated file', + callback: (String? x) => generateOptions!.className = x, + help: 'Custom class name for generated keys class (keys format)', ); return parser; @@ -104,7 +104,13 @@ class GenerateOptions { @override String toString() { - return 'format: $format sourceDir: $sourceDir sourceFile: $sourceFile outputDir: $outputDir outputFile: $outputFile skipUnnecessaryKeys: $skipUnnecessaryKeys'; + return 'format: $format ' + 'sourceDir: $sourceDir ' + 'sourceFile: $sourceFile ' + 'outputDir: $outputDir ' + 'outputFile: $outputFile ' + 'skipUnnecessaryKeys: $skipUnnecessaryKeys ' + 'className: $className'; } } @@ -194,7 +200,7 @@ Future _writeKeys( // ignore_for_file: constant_identifier_names -abstract class $className { +abstract class $className { '''; final fileData = File(files.first.path); From e08cb29dca53b456d5791f6269086425794e61c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ali=20Dikc=CC=A7inar?= Date: Sat, 20 Sep 2025 16:19:18 +0300 Subject: [PATCH 5/5] chore: Release version 3.0.9 with new custom class name option for code generation --- CHANGELOG.md | 7 +++++++ README.md | 1 + pubspec.yaml | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf74e56..c7a0b499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### [3.0.9] + +- Added custom class name option for code generation (`--class-name` or `-c` parameter) + ### [3.0.8] - code audit and maintenance updates @@ -51,9 +55,11 @@ - **BREAKING**: removed context parameter from `plural()` and `tr()` - Added Formatting linked translations [more](https://github.com/aissat/easy_localization#linked-translations) - Updated `plural()` function, with arguments [more](https://github.com/aissat/easy_localization#linked-translations) + ```dart var money = plural('money_args', 10.23, args: ['John', '10.23']) // output: John has 10.23 dollars ``` + - Removed preloader widget ~~`preloaderWidget`~~ - fixed many issues. - customizable logger [EasyLogger] @@ -84,6 +90,7 @@ ```dart context.locale = locale; ``` + :information_source: No breaking changes, you can use old the static method `EasyLocalization.of(context)` ### [2.2.2] diff --git a/README.md b/README.md index 365930a2..ab526341 100644 --- a/README.md +++ b/README.md @@ -475,6 +475,7 @@ Code generation supports only json files, for more information run in terminal ` | --output-dir | -O | lib/generated | Output folder stores for the generated file | | --output-file | -o | codegen_loader.g.dart | Output file name | | --format | -f | json | Support json or keys formats | +| --class-name | -c | LocaleKeys | Custom class name for generated keys class | | --[no-]skip-unnecessary-keys | -u | false | Ignores keys defining nested object except for pluarl(), gender() keywords. | ### 🔌 Localization asset loader class diff --git a/pubspec.yaml b/pubspec.yaml index beed69fe..eaeaa376 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/aissat/easy_localization issue_tracker: https://github.com/aissat/easy_localization/issues # publish_to: none -version: 3.0.8 +version: 3.0.9 environment: sdk: '>=2.12.0 <4.0.0'