From bb55683057f6aac12076f32e1b8cadf026067a8e Mon Sep 17 00:00:00 2001 From: Quentin Gabriele Date: Sun, 4 Aug 2024 16:40:18 +0200 Subject: [PATCH] supoport partial translation key --- src/Commands/ShowDeadTranslationsCommand.php | 38 ++++++++++++++++++- src/Services/SearchCode/PhpParserService.php | 2 +- src/Translator.php | 5 ++- tests/Feature/TranslatorTest.php | 5 +++ tests/Unit/PhpParserServiceTest.php | 8 ++++ tests/src/lang/en/messages.php | 5 +++ tests/src/lang/fr/messages.php | 5 +++ .../src/resources/views/dummy-view.blade.php | 1 + 8 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Commands/ShowDeadTranslationsCommand.php b/src/Commands/ShowDeadTranslationsCommand.php index fbb2e47..d27372c 100644 --- a/src/Commands/ShowDeadTranslationsCommand.php +++ b/src/Commands/ShowDeadTranslationsCommand.php @@ -18,12 +18,46 @@ public function handle(): int Translator::clearCache(); } + $results = [ + ['.blade.php', 0, 0], + ['.php', 0, 0], + ]; + + $bar = $this->output->createProgressBar(); + $translations = Translator::getAllDeadTranslations( - progress: function (string $file, array $translations) { - $this->line($file); + progress: function (string $file, array $translations) use (&$results, $bar) { + + if (str($file)->endsWith('.blade.php')) { + // $this->line($file); + + $results[0][2] += 1; + if (count($translations)) { + $results[0][1] += 1; + } + } elseif (str($file)->endsWith('.php')) { + $results[1][2] += 1; + if (count($translations)) { + $results[1][1] += 1; + } + } + + $bar->advance(); + + // $this->output->write("✔️"); + // $this->output->write("."); } ); + $bar->finish(); + + $this->newLine(); + + $this->table( + headers: ['Type', 'With translations', 'Total'], + rows: $results, + ); + $rows = collect($translations) ->flatMap( fn (array $namespaces, string $locale) => collect($namespaces) diff --git a/src/Services/SearchCode/PhpParserService.php b/src/Services/SearchCode/PhpParserService.php index c57efc3..b699068 100644 --- a/src/Services/SearchCode/PhpParserService.php +++ b/src/Services/SearchCode/PhpParserService.php @@ -80,8 +80,8 @@ public static function scanCode(string $code): array return $value instanceof String_ ? $value->value : null; }) ->filter() - ->values() ->sort(SORT_NATURAL) + ->values() ->toArray(); } diff --git a/src/Translator.php b/src/Translator.php index fb68397..a020bc8 100755 --- a/src/Translator.php +++ b/src/Translator.php @@ -124,7 +124,10 @@ public function getDeadTranslations( $usedTranslationsKeys = array_keys($this->getFilesByUsedTranslations($service, $progress)); - return $definedTranslationsKeys->filter(fn (string $key) => ! in_array("{$namespace}.{$key}", $usedTranslationsKeys))->toArray(); + return $definedTranslationsKeys + ->reject(fn (string $key) => str("{$namespace}.{$key}")->startsWith($usedTranslationsKeys)) + ->values() + ->toArray(); } /** diff --git a/tests/Feature/TranslatorTest.php b/tests/Feature/TranslatorTest.php index 0001e27..f50d723 100644 --- a/tests/Feature/TranslatorTest.php +++ b/tests/Feature/TranslatorTest.php @@ -33,6 +33,11 @@ 'dummy' => [ 'class' => 'class factice', 'component' => 'composant factice', + 'nested' => [ + 'used', + 'as', + 'array', + ], 'view' => 'vue factice', ], 'empty' => 'Vide', diff --git a/tests/Unit/PhpParserServiceTest.php b/tests/Unit/PhpParserServiceTest.php index c5bb6d4..9d932d1 100644 --- a/tests/Unit/PhpParserServiceTest.php +++ b/tests/Unit/PhpParserServiceTest.php @@ -39,6 +39,7 @@ 'messages.dummy.view', ], "{$resourcesPath}/views/dummy-view.blade.php" => [ + 'messages.dummy.nested', 'messages.dummy.view', 'messages.dummy.view', ], @@ -71,6 +72,12 @@ "{$resourcesPath}/components/dummy-component.blade.php", ], ], + 'messages.dummy.nested' => [ + 'count' => 1, + 'files' => [ + "{$resourcesPath}/views/dummy-view.blade.php", + ], + ], 'messages.dummy.view' => [ 'count' => 3, 'files' => [ @@ -78,6 +85,7 @@ "{$resourcesPath}/views/dummy-view.blade.php", ], ], + ]); })->skipOnWindows(); diff --git a/tests/src/lang/en/messages.php b/tests/src/lang/en/messages.php index f692386..a9ce60b 100644 --- a/tests/src/lang/en/messages.php +++ b/tests/src/lang/en/messages.php @@ -12,5 +12,10 @@ 'class' => 'dummy class', 'component' => 'dummy component', 'view' => 'dummy view', + 'nested' => [ + 'used', + 'as', + 'array', + ], ], ]; diff --git a/tests/src/lang/fr/messages.php b/tests/src/lang/fr/messages.php index d96b69a..089ed6b 100644 --- a/tests/src/lang/fr/messages.php +++ b/tests/src/lang/fr/messages.php @@ -14,5 +14,10 @@ 'class' => 'class factice', 'component' => 'composant factice', 'view' => 'vue factice', + 'nested' => [ + 'used', + 'as', + 'array', + ], ], ]; diff --git a/tests/src/resources/views/dummy-view.blade.php b/tests/src/resources/views/dummy-view.blade.php index a8efd71..5b92f15 100644 --- a/tests/src/resources/views/dummy-view.blade.php +++ b/tests/src/resources/views/dummy-view.blade.php @@ -1,4 +1,5 @@
{{ __('messages.dummy.view') }} {{ __('messages.dummy.view') }} + {{ __('messages.dummy.nested') }}