Skip to content

Commit

Permalink
Merge pull request #5 from gpibarra/only_and_exclude
Browse files Browse the repository at this point in the history
Only and exclude
  • Loading branch information
alies-dev committed Apr 25, 2024
2 parents ff80181 + 69d4587 commit 40ceab7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,16 @@ You can specify a base locale:
php artisan translations:missing --base=es
```

You can specify a list of locales to check:
```sh
php artisan translations:missing --base=es --only=be,en
```

You can specify a list of locales to exclude:
```sh
php artisan translations:missing --base=es --exclude=fr,de
```

You can specify a relative or absolute path to `lang` directory location:
```sh
php artisan translations:missing --dir=/resources/my-custom-lang-dirname
Expand Down
27 changes: 26 additions & 1 deletion src/Commands/FindMissingTranslations.php
Expand Up @@ -21,7 +21,9 @@ class FindMissingTranslations extends Command
*/
protected $signature = 'translations:missing
{--dir= : Relative path of lang directory, e.g. "/resources/lang", a directory that contains all supported locales}
{--base= : Base locale, e.g. "en". All other locales are compared to this locale}';
{--base= : Base locale, e.g. "en". All other locales are compared to this locale}
{--only= : Only compare specified locales, e.g. "be,de,es,fr". All other locales are ignored}
{--exclude= : Exclude specified locales, e.g. "be,de,es,fr". All other locales are compared}';

/**
* The console command description.
Expand All @@ -47,6 +49,11 @@ public function handle(): int
assert(is_string($baseLocale), 'Invalid base locale');
$baseLocaleDirectoryPath = $pathToLocates.\DIRECTORY_SEPARATOR.$baseLocale;

$onlyLocales = $this->option('only');
$onlyLocalesArray = $onlyLocales ? explode(',', $onlyLocales) : [];

Check failure on line 53 in src/Commands/FindMissingTranslations.php

View workflow job for this annotation

GitHub Actions / psalm

RiskyTruthyFalsyComparison

src/Commands/FindMissingTranslations.php:53:29: RiskyTruthyFalsyComparison: Operand of type array<array-key, mixed>|bool|null|string contains types array<array-key, mixed>|string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 53 in src/Commands/FindMissingTranslations.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidScalarArgument

src/Commands/FindMissingTranslations.php:53:57: InvalidScalarArgument: Argument 2 of explode expects string, but non-empty-array<array-key, mixed>|non-falsy-string|true provided (see https://psalm.dev/012)
$excludeLocales = $this->option('exclude');
$excludeLocalesArray = $excludeLocales ? explode(',', $excludeLocales) : [];

Check failure on line 55 in src/Commands/FindMissingTranslations.php

View workflow job for this annotation

GitHub Actions / psalm

RiskyTruthyFalsyComparison

src/Commands/FindMissingTranslations.php:55:32: RiskyTruthyFalsyComparison: Operand of type array<array-key, mixed>|bool|null|string contains types array<array-key, mixed>|string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 55 in src/Commands/FindMissingTranslations.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidScalarArgument

src/Commands/FindMissingTranslations.php:55:63: InvalidScalarArgument: Argument 2 of explode expects string, but non-empty-array<array-key, mixed>|non-falsy-string|true provided (see https://psalm.dev/012)

$localeDirectories = File::directories($pathToLocates);
$baseLocaleFiles = $this->getFilenames($baseLocaleDirectoryPath);

Expand All @@ -59,12 +66,30 @@ public function handle(): int
if ($isDirectoryForBaseLocale) {
continue;
}
if (count($onlyLocalesArray) > 0 && !in_array($currentLocale, $onlyLocalesArray, true)) {
continue;
}
if (in_array($currentLocale, $excludeLocalesArray, true)) {
continue;
}

$this->info("Comparing {$baseLocale} to {$currentLocale}.", 'v');

$this->compareLanguages($baseLocaleDirectoryPath, $baseLocaleFiles, $currentLocaleDirectoryPath, $languageFiles, $currentLocale);
}

if (count($onlyLocalesArray) > 0) {
$locales = array_map(function ($currentLocaleDirectoryPath) {
preg_match('/(\w{2})$/', $currentLocaleDirectoryPath, $matchedParts);

Check failure on line 83 in src/Commands/FindMissingTranslations.php

View workflow job for this annotation

GitHub Actions / psalm

MixedArgument

src/Commands/FindMissingTranslations.php:83:42: MixedArgument: Argument 2 of preg_match cannot be mixed, expecting string (see https://psalm.dev/030)
return $matchedParts[0];
}, $localeDirectories);
$localesMissing = array_values(array_diff($onlyLocalesArray, $locales));
if (count($localesMissing) > 0) {
$this->error('The following locales are missing:', 'q');
$this->table(['locale'], array_map(fn($locale) => [$locale], $localesMissing));
}
}

$this->info('Successfully compared all languages.');

return $this->exitCode;
Expand Down
36 changes: 34 additions & 2 deletions tests/Commands/FindMissingTranslationsTest.php
Expand Up @@ -15,7 +15,7 @@ public function it_does_not_report_about_synchronized_files(): void
{
$this->withoutMockingConsoleOutput();

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

Expand All @@ -28,11 +28,43 @@ public function it_reports_about_missing_translation_keys(): void
{
$this->withoutMockingConsoleOutput();

$dir = __DIR__.'/unsync_lang_files';
$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 | 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_only_lang(): void
{
$this->withoutMockingConsoleOutput();

$dir = __DIR__ . '/unsync_lang_files';
$exitCode = $this->artisan("translations:missing --dir=$dir --base=en --only=be,es");
$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);
}

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

$dir = __DIR__ . '/unsync_lang_files';
$exitCode = $this->artisan("translations:missing --dir=$dir --base=en --exclude=be,fr");
$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);
}
}
9 changes: 9 additions & 0 deletions tests/Commands/sync_lang_files/es/a.php
@@ -0,0 +1,9 @@
<?php

return [
'Yes' => 'Si',

'group' => [
'Help' => 'Ayuda',
],
];
9 changes: 9 additions & 0 deletions tests/Commands/sync_lang_files/fr/a.php
@@ -0,0 +1,9 @@
<?php

return [
'Yes' => 'Oui',

'group' => [
'Help' => 'Aide',
],
];
9 changes: 9 additions & 0 deletions tests/Commands/unsync_lang_files/es/a.php
@@ -0,0 +1,9 @@
<?php

return [
'Yes' => 'Si',
'No' => 'No',
// 'OK' => 'OK', missing key

// 'group' => [], // missing group
];
10 changes: 10 additions & 0 deletions tests/Commands/unsync_lang_files/fr/a.php
@@ -0,0 +1,10 @@
<?php

return [
#en frances
'Yes' => 'Oui',
'No' => 'Non',
// 'OK' => 'OK', missing key

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

0 comments on commit 40ceab7

Please sign in to comment.