Skip to content

Commit

Permalink
Merge pull request #6 from cappuc/prefix-grouped-keys
Browse files Browse the repository at this point in the history
Prefix grouped keys in command output
  • Loading branch information
alies-dev authored Dec 16, 2024
2 parents 7f99a4c + b031b8a commit 22a4bd9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/Commands/FindMissingTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,27 @@ private function compareLanguages(string $baseLanguagePath, array $baseLanguageF
* Compare array keys recursively
* @param array<string, string|array<string, string>> $firstArray
* @param array<string, string|array<string, string>> $secondArray
* @param string|null $prefix
* @return list<string>
*/
private function arrayDiffRecursive(array $firstArray, array $secondArray): array
private function arrayDiffRecursive(array $firstArray, array $secondArray, ?string $prefix = null): array
{
$outputDiff = [];

foreach ($firstArray as $key => $value) {
$fullKey = $prefix !== null ? "{$prefix}.{$key}" : $key;

if (array_key_exists($key, $secondArray)) {
if (is_array($value)) {
$recursiveDiff = $this->arrayDiffRecursive($value, $secondArray[$key]);
$recursiveDiff = $this->arrayDiffRecursive($value, $secondArray[$key], $fullKey);
if (count($recursiveDiff)) {
foreach ($recursiveDiff as $diff) {
$outputDiff[] = $diff;
}
}
}
} else {
$outputDiff[] = $key;
$outputDiff[] = $fullKey;
}
}

Expand Down
33 changes: 24 additions & 9 deletions tests/Commands/FindMissingTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,24 @@ public function it_reports_about_missing_translation_keys(): void
$output = Artisan::output();

$this->assertSame(1, $exitCode);
$this->assertStringContainsString('| be | a.php | OK |', $output);
$this->assertStringContainsString('| es | a.php | OK |', $output);
$this->assertStringContainsString('| fr | a.php | OK |', $output);
$this->assertStringContainsString('| be | a.php | OK ', $output);
$this->assertStringContainsString('| es | a.php | OK ', $output);
$this->assertStringContainsString('| fr | a.php | OK ', $output);
}

#[Test]
public function it_reports_about_missing_translation_keys_inside_group(): void
{
$this->withoutMockingConsoleOutput();

$dir = __DIR__ . '/unsync_lang_files';
$exitCode = $this->artisan("translations:missing --dir=$dir --base=en");
$output = Artisan::output();

$this->assertSame(1, $exitCode);
$this->assertStringContainsString('| be | a.php | group.Help ', $output);
$this->assertStringContainsString('| es | a.php | group.Help ', $output);
$this->assertStringContainsString('| fr | a.php | group.Help ', $output);
}

#[Test]
Expand All @@ -48,9 +63,9 @@ public function it_reports_about_missing_translation_keys_only_lang(): void
$output = Artisan::output();

$this->assertSame(1, $exitCode);
$this->assertStringContainsString('| be | a.php | OK |', $output);
$this->assertStringContainsString('| es | a.php | OK |', $output);
$this->assertStringNotContainsString('| fr | a.php | OK |', $output);
$this->assertStringContainsString('| be | a.php | OK ', $output);
$this->assertStringContainsString('| es | a.php | OK ', $output);
$this->assertStringNotContainsString('| fr | a.php | OK ', $output);
}

#[Test]
Expand All @@ -63,8 +78,8 @@ public function it_reports_about_missing_translation_keys_exclude_lang(): void
$output = Artisan::output();

$this->assertSame(1, $exitCode);
$this->assertStringNotContainsString('| be | a.php | OK |', $output);
$this->assertStringContainsString('| es | a.php | OK |', $output);
$this->assertStringNotContainsString('| fr | a.php | OK |', $output);
$this->assertStringNotContainsString('| be | a.php | OK ', $output);
$this->assertStringContainsString('| es | a.php | OK ', $output);
$this->assertStringNotContainsString('| fr | a.php | OK ', $output);
}
}
4 changes: 3 additions & 1 deletion tests/Commands/unsync_lang_files/be/a.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
'No' => 'Не',
// 'OK' => 'OK', missing key

// 'group' => [], // missing group
'group' => [
// missing group key
],
];
4 changes: 4 additions & 0 deletions tests/Commands/unsync_lang_files/en/a.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
'Yes' => 'Yes',
'No' => 'No',
'OK' => 'OK',

'group' => [
'Help' => 'Help',
],
];
4 changes: 3 additions & 1 deletion tests/Commands/unsync_lang_files/es/a.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
'No' => 'No',
// 'OK' => 'OK', missing key

// 'group' => [], // missing group
'group' => [
// missing group key
],
];
4 changes: 3 additions & 1 deletion tests/Commands/unsync_lang_files/fr/a.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
'No' => 'Non',
// 'OK' => 'OK', missing key

// 'group' => [], // missing group
'group' => [
// missing group key
],
];

0 comments on commit 22a4bd9

Please sign in to comment.