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

feat: introduce a normalize flag to be able to skip the current folder/header normalize step #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/Console/LarexExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/Console/LarexImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/Exporters/LaravelExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/"));
Expand Down
3 changes: 2 additions & 1 deletion src/Importers/LaravelImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,string> $languages */
$languages = collect([]);
Expand All @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions tests/Exporters/LaravelExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
54 changes: 54 additions & 0 deletions tests/Importers/LaravelImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
14 changes: 14 additions & 0 deletions tests/Stubs/exporters/laravel/normalize/input.stub
Original file line number Diff line number Diff line change
@@ -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,
9 changes: 9 additions & 0 deletions tests/Stubs/exporters/laravel/normalize/output-en-app.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

'first' => 'First',
'second' => 'Second',
'third' => 'Third',

];
9 changes: 9 additions & 0 deletions tests/Stubs/exporters/laravel/normalize/output-it-app.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

'first' => 'Primo',
'second' => 'Secondo',
'third' => 'Terzo',

];
7 changes: 7 additions & 0 deletions tests/Stubs/importers/laravel/normalize/input-en-simple.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'hello' => 'Hello',

];
8 changes: 8 additions & 0 deletions tests/Stubs/importers/laravel/normalize/input-it-simple.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [

'hello' => 'Ciao',
'bike' => 'Bicicletta'

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
group,key,en,it-100
simple,hello,Hello,Ciao
simple,bike,,Bicicletta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
group,key,en,it_100
simple,hello,Hello,Ciao
simple,bike,,Bicicletta