|
4 | 4 |
|
5 | 5 | use Elegantly\Translator\Facades\Translator;
|
6 | 6 | use Illuminate\Console\Command;
|
| 7 | +use Illuminate\Contracts\Console\PromptsForMissingInput; |
| 8 | +use Laravel\Prompts\Progress; |
| 9 | +use Laravel\Prompts\Table; |
| 10 | +use Symfony\Component\Console\Helper\TableCell; |
7 | 11 | use Symfony\Component\Console\Helper\TableSeparator;
|
8 | 12 |
|
9 |
| -class ShowDeadTranslationsCommand extends Command |
| 13 | +use function Laravel\Prompts\alert; |
| 14 | +use function Laravel\Prompts\multiselect; |
| 15 | +use function Laravel\Prompts\pause; |
| 16 | +use function Laravel\Prompts\progress; |
| 17 | + |
| 18 | +class ShowDeadTranslationsCommand extends Command implements PromptsForMissingInput |
10 | 19 | {
|
11 |
| - public $signature = 'translator:dead {--clear-cache}'; |
| 20 | + public $signature = 'translator:dead {locales* : The locales to scan} {--clear-cache}'; |
12 | 21 |
|
13 | 22 | public $description = 'Show all dead translations defined in translations files but not used in the codebase.';
|
14 | 23 |
|
15 |
| - public function handle(): int |
| 24 | + public function handleLocale(string $locale): array |
16 | 25 | {
|
17 |
| - if ($this->option('clear-cache')) { |
18 |
| - Translator::clearCache(); |
19 |
| - } |
20 | 26 |
|
21 |
| - $results = [ |
22 |
| - ['.blade.php', 0, 0], |
23 |
| - ['.php', 0, 0], |
24 |
| - ]; |
| 27 | + $namespaces = Translator::getNamespaces($locale); |
25 | 28 |
|
26 |
| - $bar = $this->output->createProgressBar(); |
| 29 | + return progress( |
| 30 | + "Scanning files: {$locale}", |
| 31 | + array_combine($namespaces, $namespaces), |
| 32 | + function (string $namespace, Progress $progress) use ($locale) { |
| 33 | + $progress->hint($namespace); |
27 | 34 |
|
28 |
| - $translations = Translator::getAllDeadTranslations( |
29 |
| - progress: function (string $file, array $translations) use (&$results, $bar) { |
| 35 | + $translations = Translator::getDeadTranslations( |
| 36 | + locale: $locale, |
| 37 | + namespace: $namespace, |
| 38 | + ); |
30 | 39 |
|
31 |
| - if (str($file)->endsWith('.blade.php')) { |
32 |
| - // $this->line($file); |
| 40 | + return [$namespace, $translations]; |
| 41 | + } |
| 42 | + ); |
| 43 | + } |
33 | 44 |
|
34 |
| - $results[0][2] += 1; |
35 |
| - if (count($translations)) { |
36 |
| - $results[0][1] += 1; |
37 |
| - } |
38 |
| - } elseif (str($file)->endsWith('.php')) { |
39 |
| - $results[1][2] += 1; |
40 |
| - if (count($translations)) { |
41 |
| - $results[1][1] += 1; |
42 |
| - } |
43 |
| - } |
| 45 | + public function displayLocale(string $locale) |
| 46 | + { |
| 47 | + $result = collect($this->handleLocale($locale)); |
| 48 | + |
| 49 | + alert($result->flatten()->count().' dead translations found.'); |
| 50 | + |
| 51 | + $table = new Table(['Keys'], []); |
| 52 | + |
| 53 | + $length = $result->count(); |
44 | 54 |
|
45 |
| - $bar->advance(); |
| 55 | + foreach ($result as $index => [$namespace, $items]) { |
| 56 | + $count = count($items); |
46 | 57 |
|
47 |
| - // $this->output->write("<fg=green>✔️</>"); |
48 |
| - // $this->output->write("."); |
| 58 | + $table->rows[] = ["<info>{$namespace}</info> : <fg=gray>{$count}</>"]; |
| 59 | + $table->rows[] = new TableSeparator; |
| 60 | + |
| 61 | + if (! $count) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + foreach ($items as $item) { |
| 66 | + $table->rows[] = [new TableCell("{$namespace}.{$item}")]; |
49 | 67 | }
|
50 |
| - ); |
51 | 68 |
|
52 |
| - $bar->finish(); |
| 69 | + if ($index < $length - 1) { |
| 70 | + $table->rows[] = new TableSeparator; |
| 71 | + } |
| 72 | + } |
53 | 73 |
|
54 |
| - $this->newLine(); |
| 74 | + $table->display(); |
| 75 | + } |
55 | 76 |
|
56 |
| - $this->table( |
57 |
| - headers: ['Type', 'With translations', 'Total'], |
58 |
| - rows: $results, |
59 |
| - ); |
| 77 | + public function handle(): int |
| 78 | + { |
| 79 | + if ($this->option('clear-cache')) { |
| 80 | + Translator::clearCache(); |
| 81 | + } |
60 | 82 |
|
61 |
| - $rows = collect($translations) |
62 |
| - ->flatMap( |
63 |
| - fn (array $namespaces, string $locale) => collect($namespaces) |
64 |
| - ->flatMap(function (array $keys, string $namespace) use ($locale, $namespaces) { |
65 |
| - $values = array_map(fn (string $key) => [$locale, "{$namespace}.$key"], $keys); |
| 83 | + $locales = $this->argument('locales'); |
66 | 84 |
|
67 |
| - if (array_key_last($namespaces) !== $namespace) { |
68 |
| - $values[] = [new TableSeparator, new TableSeparator]; |
69 |
| - } |
| 85 | + foreach ($locales as $index => $locale) { |
70 | 86 |
|
71 |
| - return $values; |
72 |
| - }) |
73 |
| - )->toArray(); |
| 87 | + $this->displayLocale($locale); |
74 | 88 |
|
75 |
| - $this->table( |
76 |
| - headers: ['Language', 'Dead key'], |
77 |
| - rows: $rows |
78 |
| - ); |
| 89 | + if ($nextLocale = $locales[$index + 1] ?? null) { |
| 90 | + pause("Press enter to continue with '{$nextLocale}'..."); |
| 91 | + } |
| 92 | + } |
79 | 93 |
|
80 | 94 | return self::SUCCESS;
|
81 | 95 | }
|
| 96 | + |
| 97 | + public function promptForMissingArgumentsUsing() |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'locales' => function () { |
| 101 | + return multiselect( |
| 102 | + label: 'What locales would you like to scan?', |
| 103 | + options: Translator::getLocales(), |
| 104 | + default: [config('app.locale')], |
| 105 | + required: true, |
| 106 | + ); |
| 107 | + }, |
| 108 | + |
| 109 | + ]; |
| 110 | + } |
82 | 111 | }
|
0 commit comments