Skip to content

feat: [Commands] lang:find show bad keys when scanning #8149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions system/Commands/Translation/LocalizationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private function process(string $currentDir, string $currentLocale): void
$files = iterator_to_array($iterator, true);
ksort($files);

[$foundLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files);
[$foundLanguageKeys, $badLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files);
ksort($foundLanguageKeys);

$languageDiff = [];
Expand Down Expand Up @@ -155,14 +155,28 @@ private function process(string $currentDir, string $currentLocale): void

$this->writeIsVerbose('Files found: ' . $countFiles);
$this->writeIsVerbose('New translates found: ' . $countNewKeys);
$this->writeIsVerbose('Bad translates found: ' . count($badLanguageKeys));

if ($this->verbose && $badLanguageKeys !== []) {
$tableBadRows = [];

foreach ($badLanguageKeys as $key => $value) {
$tableBadRows[] = [$key, $value];
}

CLI::table($tableBadRows, ['№', 'Bad key']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you need the No. for? I want the file paths.

Suggested change
CLI::table($tableBadRows, ['', 'Bad key']);
CLI::table($tableBadRows, ['', 'Bad Key']);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number is just for understanding which line. I think file paths are not an option to show. Since one language key can be in ~1000 different files. How do you see it?

Copy link
Member

@kenjis kenjis Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a bad key is used in 1000 files, then those 1000 locations should be shown.

No. Bad Key File
1 BadKey1 path/to/file1.php
2 BadKey1 path/to/file2.php
... ... ...
1000 BadKey1 path/to/file1000.php

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, I'll try to do it.

}
}

/**
* @param SplFileInfo|string $file
*
* @return array<int, array>
*/
private function findTranslationsInFile($file): array
{
$foundLanguageKeys = [];
$badLanguageKeys = [];

if (is_string($file) && is_file($file)) {
$file = new SplFileInfo($file);
Expand All @@ -172,14 +186,16 @@ private function findTranslationsInFile($file): array
preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches);

if ($matches[1] === []) {
return [];
return [[], []];
}

foreach ($matches[1] as $phraseKey) {
$phraseKeys = explode('.', $phraseKey);

// Language key not have Filename or Lang key
if (count($phraseKeys) < 2) {
$badLanguageKeys[] = $phraseKey;

continue;
}

Expand All @@ -189,6 +205,8 @@ private function findTranslationsInFile($file): array
|| ($languageFileName === '' && $phraseKeys[0] === '');

if ($isEmptyNestedArray) {
$badLanguageKeys[] = $phraseKey;

continue;
}

Expand All @@ -201,7 +219,7 @@ private function findTranslationsInFile($file): array
}
}

return $foundLanguageKeys;
return [$foundLanguageKeys, $badLanguageKeys];
}

private function isIgnoredFile(SplFileInfo $file): bool
Expand Down Expand Up @@ -335,11 +353,11 @@ private function isSubDirectory(string $directory, string $rootDirectory): bool
* @param SplFileInfo[] $files
*
* @return array<int, array|int>
* @phpstan-return list{0: array<string, array<string, string>>, 1: int}
* @phpstan-return list{0: array<string, array<string, string>>, 1: array<string, array<string, string>>, 2: int}
*/
private function findLanguageKeysInFiles(array $files): array
{
$foundLanguageKeys = [];
$foundLanguageKeys = [[], []];
$countFiles = 0;

foreach ($files as $file) {
Expand All @@ -349,9 +367,13 @@ private function findLanguageKeysInFiles(array $files): array

$this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH)));
$countFiles++;
$foundLanguageKeys = array_replace_recursive($this->findTranslationsInFile($file), $foundLanguageKeys);

$findInFile = $this->findTranslationsInFile($file);

$foundLanguageKeys[0] = array_replace_recursive($findInFile[0], $foundLanguageKeys[0]);
$foundLanguageKeys[1] = array_merge($findInFile[1], $foundLanguageKeys[1]);
Comment on lines +373 to +374
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable names are difficult to understand. Don't use [0] or [1].
Name meaningful variable names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. this is because the function returns multiple values instead of one

}

return [$foundLanguageKeys, $countFiles];
return [$foundLanguageKeys[0], array_unique($foundLanguageKeys[1]), $countFiles];
}
}
24 changes: 24 additions & 0 deletions tests/system/Commands/Translation/LocalizationFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public function testShowNewTranslation(): void
$this->assertStringContainsString($this->getActualTableWithNewKeys(), $this->getStreamFilterBuffer());
}

public function testShowBadTranslation(): void
{
$this->makeLocaleDirectory();

command('lang:find --dir Translation --verbose');

$this->assertStringContainsString($this->getActualTableWithBadKeys(), $this->getStreamFilterBuffer());
}

private function getActualTranslationOneKeys(): array
{
return [
Expand Down Expand Up @@ -193,6 +202,21 @@ private function getActualTableWithNewKeys(): string
TEXT_WRAP;
}

private function getActualTableWithBadKeys(): string
{
return <<<'TEXT_WRAP'
+---+------------------------+
| № | Bad key |
+---+------------------------+
| 0 | TranslationTwo |
| 1 | .invalid_key |
| 2 | TranslationTwo. |
| 3 | TranslationTwo... |
| 4 | ..invalid_nested_key.. |
+---+------------------------+
TEXT_WRAP;
}

private function assertTranslationsExistAndHaveTranslatedKeys(): void
{
$this->assertFileExists(self::$languageTestPath . self::$locale . '/TranslationOne.php');
Expand Down