Last active
March 17, 2023 07:55
-
-
Save AnandPilania/79c4588d675ae7bc9bf46cc06b94e709 to your computer and use it in GitHub Desktop.
Laravel: find missing translations with Artisan Closure Command (cli).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HOW TO:
finding key without specifying file & lang:
php artisan lang failedfinding key inside specific file & all lang:
php artisan lang auth.failedfinding key inside specific lang only:
php artisan lang failed en