diff --git a/README.md b/README.md index 7b69082..cc92b7f 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,10 @@ php artisan vendor:publish --provider="Lukasss93\Larex\LarexServiceProvider" --t * You can use `php artisan larex:insert` to add new items via CLI too! * You can use `php artisan larex:import --include=en,it` to import only _"en"_ and _"it"_ items. * You can use `php artisan larex:import --exclude=it` to import all items except _"it"_ item. +* You can use `php artisan larex:import --normalize-folder-name=false` to keep the same csv header name as the folder name. (defaults to true) * You can use `php artisan larex:export --include=en,it` to export only _"en"_ and _"it"_ columns. * You can use `php artisan larex:export --exclude=it` to export all columns except _"it"_ column. +* You can use `php artisan larex:export --normalize-folder-name=false` to keep the same folder name as the csv header name. (defaults to true) * You can use `php artisan larex:localize` to find unlocalized strings (use the `--import` option to add strings in your CSV). * You can use `php artisan larex:find` to search existing groups or keys in your CSV file. diff --git a/src/Console/LarexExportCommand.php b/src/Console/LarexExportCommand.php index 795adf9..cd34257 100644 --- a/src/Console/LarexExportCommand.php +++ b/src/Console/LarexExportCommand.php @@ -19,7 +19,8 @@ class LarexExportCommand extends Command {exporter? : Exporter} {--watch : Watch the CSV file from changes} {--include= : Languages allowed to export in the application} - {--exclude= : Languages not allowed to export in the application}'; + {--exclude= : Languages not allowed to export in the application} + {--normalize-folder-name=true : Normalize the folder name from csv header or keep as is without transforms}'; /** * The console command description. diff --git a/src/Console/LarexImportCommand.php b/src/Console/LarexImportCommand.php index 049c4ee..bcf3108 100644 --- a/src/Console/LarexImportCommand.php +++ b/src/Console/LarexImportCommand.php @@ -23,7 +23,8 @@ class LarexImportCommand extends Command {--f|force : Overwrite CSV file if already exists} {--include= : Languages allowed to import in the CSV} {--exclude= : Languages not allowed to import in the CSV} - {--skip-source-reordering : Skip source reordering}'; + {--skip-source-reordering : Skip source reordering} + {--normalize-folder-name=true : Normalize the folder name from csv header or keep as is without transforms}'; /** * The console command description. diff --git a/src/Exporters/LaravelExporter.php b/src/Exporters/LaravelExporter.php index 356c88f..009e742 100644 --- a/src/Exporters/LaravelExporter.php +++ b/src/Exporters/LaravelExporter.php @@ -30,8 +30,10 @@ public function handle(LarexExportCommand $command, CsvReader $reader): int $command->warn($warning); } - $include = $command->option('include') !== null ? (explode(',', $command->option('include'))) : []; + $include = $command->option('include') !== null ? explode(',', $command->option('include')) : []; $exclude = $command->option('exclude') !== null ? explode(',', $command->option('exclude')) : []; + $normalizeFolderName = $command->option('normalize-folder-name') === 'true'; + $eol = config('larex.eol', PHP_EOL); //finally save the files @@ -45,7 +47,7 @@ public function handle(LarexExportCommand $command, CsvReader $reader): int } $found++; - $folder = str_replace('-', '_', $language); + $folder = $normalizeFolderName ? str_replace('-', '_', $language) : $language; if (!File::exists(lang_path("$folder/"))) { File::makeDirectory(lang_path("$folder/")); diff --git a/src/Importers/LaravelImporter.php b/src/Importers/LaravelImporter.php index 4e6cdff..985ea67 100644 --- a/src/Importers/LaravelImporter.php +++ b/src/Importers/LaravelImporter.php @@ -27,6 +27,7 @@ public function handle(LarexImportCommand $command): Collection { $include = Str::of($command->option('include'))->explode(',')->reject(fn ($i) => empty($i)); $exclude = Str::of($command->option('exclude'))->explode(',')->reject(fn ($i) => empty($i)); + $normalizeFolderName = $command->option('normalize-folder-name') === 'true'; /** @var Collection $languages */ $languages = collect([]); @@ -40,7 +41,7 @@ public function handle(LarexImportCommand $command): Collection foreach ($files as $file) { $items = include $file; $group = pathinfo($file, PATHINFO_FILENAME); - $lang = str_replace('_', '-', basename(dirname($file))); + $lang = $normalizeFolderName ? str_replace('_', '-', basename(dirname($file))) : basename(dirname($file)); if ($include->isNotEmpty() && !$include->contains($lang)) { continue; diff --git a/tests/Exporters/LaravelExporterTest.php b/tests/Exporters/LaravelExporterTest.php index f5c898c..f5f5f69 100644 --- a/tests/Exporters/LaravelExporterTest.php +++ b/tests/Exporters/LaravelExporterTest.php @@ -220,3 +220,63 @@ ->fileContent() ->toEqualStub('exporters.laravel.spaces.output-it'); }); + +it('creates folder with normalize option on', function () { + initFromStub('exporters.laravel.normalize.input'); + + $this->artisan(LarexExportCommand::class, ['exporter' => 'laravel', '--normalize-folder-name' => 'true']) + ->expectsOutput(sprintf("Processing the '%s' file...", csv_path(true))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('en/app.php'))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('it_100/app.php'))) + ->assertExitCode(0); + + expect(lang_path('en/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-en-app'); + + expect(lang_path('it_100/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-it-app'); +}); + +it('creates folder with normalize option on set by user', function () { + initFromStub('exporters.laravel.normalize.input'); + + $this->artisan(LarexExportCommand::class, ['exporter' => 'laravel']) + ->expectsOutput(sprintf("Processing the '%s' file...", csv_path(true))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('en/app.php'))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('it_100/app.php'))) + ->assertExitCode(0); + + expect(lang_path('en/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-en-app'); + + expect(lang_path('it_100/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-it-app'); +}); + +it('creates folder with normalize option off', function () { + initFromStub('exporters.laravel.normalize.input'); + + $this->artisan(LarexExportCommand::class, ['exporter' => 'laravel', '--normalize-folder-name' => 'false']) + ->expectsOutput(sprintf("Processing the '%s' file...", csv_path(true))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('en/app.php'))) + ->expectsOutput(sprintf('%s created successfully.', lang_rpath('it-100/app.php'))) + ->assertExitCode(0); + + expect(lang_path('en/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-en-app'); + + expect(lang_path('it-100/app.php')) + ->toBeFile() + ->fileContent() + ->toEqualStub('exporters.laravel.normalize.output-it-app'); +}); diff --git a/tests/Importers/LaravelImporterTest.php b/tests/Importers/LaravelImporterTest.php index 215877b..b382a54 100644 --- a/tests/Importers/LaravelImporterTest.php +++ b/tests/Importers/LaravelImporterTest.php @@ -111,3 +111,57 @@ function (string $source, string $expected, bool $skipSourceReordering) { 'en-skip' => ['en', 'importers.laravel.source.output-ar', true], 'invalid-lang' => ['es', 'importers.laravel.source.output-ar', false], ]); + +it('imports strings with normalize option on', function () { + File::makeDirectory(lang_path('en'), 0755, true, true); + File::makeDirectory(lang_path('it'), 0755, true, true); + + initFromStub('importers.laravel.normalize.input-en-simple', lang_path('en/simple.php')); + initFromStub('importers.laravel.normalize.input-it-simple', lang_path('it_100/simple.php')); + + $this->artisan(LarexImportCommand::class, ['importer' => 'laravel']) + ->expectsOutput('Importing entries...') + ->expectsOutput('Data imported successfully.') + ->assertExitCode(0); + + expect(csv_path()) + ->toBeFile() + ->fileContent() + ->toEqualStub('importers.laravel.normalize.output_normalized'); +}); + +it('imports strings with normalize option on set by user', function () { + File::makeDirectory(lang_path('en'), 0755, true, true); + File::makeDirectory(lang_path('it'), 0755, true, true); + + initFromStub('importers.laravel.normalize.input-en-simple', lang_path('en/simple.php')); + initFromStub('importers.laravel.normalize.input-it-simple', lang_path('it_100/simple.php')); + + $this->artisan(LarexImportCommand::class, ['importer' => 'laravel', '--normalize-folder-name' => 'true']) + ->expectsOutput('Importing entries...') + ->expectsOutput('Data imported successfully.') + ->assertExitCode(0); + + expect(csv_path()) + ->toBeFile() + ->fileContent() + ->toEqualStub('importers.laravel.normalize.output_normalized'); +}); + +it('imports strings with normalize option off', function () { + File::makeDirectory(lang_path('en'), 0755, true, true); + File::makeDirectory(lang_path('it'), 0755, true, true); + + initFromStub('importers.laravel.normalize.input-en-simple', lang_path('en/simple.php')); + initFromStub('importers.laravel.normalize.input-it-simple', lang_path('it_100/simple.php')); + + $this->artisan(LarexImportCommand::class, ['importer' => 'laravel', '--normalize-folder-name' => 'false']) + ->expectsOutput('Importing entries...') + ->expectsOutput('Data imported successfully.') + ->assertExitCode(0); + + expect(csv_path()) + ->toBeFile() + ->fileContent() + ->toEqualStub('importers.laravel.normalize.output_normalized_false'); +}); diff --git a/tests/Stubs/exporters/laravel/normalize/input.stub b/tests/Stubs/exporters/laravel/normalize/input.stub new file mode 100644 index 0000000..819f64b --- /dev/null +++ b/tests/Stubs/exporters/laravel/normalize/input.stub @@ -0,0 +1,14 @@ +group,key,en,it-100 +app,first,First,Primo +app,second,Second,Secondo +app,third,Third,Terzo +special,multi.a,A,a +special,multi.b,B,b +special,empty.escape,nope,"" +special,empty.noescape,nope, +special,enclosure,nope,"รจ ""molto"" bello" +special,numeric.1,January,Gennaio +special,numeric.2,February,Febbraio +special,numeric.3,March,Marzo +special,space.escape,nope," " +special,space.noescape,nope, diff --git a/tests/Stubs/exporters/laravel/normalize/output-en-app.stub b/tests/Stubs/exporters/laravel/normalize/output-en-app.stub new file mode 100644 index 0000000..a16bae4 --- /dev/null +++ b/tests/Stubs/exporters/laravel/normalize/output-en-app.stub @@ -0,0 +1,9 @@ + 'First', + 'second' => 'Second', + 'third' => 'Third', + +]; diff --git a/tests/Stubs/exporters/laravel/normalize/output-it-app.stub b/tests/Stubs/exporters/laravel/normalize/output-it-app.stub new file mode 100644 index 0000000..a56a9c2 --- /dev/null +++ b/tests/Stubs/exporters/laravel/normalize/output-it-app.stub @@ -0,0 +1,9 @@ + 'Primo', + 'second' => 'Secondo', + 'third' => 'Terzo', + +]; diff --git a/tests/Stubs/importers/laravel/normalize/input-en-simple.stub b/tests/Stubs/importers/laravel/normalize/input-en-simple.stub new file mode 100644 index 0000000..93b816d --- /dev/null +++ b/tests/Stubs/importers/laravel/normalize/input-en-simple.stub @@ -0,0 +1,7 @@ + 'Hello', + +]; \ No newline at end of file diff --git a/tests/Stubs/importers/laravel/normalize/input-it-simple.stub b/tests/Stubs/importers/laravel/normalize/input-it-simple.stub new file mode 100644 index 0000000..bb6f982 --- /dev/null +++ b/tests/Stubs/importers/laravel/normalize/input-it-simple.stub @@ -0,0 +1,8 @@ + 'Ciao', + 'bike' => 'Bicicletta' + +]; \ No newline at end of file diff --git a/tests/Stubs/importers/laravel/normalize/output_normalized.stub b/tests/Stubs/importers/laravel/normalize/output_normalized.stub new file mode 100644 index 0000000..0196c52 --- /dev/null +++ b/tests/Stubs/importers/laravel/normalize/output_normalized.stub @@ -0,0 +1,3 @@ +group,key,en,it-100 +simple,hello,Hello,Ciao +simple,bike,,Bicicletta diff --git a/tests/Stubs/importers/laravel/normalize/output_normalized_false.stub b/tests/Stubs/importers/laravel/normalize/output_normalized_false.stub new file mode 100644 index 0000000..5fe0f97 --- /dev/null +++ b/tests/Stubs/importers/laravel/normalize/output_normalized_false.stub @@ -0,0 +1,3 @@ +group,key,en,it_100 +simple,hello,Hello,Ciao +simple,bike,,Bicicletta