Skip to content

Commit

Permalink
supoport partial translation key
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 4, 2024
1 parent c7b2567 commit bb55683
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/Commands/ShowDeadTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<fg=green>✔️</>");
// $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)
Expand Down
2 changes: 1 addition & 1 deletion src/Services/SearchCode/PhpParserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Feature/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
'dummy' => [
'class' => 'class factice',
'component' => 'composant factice',
'nested' => [
'used',
'as',
'array',
],
'view' => 'vue factice',
],
'empty' => 'Vide',
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/PhpParserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'messages.dummy.view',
],
"{$resourcesPath}/views/dummy-view.blade.php" => [
'messages.dummy.nested',
'messages.dummy.view',
'messages.dummy.view',
],
Expand Down Expand Up @@ -71,13 +72,20 @@
"{$resourcesPath}/components/dummy-component.blade.php",
],
],
'messages.dummy.nested' => [
'count' => 1,
'files' => [
"{$resourcesPath}/views/dummy-view.blade.php",
],
],
'messages.dummy.view' => [
'count' => 3,
'files' => [
"{$resourcesPath}/components/dummy-component.blade.php",
"{$resourcesPath}/views/dummy-view.blade.php",
],
],

]);
})->skipOnWindows();

Expand Down
5 changes: 5 additions & 0 deletions tests/src/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
'class' => 'dummy class',
'component' => 'dummy component',
'view' => 'dummy view',
'nested' => [
'used',
'as',
'array',
],
],
];
5 changes: 5 additions & 0 deletions tests/src/lang/fr/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
'class' => 'class factice',
'component' => 'composant factice',
'view' => 'vue factice',
'nested' => [
'used',
'as',
'array',
],
],
];
1 change: 1 addition & 0 deletions tests/src/resources/views/dummy-view.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div>
{{ __('messages.dummy.view') }}
{{ __('messages.dummy.view') }}
{{ __('messages.dummy.nested') }}
</div>

0 comments on commit bb55683

Please sign in to comment.