From c11cdf419c4271773f7a2b64f75e9ea29ef8268f Mon Sep 17 00:00:00 2001 From: John Boehr Date: Mon, 19 May 2025 17:55:24 -0700 Subject: [PATCH 1/5] Add an artisan test via orchestra/testbench --- .../FindMissingTranslationStringsTest.php | 54 +++++++++++++++++++ tests/application/lang/en.json | 4 ++ tests/application/lang/en/messages.php | 3 ++ tests/application/lang/ja.json | 4 ++ tests/application/lang/ja/messages.php | 3 ++ .../resources/views/sample.blade.php | 1 + 6 files changed, 69 insertions(+) create mode 100644 tests/Console/Commands/FindMissingTranslationStringsTest.php create mode 100644 tests/application/lang/en.json create mode 100644 tests/application/lang/en/messages.php create mode 100644 tests/application/lang/ja.json create mode 100644 tests/application/lang/ja/messages.php create mode 100644 tests/application/resources/views/sample.blade.php diff --git a/tests/Console/Commands/FindMissingTranslationStringsTest.php b/tests/Console/Commands/FindMissingTranslationStringsTest.php new file mode 100644 index 0000000..e8ec6d9 --- /dev/null +++ b/tests/Console/Commands/FindMissingTranslationStringsTest.php @@ -0,0 +1,54 @@ +withoutMockingConsoleOutput() + ->artisan('lost-in-translation:find ja --no-progress --sorted'); + + $output = Artisan::output(); + + $this->assertSame("foobar\nglobal_key\nmessages.namespaced_key\n", $output); + $this->assertSame(0, $exit_code); + } + + public function testMissingLocale(): void + { + $exit_code = $this + ->withoutMockingConsoleOutput() + ->artisan('lost-in-translation:find zh --no-progress --sorted'); + + $output = Artisan::output(); + + $this->assertSame("foobar\nglobal_key\nkey_in_both\nmessages.namespaced_key\n", $output); + $this->assertSame(0, $exit_code); + } + + public function getPackageProviders($app) + { + return [ + LostInTranslationServiceProvider::class, + ]; + } + + public function defineEnvironment($app) + { + $appPath = __DIR__ . '/../../application'; + $app->useLangPath($appPath . '/lang'); + + tap($app['config'], static function (Repository $config) use ($appPath) { + $config->set('lost-in-translation.paths', [ + $appPath . '/resources/views' + ]); + }); + } +} diff --git a/tests/application/lang/en.json b/tests/application/lang/en.json new file mode 100644 index 0000000..c2bf198 --- /dev/null +++ b/tests/application/lang/en.json @@ -0,0 +1,4 @@ +{ + "global_key": "this is a global key", + "key_in_both": "both should have this key" +} \ No newline at end of file diff --git a/tests/application/lang/en/messages.php b/tests/application/lang/en/messages.php new file mode 100644 index 0000000..34b5e7c --- /dev/null +++ b/tests/application/lang/en/messages.php @@ -0,0 +1,3 @@ + 'this is a namespaced key', +]; \ No newline at end of file diff --git a/tests/application/lang/ja.json b/tests/application/lang/ja.json new file mode 100644 index 0000000..8f0a109 --- /dev/null +++ b/tests/application/lang/ja.json @@ -0,0 +1,4 @@ +{ + "another_global_key": "this is another global key", + "key_in_both": "both should have this key" +} \ No newline at end of file diff --git a/tests/application/lang/ja/messages.php b/tests/application/lang/ja/messages.php new file mode 100644 index 0000000..15cce78 --- /dev/null +++ b/tests/application/lang/ja/messages.php @@ -0,0 +1,3 @@ + 'this is another namespaced key', +]; \ No newline at end of file diff --git a/tests/application/resources/views/sample.blade.php b/tests/application/resources/views/sample.blade.php new file mode 100644 index 0000000..1cfaac7 --- /dev/null +++ b/tests/application/resources/views/sample.blade.php @@ -0,0 +1 @@ +{{ __('foobar') }} \ No newline at end of file From 041b59a6014e9348fed9d93a3d09fef4bd4d8dc3 Mon Sep 17 00:00:00 2001 From: John Boehr Date: Mon, 19 May 2025 17:55:50 -0700 Subject: [PATCH 2/5] Also include lang in lang/*.json --- src/Console/Commands/FindMissingTranslationStrings.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Console/Commands/FindMissingTranslationStrings.php b/src/Console/Commands/FindMissingTranslationStrings.php index 42ba977..447ff8f 100644 --- a/src/Console/Commands/FindMissingTranslationStrings.php +++ b/src/Console/Commands/FindMissingTranslationStrings.php @@ -135,11 +135,13 @@ protected function findInArray(string $baseLocale, mixed $locale) if ($baseLocale === $locale) { return Collection::empty(); } + return Collection::make($this->files->files(lang_path($baseLocale))) ->mapWithKeys(function (SplFileInfo $file) { return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())]; }) ->dot() + ->merge($this->translator->get('*')) ->keys() ->filter(function ($key) use ($locale) { return !$this->translator->hasForLocale($key, $locale); @@ -161,4 +163,4 @@ protected function collectFiles() return Str::endsWith($file->getExtension(), 'php'); }); } -} \ No newline at end of file +} From 8ed4f0f6e9246d1bc1dc714632cb3929b665b3eb Mon Sep 17 00:00:00 2001 From: John Boehr Date: Mon, 19 May 2025 18:04:25 -0700 Subject: [PATCH 3/5] Collection::dot() not available in Laravel 9, use Arr::dot() --- .../FindMissingTranslationStrings.php | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Console/Commands/FindMissingTranslationStrings.php b/src/Console/Commands/FindMissingTranslationStrings.php index 447ff8f..5bb4a77 100644 --- a/src/Console/Commands/FindMissingTranslationStrings.php +++ b/src/Console/Commands/FindMissingTranslationStrings.php @@ -8,6 +8,7 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Translation\Translator; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Symfony\Component\Console\Formatter\OutputFormatter; @@ -136,13 +137,18 @@ protected function findInArray(string $baseLocale, mixed $locale) return Collection::empty(); } - return Collection::make($this->files->files(lang_path($baseLocale))) - ->mapWithKeys(function (SplFileInfo $file) { - return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())]; - }) - ->dot() - ->merge($this->translator->get('*')) - ->keys() + $data = Collection::make($this->translator->get('*')) + ->merge( + Arr::dot( + Collection::make($this->files->files(lang_path($baseLocale))) + ->mapWithKeys(function (SplFileInfo $file) { + return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())]; + }) + ->toArray() + ) + ); + + return $data->keys() ->filter(function ($key) use ($locale) { return !$this->translator->hasForLocale($key, $locale); }); From de7c88d98279435e10e2667e7b772d1d0c4f27ae Mon Sep 17 00:00:00 2001 From: John Boehr Date: Mon, 19 May 2025 18:08:27 -0700 Subject: [PATCH 4/5] Prevent error when only a json language file is present --- src/Console/Commands/FindMissingTranslationStrings.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/FindMissingTranslationStrings.php b/src/Console/Commands/FindMissingTranslationStrings.php index 5bb4a77..b5e9f7a 100644 --- a/src/Console/Commands/FindMissingTranslationStrings.php +++ b/src/Console/Commands/FindMissingTranslationStrings.php @@ -132,13 +132,16 @@ protected function printErrors(array $errors, OutputInterface $output): void * @return \Illuminate\Support\Collection */ protected function findInArray(string $baseLocale, mixed $locale) + { if ($baseLocale === $locale) { return Collection::empty(); } - $data = Collection::make($this->translator->get('*')) - ->merge( + $data = Collection::make($this->translator->get('*')); + + if ($this->files->exists(lang_path($baseLocale))) { + $data = $data->merge( Arr::dot( Collection::make($this->files->files(lang_path($baseLocale))) ->mapWithKeys(function (SplFileInfo $file) { @@ -147,13 +150,13 @@ protected function findInArray(string $baseLocale, mixed $locale) ->toArray() ) ); + } return $data->keys() ->filter(function ($key) use ($locale) { return !$this->translator->hasForLocale($key, $locale); }); } - /** * @return mixed */ From 80e24d97bd1bd1fe81bc9411ac011e54350ba0e2 Mon Sep 17 00:00:00 2001 From: John Boehr Date: Tue, 20 May 2025 09:08:07 -0700 Subject: [PATCH 5/5] Use base TestCase --- .../Commands/FindMissingTranslationStringsTest.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/Console/Commands/FindMissingTranslationStringsTest.php b/tests/Console/Commands/FindMissingTranslationStringsTest.php index e8ec6d9..842cafd 100644 --- a/tests/Console/Commands/FindMissingTranslationStringsTest.php +++ b/tests/Console/Commands/FindMissingTranslationStringsTest.php @@ -3,9 +3,9 @@ namespace CodingSocks\LostInTranslation\Tests\Console\Commands; use CodingSocks\LostInTranslation\LostInTranslationServiceProvider; +use CodingSocks\LostInTranslation\Tests\TestCase; use Illuminate\Config\Repository; use Illuminate\Support\Facades\Artisan; -use Orchestra\Testbench\TestCase; class FindMissingTranslationStringsTest extends TestCase { @@ -33,13 +33,6 @@ public function testMissingLocale(): void $this->assertSame(0, $exit_code); } - public function getPackageProviders($app) - { - return [ - LostInTranslationServiceProvider::class, - ]; - } - public function defineEnvironment($app) { $appPath = __DIR__ . '/../../application';