Skip to content

Commit

Permalink
feat: introduce a normalize flag to be able to skip the current folde…
Browse files Browse the repository at this point in the history
…r/header normalize step
  • Loading branch information
CoolGoose committed May 5, 2024
1 parent a62e6d9 commit 29c4f3b
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 24 deletions.
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
9 changes: 6 additions & 3 deletions 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 Expand Up @@ -113,8 +114,10 @@ public function handle(): int

//set source languages
if (!$this->option('skip-source-reordering')) {
$this->callSilently(LarexLangOrderCommand::class,
['from' => config('larex.source_language', 'en'), 'to' => 1]);
$this->callSilently(
LarexLangOrderCommand::class,
['from' => config('larex.source_language', 'en'), 'to' => 1]
);
}

$this->info('Data imported successfully.');
Expand Down
22 changes: 14 additions & 8 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,15 +47,19 @@ 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/"));
}

foreach ($groups as $group => $keys) {
$write = fopen(lang_path("$folder/$group.php"), 'wb');
fwrite($write, /** @lang text */ "<?php$eol{$eol}return [$eol$eol");
fwrite(
$write,
/** @lang text */
"<?php$eol{$eol}return [$eol$eol"
);

foreach ($keys as $key => $value) {
self::writeKeyValue($key, $value, $write, 1, $eol);
Expand All @@ -78,24 +84,24 @@ protected static function writeKeyValue($key, $value, &$file, int $level = 1, $e
$enclosure = '"';

if (is_array($value)) {
fwrite($file, str_repeat(' ', $level)."'$key' => [$eol");
fwrite($file, str_repeat(' ', $level) . "'$key' => [$eol");
$level++;
foreach ($value as $childKey => $childValue) {
self::writeKeyValue($childKey, $childValue, $file, $level, $eol);
}
fwrite($file, str_repeat(' ', $level - 1)."],$eol");
fwrite($file, str_repeat(' ', $level - 1) . "],$eol");

return;
}

$value = (string)$value;
$value = str_replace(["'", '\\'.$enclosure], ["\'", $enclosure], $value);
$value = str_replace(["'", '\\' . $enclosure], ["\'", $enclosure], $value);

if (is_int($key) || (is_numeric($key) && ctype_digit($key))) {
$key = (int)$key;
fwrite($file, str_repeat(' ', $level)."$key => '$value',$eol");
fwrite($file, str_repeat(' ', $level) . "$key => '$value',$eol");
} else {
fwrite($file, str_repeat(' ', $level)."'$key' => '$value',$eol");
fwrite($file, str_repeat(' ', $level) . "'$key' => '$value',$eol");
}
}
}
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
64 changes: 62 additions & 2 deletions tests/Exporters/LaravelExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@

$this->artisan(LarexExportCommand::class, ['exporter' => 'laravel', '--exclude' => 'en'])
->expectsOutput(sprintf("Processing the '%s' file...", csv_path(true)))
->expectsOutput(lang_rpath('it/app.php').' created successfully.')
->expectsOutput(lang_rpath('it/another.php').' created successfully.')
->expectsOutput(lang_rpath('it/app.php') . ' created successfully.')
->expectsOutput(lang_rpath('it/another.php') . ' created successfully.')
->assertExitCode(0);

expect(lang_path('en/app.php'))->not->toBeFile();
Expand Down 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');
});
76 changes: 67 additions & 9 deletions tests/Importers/LaravelImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
->toEqualStub('importers.laravel.territory.output');
});

it('imports strings and set the source language',
it(
'imports strings and set the source language',
function (string $source, string $expected, bool $skipSourceReordering) {
File::makeDirectory(lang_path('ar'), 0755, true, true);
File::makeDirectory(lang_path('en'), 0755, true, true);
Expand All @@ -95,8 +96,10 @@ function (string $source, string $expected, bool $skipSourceReordering) {

config(['larex.source_language' => $source]);

$this->artisan(LarexImportCommand::class,
['importer' => 'laravel', '--skip-source-reordering' => $skipSourceReordering])
$this->artisan(
LarexImportCommand::class,
['importer' => 'laravel', '--skip-source-reordering' => $skipSourceReordering]
)
->expectsOutput('Importing entries...')
->expectsOutput('Data imported successfully.')
->assertExitCode(0);
Expand All @@ -105,9 +108,64 @@ function (string $source, string $expected, bool $skipSourceReordering) {
->toBeFile()
->fileContent()
->toEqualStub($expected);
})->with([
'ar' => ['ar', 'importers.laravel.source.output-ar', false],
'en' => ['en', 'importers.laravel.source.output-en', false],
'en-skip' => ['en', 'importers.laravel.source.output-ar', true],
'invalid-lang' => ['es', 'importers.laravel.source.output-ar', false],
]);
}
)->with([
'ar' => ['ar', 'importers.laravel.source.output-ar', false],
'en' => ['en', 'importers.laravel.source.output-en', false],
'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

0 comments on commit 29c4f3b

Please sign in to comment.