From 5ca63dfc7f7ff2b9934eae19d02c787f212859b4 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 00:28:34 -0500 Subject: [PATCH 01/15] Add tests --- src/Console/InstallCommand.php | 5 +- tests/TestCase.php | 7 -- tests/Unit/InstallLanguageTest.php | 108 ++++++++++++++++++++++++ tests/Unit/PublishLanguageFilesTest.php | 58 ------------- tests/fixtures/.gitignore | 3 + tests/stubs/config/app.php | 28 ++++++ 6 files changed, 141 insertions(+), 68 deletions(-) create mode 100644 tests/Unit/InstallLanguageTest.php delete mode 100644 tests/Unit/PublishLanguageFilesTest.php create mode 100644 tests/fixtures/.gitignore create mode 100644 tests/stubs/config/app.php diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index daec348..79779e5 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -17,7 +17,7 @@ public function handle() { $locale = (string)$this->argument('locale'); - if (!in_array($locale, $this->getLocales())) { + if (!in_array($locale, $this->getLocales(base_path('vendor/laravel-lang/lang/locales')))) { $this->error("Language [{$locale}] is not supported!"); return; } @@ -66,11 +66,10 @@ private function loadJsonFile($locale) /** * @return array */ - protected function getLocales(): array + protected function getLocales(string $path): array { $filesystem = new Filesystem; - $path = base_path("vendor/laravel-lang/lang/locales"); $directories = $filesystem->directories($path); $locales = []; diff --git a/tests/TestCase.php b/tests/TestCase.php index 77889de..2aef2a8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,11 +13,4 @@ protected function getPackageProviders($app) LangServiceProvider::class ]; } - - protected function getPackageAliases($app) - { - return [ - // - ]; - } } diff --git a/tests/Unit/InstallLanguageTest.php b/tests/Unit/InstallLanguageTest.php new file mode 100644 index 0000000..772208c --- /dev/null +++ b/tests/Unit/InstallLanguageTest.php @@ -0,0 +1,108 @@ +app->setBasePath(__DIR__.'/../fixtures'); + + File::ensureDirectoryExists(config_path()); + copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php')); + } + + protected function tearDown(): void + { + parent::tearDown(); +// File::deleteDirectory(config_path()); + File::deleteDirectory(resource_path()); + } + + /** @test */ + function command_installs_supported_language() + { + $this->assertFalse(File::exists(resource_path('lang/xx_GB'))); + $this->assertFalse(File::exists(resource_path('lang/xx_GB/auth.php'))); + $this->assertFalse(File::exists(resource_path('lang/xx_GB/passwords.php'))); + $this->assertFalse(File::exists(resource_path('lang/xx_GB/pagination.php'))); + $this->assertFalse(File::exists(resource_path('lang/xx_GB/validation.php'))); + $this->assertFalse(File::exists(resource_path('lang/xx_GB.json'))); + + $this->artisan('lang:add xx_GB') + ->expectsOutput("Language [xx_GB] installed successfully as default.") + ->doesntExpectOutput("Language [es] installed successfully as default."); + + $this->assertTrue(File::exists(resource_path('lang/xx_GB'))); + $this->assertTrue(File::exists(resource_path('lang/xx_GB/auth.php'))); + $this->assertTrue(File::exists(resource_path('lang/xx_GB/passwords.php'))); + $this->assertTrue(File::exists(resource_path('lang/xx_GB/pagination.php'))); + $this->assertTrue(File::exists(resource_path('lang/xx_GB/validation.php'))); + $this->assertTrue(File::exists(resource_path('lang/xx_GB.json'))); + } + + /** @test */ + function command_installs_supported_language_with_validation_inline() + { + $this->artisan('lang:add es --inline') + ->expectsOutput("Language [es] installed successfully as default."); + } + + /** @test */ + function command_installs_spanish_language_by_default() + { + $this->artisan('lang:add') + ->expectsOutput("Language [es] installed successfully as default."); + } + + /** @test */ + function command_install_supported_language_with_validation_inline() + { + $this->artisan('lang:add es --inline') + ->expectsOutput("Language [es] installed successfully as default."); + } + + /** @test */ + function command_doesnt_install_language_if_not_supported() + { + $this->assertFalse(File::exists(resource_path('lang/no_valid'))); + $this->artisan('lang:add no-valid') + ->expectsOutput("Language [no-valid] is not supported!"); + + $this->assertFalse(File::exists(resource_path('lang/no_valid'))); + + $this->artisan('lang:add es') + ->doesntExpectOutput("Language [es] is not supported!"); + } + + /** @test */ + function command_modifies_config_app_locale_by_default() + { + $this->assertTrue(File::exists(config_path('app.php'))); + $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); + + $this->artisan('lang:add xx_GB'); + + $this->assertTrue(File::exists(config_path('app.php'))); + $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'xx_GB'")); + $this->assertFalse(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); + } + + /** @test */ + function command_doesnt_modify_config_app_locale_if_pass_no_default_option() + { + $this->assertTrue(File::exists(config_path('app.php'))); + $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); + + $this->artisan('lang:add xx_GB --no-default'); + + $this->assertTrue(File::exists(config_path('app.php'))); + $this->assertFalse(Str::contains(File::get(config_path('app.php')), "'locale' => 'xx_GB'")); + $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); + } +} diff --git a/tests/Unit/PublishLanguageFilesTest.php b/tests/Unit/PublishLanguageFilesTest.php deleted file mode 100644 index a0f5e0f..0000000 --- a/tests/Unit/PublishLanguageFilesTest.php +++ /dev/null @@ -1,58 +0,0 @@ -artisan('lang:add es') - ->expectsOutput("Language [es] installed successfully as default."); - - $this->artisan('lang:add fr') - ->expectsOutput("Language [fr] installed successfully as default."); - - $this->artisan('lang:add fr --no-default') - ->expectsOutput("Language [fr] installed successfully, but it isn't the default language."); - } - - /** @test */ - function validate_if_locale_directory_exists() - { - $locales = $this->getLocales(); - - $this->artisan('lang:add no-valid') - ->expectsOutput("Language [no-valid] is not supported!"); - -// dd($locales); - } - - /** - * @return array - */ - protected function getLocales(): array - { - $filesystem = new Filesystem; - - $path = __DIR__ . "/../../vendor/laravel-lang/lang/locales"; - $directories = $filesystem->directories($path); - - $locales = []; - - foreach ($directories as $directory) { - $locales[] = $filesystem->name($directory); - } - return $locales; - } -} diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore new file mode 100644 index 0000000..0acef46 --- /dev/null +++ b/tests/fixtures/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore +!/vendor/* diff --git a/tests/stubs/config/app.php b/tests/stubs/config/app.php new file mode 100644 index 0000000..ac791e6 --- /dev/null +++ b/tests/stubs/config/app.php @@ -0,0 +1,28 @@ + 'First Key', + + /* + |-------------------------------------------------------------------------- + | Application Locale Configuration + |-------------------------------------------------------------------------- + | + | Comment + | + */ + + 'locale' => 'en', + + /* + |-------------------------------------------------------------------------- + | Comment key 3 + |-------------------------------------------------------------------------- + | + | Comment + | + */ + + 'other' => 'other key' + +]; From 1a613353b0afc183d737233b795e722a68f0c156 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 00:47:25 -0500 Subject: [PATCH 02/15] fix .gitignore --- tests/Unit/InstallLanguageTest.php | 7 ------- tests/fixtures/.gitignore | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/Unit/InstallLanguageTest.php b/tests/Unit/InstallLanguageTest.php index 772208c..87a21cd 100644 --- a/tests/Unit/InstallLanguageTest.php +++ b/tests/Unit/InstallLanguageTest.php @@ -60,13 +60,6 @@ function command_installs_spanish_language_by_default() ->expectsOutput("Language [es] installed successfully as default."); } - /** @test */ - function command_install_supported_language_with_validation_inline() - { - $this->artisan('lang:add es --inline') - ->expectsOutput("Language [es] installed successfully as default."); - } - /** @test */ function command_doesnt_install_language_if_not_supported() { diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore index 0acef46..bdaebc8 100644 --- a/tests/fixtures/.gitignore +++ b/tests/fixtures/.gitignore @@ -1,3 +1,3 @@ * +!vendor/ !.gitignore -!/vendor/* From e6e67ec00582af265c12fe45fad9817da54eccf3 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 12:31:09 -0500 Subject: [PATCH 03/15] Add test fixtures --- tests/Unit/InstallLanguageTest.php | 2 +- tests/fixtures/.gitignore | 3 --- .../laravel-lang/lang/locales/es/auth.php | 5 +++++ .../laravel-lang/lang/locales/es/es.json | 9 ++++++++ .../lang/locales/es/pagination.php | 17 ++++++++++++++ .../lang/locales/es/passwords.php | 20 +++++++++++++++++ .../lang/locales/es/validation-inline.php | 12 ++++++++++ .../lang/locales/es/validation.php | 12 ++++++++++ .../laravel-lang/lang/locales/xx_GB/auth.php | 5 +++++ .../lang/locales/xx_GB/pagination.php | 19 ++++++++++++++++ .../lang/locales/xx_GB/passwords.php | 22 +++++++++++++++++++ .../lang/locales/xx_GB/validation-inline.php | 12 ++++++++++ .../lang/locales/xx_GB/validation.php | 12 ++++++++++ .../lang/locales/xx_GB/xx_GB.json | 9 ++++++++ 14 files changed, 155 insertions(+), 4 deletions(-) delete mode 100644 tests/fixtures/.gitignore create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/auth.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/pagination.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/passwords.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/validation-inline.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/es/validation.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/auth.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/pagination.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/passwords.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation-inline.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation.php create mode 100644 tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json diff --git a/tests/Unit/InstallLanguageTest.php b/tests/Unit/InstallLanguageTest.php index 87a21cd..ee996d5 100644 --- a/tests/Unit/InstallLanguageTest.php +++ b/tests/Unit/InstallLanguageTest.php @@ -20,7 +20,7 @@ protected function setUp(): void protected function tearDown(): void { parent::tearDown(); -// File::deleteDirectory(config_path()); + File::deleteDirectory(config_path()); File::deleteDirectory(resource_path()); } diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore deleted file mode 100644 index bdaebc8..0000000 --- a/tests/fixtures/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -* -!vendor/ -!.gitignore diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/auth.php b/tests/fixtures/vendor/laravel-lang/lang/locales/es/auth.php new file mode 100644 index 0000000..9a82999 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/auth.php @@ -0,0 +1,5 @@ + 'Estas credenciales no coinciden con nuestros registros.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json new file mode 100644 index 0000000..5bbe2f4 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json @@ -0,0 +1,9 @@ +{ + "Add": "Añadir", + "Cancel": "Cancelar", + "Card": "Tarjeta", + "Changes": "Cambios", + "Confirm Password": "Confirmar contraseña", + "Confirm your :amount payment": "Confirme su pago de :amount", + "If you already have an account, you may accept this invitation by clicking the button below:": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo:" +} diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/pagination.php b/tests/fixtures/vendor/laravel-lang/lang/locales/es/pagination.php new file mode 100644 index 0000000..d8f0d19 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/pagination.php @@ -0,0 +1,17 @@ + 'Siguiente »', + 'previous' => '« Anterior', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/passwords.php b/tests/fixtures/vendor/laravel-lang/lang/locales/es/passwords.php new file mode 100644 index 0000000..7745e64 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/passwords.php @@ -0,0 +1,20 @@ + '¡Su contraseña ha sido restablecida!', + 'sent' => '¡Le hemos enviado por correo electrónico el enlace para restablecer su contraseña!', + 'throttled' => 'Por favor espere antes de intentar de nuevo.', + 'token' => 'El token de restablecimiento de contraseña es inválido.', + 'user' => 'No encontramos ningún usuario con ese correo electrónico.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation-inline.php b/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation-inline.php new file mode 100644 index 0000000..ca7c0f3 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation-inline.php @@ -0,0 +1,12 @@ + 'Debe ser una fecha anterior o igual a :date.', + 'between' => [ + 'array' => 'El contenido debe tener entre :min y :max elementos.', + 'file' => 'Este archivo debe ser entre :min y :max kilobytes.', + 'numeric' => 'Este valor debe ser entre :min y :max.', + 'string' => 'El texto debe ser entre :min y :max caracteres.', + ], + 'boolean' => 'El campo debe ser verdadero o falso.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation.php b/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation.php new file mode 100644 index 0000000..ab23b4b --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/validation.php @@ -0,0 +1,12 @@ + ':attribute debe ser una fecha anterior o igual a :date.', + 'between' => [ + 'array' => ':attribute tiene que tener entre :min - :max elementos.', + 'file' => ':attribute debe pesar entre :min - :max kilobytes.', + 'numeric' => ':attribute tiene que estar entre :min - :max.', + 'string' => ':attribute tiene que tener entre :min - :max caracteres.', + ], + 'boolean' => 'El campo :attribute debe tener un valor verdadero o falso.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/auth.php b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/auth.php new file mode 100644 index 0000000..34016cd --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/auth.php @@ -0,0 +1,5 @@ + 'These credentials do not match our records.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/pagination.php b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/pagination.php new file mode 100644 index 0000000..d481411 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/pagination.php @@ -0,0 +1,19 @@ + '« Previous', + 'next' => 'Next »', + +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/passwords.php b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/passwords.php new file mode 100644 index 0000000..2345a56 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/passwords.php @@ -0,0 +1,22 @@ + 'Your password has been reset!', + 'sent' => 'We have emailed your password reset link!', + 'throttled' => 'Please wait before retrying.', + 'token' => 'This password reset token is invalid.', + 'user' => "We can't find a user with that email address.", + +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation-inline.php b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation-inline.php new file mode 100644 index 0000000..bb32573 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation-inline.php @@ -0,0 +1,12 @@ + 'This must be a date before or equal to :date.', + 'between' => [ + 'numeric' => 'This value must be between :min and :max.', + 'file' => 'This file must be between :min and :max kilobytes.', + 'string' => 'This string must be between :min and :max characters.', + 'array' => 'This content must have between :min and :max items.', + ], + 'boolean' => 'This field must be true or false.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation.php b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation.php new file mode 100644 index 0000000..049dace --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/validation.php @@ -0,0 +1,12 @@ + 'The :attribute must be a date before or equal to :date.', + 'between' => [ + 'numeric' => 'The :attribute must be between :min and :max.', + 'file' => 'The :attribute must be between :min and :max kilobytes.', + 'string' => 'The :attribute must be between :min and :max characters.', + 'array' => 'The :attribute must have between :min and :max items.', + ], + 'boolean' => 'The :attribute field must be true or false.', +]; diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json new file mode 100644 index 0000000..c08567c --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json @@ -0,0 +1,9 @@ +{ + "Add": "Add", + "Cancel": "Cancela", + "Card": "Card", + "Changes": "Changes", + "Confirm Password": "Confirm Password", + "Confirm your :amount payment": "Confirm your :amount payment", + "If you already have an account, you may accept this invitation by clicking the button below:": "If you already have an account, you may accept this invitation by clicking the button below:" +} From ccb9987d5a82362ee8a71bc3b6b8ceaa70afdcc5 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 12:38:45 -0500 Subject: [PATCH 04/15] Include gitignore --- tests/fixtures/.gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/fixtures/.gitignore diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore new file mode 100644 index 0000000..34a99be --- /dev/null +++ b/tests/fixtures/.gitignore @@ -0,0 +1,3 @@ +* +!vendor/* +!.gitignore \ No newline at end of file From 3f52f113670565f236ea66ade32a0c413c10db2c Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 12:42:21 -0500 Subject: [PATCH 05/15] Update test language --- .../fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json index c08567c..8b757b0 100644 --- a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json @@ -1,6 +1,6 @@ { "Add": "Add", - "Cancel": "Cancela", + "Cancel": "Cancel", "Card": "Card", "Changes": "Changes", "Confirm Password": "Confirm Password", From 410bff86b506f45eecedd561015f61f79fa87ea1 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 12:46:11 -0500 Subject: [PATCH 06/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a66676..837bc72 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ php artisan lang:add ar --inline ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. -This package does not modify the translations, only copies them of [`laravel-lang/lang`](https://github.com/Laravel-Lang/lang/). So if you want to suggest changes in the translations you can make a PR to the [`laravel-lang/lang` package](https://github.com/Laravel-Lang/lang/blob/master/docs/contributing-to-dev.md) +This package does not modify the translations, only copies them from [`laravel-lang/lang`](https://github.com/Laravel-Lang/lang/). So if you want to suggest changes in the translations you can make a PR to the [`laravel-lang/lang` package](https://github.com/Laravel-Lang/lang/blob/master/docs/contributing-to-dev.md) ## License [MIT](LICENSE.md) From 71b24cdadde29ec12727c2df9570daf5ebe50dc3 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 17:27:11 -0500 Subject: [PATCH 07/15] Build skeleton to emulate advanced tests --- tests/fixtures/.gitignore | 2 +- .../vendor/laravel-lang/lang/locales/es/es.json | 17 ++++++++++------- .../laravel-lang/lang/locales/xx_GB/xx_GB.json | 17 ++++++++++------- .../vendor/laravel-lang/lang/source/en.json | 6 ++++++ .../lang/source/packages/breeze.json | 6 ++++++ .../lang/source/packages/cashier.json | 5 +++++ 6 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/vendor/laravel-lang/lang/source/en.json create mode 100644 tests/fixtures/vendor/laravel-lang/lang/source/packages/breeze.json create mode 100644 tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json diff --git a/tests/fixtures/.gitignore b/tests/fixtures/.gitignore index 34a99be..3fe1a10 100644 --- a/tests/fixtures/.gitignore +++ b/tests/fixtures/.gitignore @@ -1,3 +1,3 @@ * !vendor/* -!.gitignore \ No newline at end of file +!.gitignore diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json index 5bbe2f4..ff19cfb 100644 --- a/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json @@ -1,9 +1,12 @@ { - "Add": "Añadir", - "Cancel": "Cancelar", - "Card": "Tarjeta", - "Changes": "Cambios", - "Confirm Password": "Confirmar contraseña", - "Confirm your :amount payment": "Confirme su pago de :amount", - "If you already have an account, you may accept this invitation by clicking the button below:": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo:" + "Add Base": "Añadir base", + "Cancel both": "Cancelar ambos", + "Card cashier": "Tarjeta cashier", + "Changes Base": "Cambios base", + "Confirm Password breeze": "Confirmar contraseña breeze", + "Confirm your :amount payment cashier": "Confirme su pago de :amount cashier", + "If you already have an account, you may accept this invitation by clicking the button below: base": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo: base", + "Log in breeze": "Iniciar sesión breeze", + "New :resource none": "Nuevo :resource ninguno", + "Whoops! all": "¡Ups! todo" } diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json index 8b757b0..07c8f60 100644 --- a/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/xx_GB/xx_GB.json @@ -1,9 +1,12 @@ { - "Add": "Add", - "Cancel": "Cancel", - "Card": "Card", - "Changes": "Changes", - "Confirm Password": "Confirm Password", - "Confirm your :amount payment": "Confirm your :amount payment", - "If you already have an account, you may accept this invitation by clicking the button below:": "If you already have an account, you may accept this invitation by clicking the button below:" + "Add Base": "Add base", + "Cancel both": "Cancel both", + "Card cashier": "Card cashier", + "Changes Base": "Changes base", + "Confirm Password breeze": "Confirm Password breeze", + "Confirm your :amount payment cashier": "Confirm your :amount payment cashier", + "If you already have an account, you may accept this invitation by clicking the button below: base": "If you already have an account, you may accept this invitation by clicking the button below: base", + "Log in breeze": "Log in breeze", + "New :resource none": "Nuevo :resource none", + "Whoops! all": "Whoops! all" } diff --git a/tests/fixtures/vendor/laravel-lang/lang/source/en.json b/tests/fixtures/vendor/laravel-lang/lang/source/en.json new file mode 100644 index 0000000..1dd7161 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/source/en.json @@ -0,0 +1,6 @@ +[ + "Add Base", + "Changes Base", + "If you already have an account, you may accept this invitation by clicking the button below: base", + "Whoops! all" +] diff --git a/tests/fixtures/vendor/laravel-lang/lang/source/packages/breeze.json b/tests/fixtures/vendor/laravel-lang/lang/source/packages/breeze.json new file mode 100644 index 0000000..92cb6c8 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/source/packages/breeze.json @@ -0,0 +1,6 @@ +[ + "Cancel both", + "Confirm Password breeze", + "Log in breeze", + "Whoops! all" +] diff --git a/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json b/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json new file mode 100644 index 0000000..46ea9f8 --- /dev/null +++ b/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json @@ -0,0 +1,5 @@ +[ + "Cancel both", + "Confirm your :amount payment cashier", + "Whoops! all" +] From aabc130355665c2276f5fca73958fb2ebc6dd55e Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 17:52:30 -0500 Subject: [PATCH 08/15] Rename tests --- tests/Unit/InstallLanguageTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/Unit/InstallLanguageTest.php b/tests/Unit/InstallLanguageTest.php index ee996d5..ade739e 100644 --- a/tests/Unit/InstallLanguageTest.php +++ b/tests/Unit/InstallLanguageTest.php @@ -11,7 +11,7 @@ class InstallLanguageTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->app->setBasePath(__DIR__.'/../fixtures'); + $this->app->setBasePath(__DIR__ . '/../fixtures'); File::ensureDirectoryExists(config_path()); copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php')); @@ -25,7 +25,7 @@ protected function tearDown(): void } /** @test */ - function command_installs_supported_language() + function installs_supported_language() { $this->assertFalse(File::exists(resource_path('lang/xx_GB'))); $this->assertFalse(File::exists(resource_path('lang/xx_GB/auth.php'))); @@ -47,21 +47,23 @@ function command_installs_supported_language() } /** @test */ - function command_installs_supported_language_with_validation_inline() + function installs_supported_language_with_validation_inline() { $this->artisan('lang:add es --inline') ->expectsOutput("Language [es] installed successfully as default."); + + //TODO: Test if generated file match } /** @test */ - function command_installs_spanish_language_by_default() + function installs_spanish_language_by_default() { $this->artisan('lang:add') ->expectsOutput("Language [es] installed successfully as default."); } /** @test */ - function command_doesnt_install_language_if_not_supported() + function doesnt_install_language_if_not_supported() { $this->assertFalse(File::exists(resource_path('lang/no_valid'))); $this->artisan('lang:add no-valid') @@ -74,7 +76,7 @@ function command_doesnt_install_language_if_not_supported() } /** @test */ - function command_modifies_config_app_locale_by_default() + function modifies_config_app_locale_by_default() { $this->assertTrue(File::exists(config_path('app.php'))); $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); @@ -87,7 +89,7 @@ function command_modifies_config_app_locale_by_default() } /** @test */ - function command_doesnt_modify_config_app_locale_if_pass_no_default_option() + function doesnt_modify_config_app_locale_if_pass_no_default_option() { $this->assertTrue(File::exists(config_path('app.php'))); $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); From 168f268283337c290d94077bc281fc88f944c512 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 22:19:15 -0500 Subject: [PATCH 09/15] Update editorconfig --- .editorconfig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 2e7acaf..0af7b6e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,4 +9,7 @@ indent_size = 4 trim_trailing_whitespace = true [*.md] -trim_trailing_whitespace = false \ No newline at end of file +trim_trailing_whitespace = false + +[*.json] +insert_final_newline = false \ No newline at end of file From 1e02bbc61d443f05fc143a91d1933ccc7767796c Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sat, 11 Sep 2021 22:24:05 -0500 Subject: [PATCH 10/15] Filter locale.json to basic locale translations --- src/Console/InstallCommand.php | 25 ++++++++++++++++++++++- tests/Unit/InstallLanguageTest.php | 32 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 79779e5..a2d0cfa 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Facades\File; class InstallCommand extends Command { @@ -60,10 +61,32 @@ protected function pregReplaceInFile($search, $replace, $path) private function loadJsonFile($locale) { - copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json"), resource_path("lang/{$locale}.json")); + $baseSource = json_decode(File::get(base_path('vendor/laravel-lang/lang/source/en.json'))); + $jsonLocale = json_decode(File::get(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json")), true); + + $modify = array_filter($jsonLocale, function ($item) use ($baseSource) { + return in_array($item, $baseSource); + }, ARRAY_FILTER_USE_KEY); + + $modifiedJson = json_encode($modify, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + +// dd($baseSource, $jsonLocale, $modifiedJson); +// $modifiedJson = <<assertFalse(Str::contains(File::get(config_path('app.php')), "'locale' => 'xx_GB'")); $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); } + + /** @test */ + function install_json_locale_only_with_base_translations() + { + $this->artisan('lang:add es'); + $expected = <<assertEquals( + $expected, + File::get(resource_path('lang/es.json')) + ); + + $this->artisan('lang:add xx_GB'); + $expected = <<assertEquals( + $expected, + File::get(resource_path('lang/xx_GB.json')) + ); + } } From 3b397211d63742a95a421d1dca29f73d53c8afb2 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sun, 12 Sep 2021 11:05:55 -0500 Subject: [PATCH 11/15] Rename tests --- tests/Unit/InstallLanguageTest.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/Unit/InstallLanguageTest.php b/tests/Unit/InstallLanguageTest.php index 2276166..563869a 100644 --- a/tests/Unit/InstallLanguageTest.php +++ b/tests/Unit/InstallLanguageTest.php @@ -25,7 +25,7 @@ protected function tearDown(): void } /** @test */ - function installs_supported_language() + function it_installs_supported_language() { $this->assertFalse(File::exists(resource_path('lang/xx_GB'))); $this->assertFalse(File::exists(resource_path('lang/xx_GB/auth.php'))); @@ -47,23 +47,31 @@ function installs_supported_language() } /** @test */ - function installs_supported_language_with_validation_inline() + function it_installs_supported_language_with_validation_inline() { $this->artisan('lang:add es --inline') ->expectsOutput("Language [es] installed successfully as default."); - //TODO: Test if generated file match + $this->assertStringContainsString( + 'El campo debe ser verdadero o falso.', + File::get(resource_path('lang/es/validation.php')) + ); + + $this->assertStringNotContainsString( + 'El campo :attribute debe tener un valor verdadero o falso.', + File::get(resource_path('lang/es/validation.php')) + ); } /** @test */ - function installs_spanish_language_by_default() + function it_installs_spanish_language_by_default() { $this->artisan('lang:add') ->expectsOutput("Language [es] installed successfully as default."); } /** @test */ - function doesnt_install_language_if_not_supported() + function it_doesnt_install_language_if_not_supported() { $this->assertFalse(File::exists(resource_path('lang/no_valid'))); $this->artisan('lang:add no-valid') @@ -76,7 +84,7 @@ function doesnt_install_language_if_not_supported() } /** @test */ - function modifies_config_app_locale_by_default() + function it_modifies_config_app_locale_by_default() { $this->assertTrue(File::exists(config_path('app.php'))); $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); @@ -89,7 +97,7 @@ function modifies_config_app_locale_by_default() } /** @test */ - function doesnt_modify_config_app_locale_if_pass_no_default_option() + function it_doesnt_modify_config_app_locale_if_pass_no_default_option() { $this->assertTrue(File::exists(config_path('app.php'))); $this->assertTrue(Str::contains(File::get(config_path('app.php')), "'locale' => 'en'")); @@ -102,7 +110,7 @@ function doesnt_modify_config_app_locale_if_pass_no_default_option() } /** @test */ - function install_json_locale_only_with_base_translations() + function it_install_json_locale_only_with_base_translations() { $this->artisan('lang:add es'); $expected = << Date: Sun, 12 Sep 2021 16:32:27 -0500 Subject: [PATCH 12/15] Update README.md --- README.md | 14 ++++- tests/Feature/DiscoverPackagesTest.php | 8 +++ .../{Unit => Feature}/InstallLanguageTest.php | 59 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/DiscoverPackagesTest.php rename tests/{Unit => Feature}/InstallLanguageTest.php (72%) diff --git a/README.md b/README.md index 837bc72..c88cae0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,16 @@ -# Laravel Lang Installer +

Laravel Lang Installer

+ +

+ + Total Downloads + + + Latest Stable Version + + + License + +

This package helps us to quickly install the language files in a preferably fresh Laravel application. diff --git a/tests/Feature/DiscoverPackagesTest.php b/tests/Feature/DiscoverPackagesTest.php new file mode 100644 index 0000000..3a2fcd2 --- /dev/null +++ b/tests/Feature/DiscoverPackagesTest.php @@ -0,0 +1,8 @@ +artisan('lang:add') + ->expectsOutput('composer.json not found!') + ->assertExitCode(0); + } + + /** @test */ + function it_discovers_supported_packages_installed_from_composer_json() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"', '"package/other": "^2.0"'], + ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] + )); + + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [breeze, cashier] packages merged!'); + + $this->setUp(); + + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"package/other": "^2.0"'], + ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] + )); + + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [breeze] package merged!'); + + $this->setUp(); + + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"'], [] + )); + + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [cashier] package merged!'); + } + + /** + * @param array $require + * @param array $requireDev + * @return string + */ + private function buildComposerWithDependencies(array $require = [], array $requireDev = []): string + { + $composerString = '{"name":"luisprmat/package","require":{'; + $composerString .= implode(',', $require); + $composerString .= '},"require-dev": {'; + $composerString .= implode(',', $requireDev); + $composerString .= '}}'; + + return json_encode( + json_decode($composerString), + JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES + ); + } } From adc3ff918b9754113cf27ac03aeba2be2aa40811 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Sun, 12 Sep 2021 21:48:03 -0500 Subject: [PATCH 13/15] Add package language discovery from composer.json --- src/Console/InstallCommand.php | 45 ++++++++++++- tests/Feature/DiscoverPackagesTest.php | 63 ++++++++++++++++++- tests/Feature/InstallLanguageTest.php | 63 +------------------ tests/TestCase.php | 21 ++++++- .../laravel-lang/lang/locales/es/es.json | 2 +- 5 files changed, 129 insertions(+), 65 deletions(-) diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index a2d0cfa..5517315 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; class InstallCommand extends Command { @@ -14,6 +15,13 @@ class InstallCommand extends Command protected $description = "Install translations for language 'locale' (default 'es')"; + private array $supportedPackages = [ + 'breeze' => 'laravel/breeze', + 'fortify' => 'laravel/fortify', + 'cashier' => 'laravel/cashier', + 'jetstream' => 'laravel/jetstream' + ]; + public function handle() { $locale = (string)$this->argument('locale'); @@ -23,6 +31,11 @@ public function handle() return; } + if (!File::exists(base_path('composer.json'))) { + $this->error('composer.json not found!'); + return; + } + (new Filesystem)->ensureDirectoryExists(resource_path("lang/{$locale}")); copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/auth.php"), resource_path("lang/{$locale}/auth.php")); @@ -35,7 +48,8 @@ public function handle() copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/validation.php"), resource_path("lang/{$locale}/validation.php")); } - $this->loadJsonFile($locale); + $discoveredPackages = $this->discoveredPackages(); + $this->loadJsonFile($locale, $discoveredPackages); if (!$this->option('no-default')) { // Set config('app.locale') @@ -44,6 +58,13 @@ public function handle() } else { $this->info("Language [{$locale}] installed successfully, but it isn't the default language."); } + + if (!empty($discoveredPackages)) { + $this->info( + 'Translations for ['. implode(', ', $discoveredPackages) .'] ' + . Str::plural('package', count($discoveredPackages)) .' merged!' + ); + } } /** @@ -59,7 +80,7 @@ protected function pregReplaceInFile($search, $replace, $path) file_put_contents($path, preg_replace($search, $replace, file_get_contents($path))); } - private function loadJsonFile($locale) + private function loadJsonFile($locale, $packages = []) { $baseSource = json_decode(File::get(base_path('vendor/laravel-lang/lang/source/en.json'))); $jsonLocale = json_decode(File::get(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json")), true); @@ -102,4 +123,24 @@ protected function getLocales(string $path): array } return $locales; } + + /** + * Returns list of installed packages that are supported according to composer.json + * + * @return array + */ + protected function discoveredPackages(): array + { + $composer = json_decode(File::get(base_path('composer.json')), true); + + $jsonToCreate = array_keys(array_merge($composer['require'], $composer['require-dev'])); + + $packagesToInstall = array_filter($jsonToCreate, function ($package) { + return in_array($package, $this->supportedPackages); + }); + + return array_keys(array_filter($this->supportedPackages, function ($package) use ($packagesToInstall) { + return in_array($package, $packagesToInstall); + })); + } } diff --git a/tests/Feature/DiscoverPackagesTest.php b/tests/Feature/DiscoverPackagesTest.php index 3a2fcd2..614ce5e 100644 --- a/tests/Feature/DiscoverPackagesTest.php +++ b/tests/Feature/DiscoverPackagesTest.php @@ -2,7 +2,68 @@ namespace Luisprmat\LaravelLangInstaller\Tests\Feature; -class DiscoverPackagesTest +use Illuminate\Support\Facades\File; +use Luisprmat\LaravelLangInstaller\Tests\TestCase; + +class DiscoverPackagesTest extends TestCase { + protected function setUp(): void + { + parent::setUp(); + $this->app->setBasePath(__DIR__ . '/../fixtures'); + + File::ensureDirectoryExists(config_path()); + File::copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php')); + } + + protected function tearDown(): void + { + parent::tearDown(); + File::deleteDirectory(config_path()); + File::deleteDirectory(resource_path()); + File::delete(base_path('composer.json')); + } + + /** @test */ + function it_doesnt_execute_if_composer_json_doesnt_exist() + { + $this->artisan('lang:add') + ->expectsOutput('composer.json not found!') + ->assertExitCode(0); + } + + /** @test */ + function it_discovers_several_supported_packages_installed_from_composer_json() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"', '"package/other": "^2.0"'], + ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] + )); + + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [breeze, cashier] packages merged!'); + } + + /** @test */ + function it_discovers_one_supported_package_installed_from_composer_json_require_dev() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"package/other": "^2.0"'], + ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] + )); + + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [breeze] package merged!'); + } + + /** @test */ + function it_discovers_one_supported_package_installed_from_composer_json_require() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"'] + )); + $command = $this->artisan('lang:add'); + $command->expectsOutput('Translations for [cashier] package merged!'); + } } diff --git a/tests/Feature/InstallLanguageTest.php b/tests/Feature/InstallLanguageTest.php index 19b96d1..999220c 100644 --- a/tests/Feature/InstallLanguageTest.php +++ b/tests/Feature/InstallLanguageTest.php @@ -1,6 +1,6 @@ app->setBasePath(__DIR__ . '/../fixtures'); File::ensureDirectoryExists(config_path()); - copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php')); + File::copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php')); + File::put(base_path('composer.json'), $this->buildComposerWithDependencies(['"any/package": "^1.0"'])); } protected function tearDown(): void @@ -141,62 +142,4 @@ function it_install_json_locale_only_with_base_translations() File::get(resource_path('lang/xx_GB.json')) ); } - - /** @test */ - function it_doesnt_execute_if_composer_json_doesnt_exist() - { - $this->artisan('lang:add') - ->expectsOutput('composer.json not found!') - ->assertExitCode(0); - } - - /** @test */ - function it_discovers_supported_packages_installed_from_composer_json() - { - File::put(base_path('composer.json'), $this->buildComposerWithDependencies( - ['"laravel/cashier": "^13.5"', '"package/other": "^2.0"'], - ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] - )); - - $command = $this->artisan('lang:add'); - $command->expectsOutput('Translations for [breeze, cashier] packages merged!'); - - $this->setUp(); - - File::put(base_path('composer.json'), $this->buildComposerWithDependencies( - ['"package/other": "^2.0"'], - ['"laravel/breeze": "^1.4"', '"laravel/no-supported": "^1.0"'] - )); - - $command = $this->artisan('lang:add'); - $command->expectsOutput('Translations for [breeze] package merged!'); - - $this->setUp(); - - File::put(base_path('composer.json'), $this->buildComposerWithDependencies( - ['"laravel/cashier": "^13.5"'], [] - )); - - $command = $this->artisan('lang:add'); - $command->expectsOutput('Translations for [cashier] package merged!'); - } - - /** - * @param array $require - * @param array $requireDev - * @return string - */ - private function buildComposerWithDependencies(array $require = [], array $requireDev = []): string - { - $composerString = '{"name":"luisprmat/package","require":{'; - $composerString .= implode(',', $require); - $composerString .= '},"require-dev": {'; - $composerString .= implode(',', $requireDev); - $composerString .= '}}'; - - return json_encode( - json_decode($composerString), - JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES - ); - } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 2aef2a8..700bc88 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,7 +5,7 @@ use Luisprmat\LaravelLangInstaller\LangServiceProvider; use Orchestra\Testbench\TestCase as BaseTestCase; -class TestCase extends BaseTestCase +abstract class TestCase extends BaseTestCase { protected function getPackageProviders($app) { @@ -13,4 +13,23 @@ protected function getPackageProviders($app) LangServiceProvider::class ]; } + + /** + * @param array $require + * @param array $requireDev + * @return string + */ + protected function buildComposerWithDependencies(array $require = [], array $requireDev = []): string + { + $composerString = '{"name":"luisprmat/package","require":{'; + $composerString .= implode(',', $require); + $composerString .= '},"require-dev": {'; + $composerString .= implode(',', $requireDev); + $composerString .= '}}'; + + return json_encode( + json_decode($composerString), + JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES + ); + } } diff --git a/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json index ff19cfb..da10f38 100644 --- a/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json +++ b/tests/fixtures/vendor/laravel-lang/lang/locales/es/es.json @@ -9,4 +9,4 @@ "Log in breeze": "Iniciar sesión breeze", "New :resource none": "Nuevo :resource ninguno", "Whoops! all": "¡Ups! todo" -} +} \ No newline at end of file From b6b9b866c8caa3e4a75da16453bb474daf80764f Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Mon, 13 Sep 2021 00:21:40 -0500 Subject: [PATCH 14/15] Optimize [locale].json with only required translations --- src/Console/InstallCommand.php | 34 ++++---- tests/Feature/InstallLanguageTest.php | 82 ++++++++++++++++++- .../lang/source/packages/cashier.json | 1 + 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 5517315..7c0d236 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -49,6 +49,12 @@ public function handle() } $discoveredPackages = $this->discoveredPackages(); + + // Add 'fortify' translations if 'jetstream' is installed + if (in_array('jetstream', $discoveredPackages)) { + array_push($discoveredPackages, 'fortify'); + } + $this->loadJsonFile($locale, $discoveredPackages); if (!$this->option('no-default')) { @@ -85,24 +91,24 @@ private function loadJsonFile($locale, $packages = []) $baseSource = json_decode(File::get(base_path('vendor/laravel-lang/lang/source/en.json'))); $jsonLocale = json_decode(File::get(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json")), true); - $modify = array_filter($jsonLocale, function ($item) use ($baseSource) { - return in_array($item, $baseSource); + $showTags = $baseSource; + + foreach ($packages as $package) { + $showTags = array_merge( + $showTags, + json_decode(File::get(base_path("vendor/laravel-lang/lang/source/packages/{$package}.json"))) + ); + } + + $showTags = array_unique($showTags); + sort($showTags); + + $modify = array_filter($jsonLocale, function ($item) use ($showTags) { + return in_array($item, $showTags); }, ARRAY_FILTER_USE_KEY); $modifiedJson = json_encode($modify, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); -// dd($baseSource, $jsonLocale, $modifiedJson); -// $modifiedJson = <<artisan('lang:add es'); $expected = <<buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"'] + )); + + $this->artisan('lang:add es'); + $expected = <<assertEquals( + $expected, + File::get(resource_path('lang/es.json')) + ); + + } + + /** @test */ + function it_installs_json_with_base_plus_discovered_packages_other() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/breeze": "^1.4"'] + )); + + $this->artisan('lang:add es'); + $expected = <<assertEquals( + $expected, + File::get(resource_path('lang/es.json')) + ); + + } + + /** @test */ + function it_installs_json_with_base_plus_discovered_packages_mixed() + { + File::put(base_path('composer.json'), $this->buildComposerWithDependencies( + ['"laravel/cashier": "^13.5"', '"laravel/breeze": "^1.4"'] + )); + + $this->artisan('lang:add es'); + $expected = <<assertEquals( + $expected, + File::get(resource_path('lang/es.json')) + ); + + } } diff --git a/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json b/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json index 46ea9f8..71bc1e3 100644 --- a/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json +++ b/tests/fixtures/vendor/laravel-lang/lang/source/packages/cashier.json @@ -1,5 +1,6 @@ [ "Cancel both", + "Card cashier", "Confirm your :amount payment cashier", "Whoops! all" ] From 65af63b2fdeded0abccf4e571919f2bb0a9d4dd8 Mon Sep 17 00:00:00 2001 From: Luis Antonio Parrado Date: Mon, 13 Sep 2021 01:32:48 -0500 Subject: [PATCH 15/15] Update README.md --- README.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c88cae0..cc810a7 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,19 @@ composer require luisprmat/laravel-lang-installer --dev ## Usage -After install a new laravel application with `Laravel >= 5.5` the package autodiscover system will register the new command `lang:add`. +### Add new language +After install a new laravel application with `Laravel >= 5.5` the package autodiscover system will register the new command `lang:add` and you can call with -This command can take a unique argument (or none) that will be the short name of the language according to **ISO 15897**. +```bash +php artisan lang:add +``` +where `` refers to the short name of any of the [supported languages](README.md#supported-languages) +> ### *Warnings* +> - **Add lang** action overwrites the language files so that you already had custom translations you could lose them. +> - When adding a language this package first consults the `composer.json` file to copy only the translations of the supported packages that are installed ([Laravel Breeze](https://laravel.com/docs/8.x/starter-kits#laravel-breeze), [Laravel Cashier](https://laravel.com/docs/8.x/billing), [Laravel Fortify](https://laravel.com/docs/8.x/fortify) and [Laravel Jetstream](https://jetstream.laravel.com/2.x/introduction.html) are supported) `resources/lang/.json`. So it is good that you first install the supported packages that you will use and then run the command `php artisan lang:add ` +> - If this command does not receive arguments, the Spanish language [`es`] will be installed by default. -If this command does not receive arguments, the Spanish language [`es`] will be installed by default. +This command can take a unique argument (or none) that will be the short name of the language according to **ISO 15897**. This command also modifies the key `locale` in the `config/app.php` file to set the default language as passed through the parameter. @@ -63,7 +71,14 @@ php artisan lang:add pt_BR --no-default php artisan lang:add ar --inline ``` - +## Supported languages +`af`, `ar`, `az`, `be`, `bg`, `bn`, `bs`, `ca`, `cs`, `cy`, `da`, `de`, `de_CH` +, `el`, `es`, `et`, `eu`, `fa`, `fi`, `fil`, `fr`, `gl`, `he`, `hi`, `hr`, `hu`, +`hy`, `id`, `is`, `it`, `ja`, `ka`, `kk`, `km`, `kn`, `ko`, `lt`, `lv`, `mk`, ` +mn`, `mr`, `ms`, `nb`, `ne`, `nl`, `nn`, `oc`, `pl`, `ps`, `pt`, `pt_BR`, `ro`, +`ru`, `sc`, `si`, `sk`, `sl`, `sq`, `sr_Cyrl`, `sr_Latn`, `sr_Latn_ME`, `sv`, `s +w`, `tg`, `th`, `tk`, `tl`, `tr`, `ug`, `uk`, `ur`, `uz_Cyrl`, `uz_Latn`, `vi`, +`zh_CN`, `zh_HK`, `zh_TW` ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. @@ -72,3 +87,8 @@ This package does not modify the translations, only copies them from [`laravel-l ## License [MIT](LICENSE.md) + +## Todo + +- [ ] Allow merge translations instead of overwrite them. +- [ ] Add Command `lang:update` to update translations and detect new installed packages to update their translations.