Skip to content

Instantly share code, notes, and snippets.

@AnandPilania
Last active March 17, 2023 07:55
Show Gist options
  • Select an option

  • Save AnandPilania/79c4588d675ae7bc9bf46cc06b94e709 to your computer and use it in GitHub Desktop.

Select an option

Save AnandPilania/79c4588d675ae7bc9bf46cc06b94e709 to your computer and use it in GitHub Desktop.
Laravel: find missing translations with Artisan Closure Command (cli).
<?php
// ADD THIS TO routes/console.php
Artisan::command('lang {key} {lang?}', function ($key, $lang = null) {
config([
'filesystems.disks.lang' => [
'driver' => 'local',
'root' => lang_path(),
],
]);
$disk = \Illuminate\Support\Facades\Storage::disk('lang');
$dirs = collect($disk->directories(null, true))->filter(function ($dir) {
return !\Illuminate\Support\Str::contains($dir, '-');
})->when($lang, function ($c) use ($lang) {
return $c->filter(function ($dir) use ($lang) {
return \Illuminate\Support\Str::endsWith($dir, $lang);
});
});
$explodedKey = explode('.', $key);
$file = null;
$key = $explodedKey[0];
if (count($explodedKey) > 1) {
list($file, $key) = $explodedKey;
}
$response = collect([]);
foreach ($dirs as $dir) {
foreach ($disk->allFiles($dir) as $_file) {
if ($file && !\Illuminate\Support\Str::contains($_file, $file)) {
continue;
}
$content = (array)include lang_path($_file);
$response->push([
'file' => $_file,
'status' => array_key_exists($key, $content) ? "<fg=green>{$content[$key]}</>" : '<bg=red;fg=yellow>MISSING</>',
]);
}
}
$this->table(['file', 'status'], $response->sortBy('status')->flatten()->chunk(2)->toArray());
return 1;
});
@AnandPilania
Copy link
Copy Markdown
Author

HOW TO:

php artisan FILE.KEY LANG

KEY is mandatory;
FILE & LANG is optional;

finding key without specifying file & lang: php artisan lang failed

finding key inside specific file & all lang: php artisan lang auth.failed

finding key inside specific lang only: php artisan lang failed en

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment