diff --git a/src/Console/Commands/FindMissingTranslationStrings.php b/src/Console/Commands/FindMissingTranslationStrings.php index 42ba977..b5e9f7a 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; @@ -131,21 +132,31 @@ 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(); } - return Collection::make($this->files->files(lang_path($baseLocale))) - ->mapWithKeys(function (SplFileInfo $file) { - return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())]; - }) - ->dot() - ->keys() + + $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) { + return [$file->getFilenameWithoutExtension() => $this->translator->get($file->getFilenameWithoutExtension())]; + }) + ->toArray() + ) + ); + } + + return $data->keys() ->filter(function ($key) use ($locale) { return !$this->translator->hasForLocale($key, $locale); }); } - /** * @return mixed */ @@ -161,4 +172,4 @@ protected function collectFiles() return Str::endsWith($file->getExtension(), 'php'); }); } -} \ No newline at end of file +} diff --git a/tests/Console/Commands/FindMissingTranslationStringsTest.php b/tests/Console/Commands/FindMissingTranslationStringsTest.php new file mode 100644 index 0000000..842cafd --- /dev/null +++ b/tests/Console/Commands/FindMissingTranslationStringsTest.php @@ -0,0 +1,47 @@ +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 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