Skip to content

Commit 66be241

Browse files
committed
better command
1 parent bb55683 commit 66be241

File tree

2 files changed

+81
-51
lines changed

2 files changed

+81
-51
lines changed

src/Commands/ShowDeadTranslationsCommand.php

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,108 @@
44

55
use Elegantly\Translator\Facades\Translator;
66
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;
711
use Symfony\Component\Console\Helper\TableSeparator;
812

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
1019
{
11-
public $signature = 'translator:dead {--clear-cache}';
20+
public $signature = 'translator:dead {locales* : The locales to scan} {--clear-cache}';
1221

1322
public $description = 'Show all dead translations defined in translations files but not used in the codebase.';
1423

15-
public function handle(): int
24+
public function handleLocale(string $locale): array
1625
{
17-
if ($this->option('clear-cache')) {
18-
Translator::clearCache();
19-
}
2026

21-
$results = [
22-
['.blade.php', 0, 0],
23-
['.php', 0, 0],
24-
];
27+
$namespaces = Translator::getNamespaces($locale);
2528

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);
2734

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+
);
3039

31-
if (str($file)->endsWith('.blade.php')) {
32-
// $this->line($file);
40+
return [$namespace, $translations];
41+
}
42+
);
43+
}
3344

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();
4454

45-
$bar->advance();
55+
foreach ($result as $index => [$namespace, $items]) {
56+
$count = count($items);
4657

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}")];
4967
}
50-
);
5168

52-
$bar->finish();
69+
if ($index < $length - 1) {
70+
$table->rows[] = new TableSeparator;
71+
}
72+
}
5373

54-
$this->newLine();
74+
$table->display();
75+
}
5576

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+
}
6082

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');
6684

67-
if (array_key_last($namespaces) !== $namespace) {
68-
$values[] = [new TableSeparator, new TableSeparator];
69-
}
85+
foreach ($locales as $index => $locale) {
7086

71-
return $values;
72-
})
73-
)->toArray();
87+
$this->displayLocale($locale);
7488

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+
}
7993

8094
return self::SUCCESS;
8195
}
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+
}
82111
}

src/Facades/Translator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* @method static Translations fixGrammarTranslations(string $locale, string $namespace, array $keys, ?GrammarServiceInterface $service)
1818
* @method static array<int, string> getMissingTranslations(string $referenceLocale, string $targetLocale, string $namespace)
1919
* @method static array getAllMissingTranslations(string $referenceLocale)
20-
* @method static array getAllDeadTranslations( null|(Closure(string $file, string[] $translations):void) $progress )
20+
* @method static array getAllDeadTranslations( null|(Closure(string $file, string[] $translations):void) $progress = null )
21+
* @method static array getDeadTranslations(string $locale, string $namespace, ?SearchCodeServiceInterface $service = null, null|(Closure(string $file, string[] $translations):void) $progress = null, ?array $ignore = null )
2122
* @method static void sortAllTranslations()
2223
* @method static \Elegantly\Translator\Translator clearCache()
2324
*

0 commit comments

Comments
 (0)