Last active
June 13, 2023 15:00
-
-
Save pboivin/3bc6bf943301ef4640f984c9aba0d0ce to your computer and use it in GitHub Desktop.
Laravel command to sort JSON translations alphabetically
This file contains 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 | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class SortTranslations extends Command | |
{ | |
protected $signature = 'app:sort-translations'; | |
protected $description = 'Sort JSON translations in `lang/`'; | |
public function handle(): int | |
{ | |
$files = glob(base_path('lang/*.json')); | |
if (empty($files)) { | |
$this->warn("\nFiles not found!\n"); | |
return Command::FAILURE; | |
} | |
try { | |
foreach ($files as $file) { | |
$content = json_decode(file_get_contents($file), true); | |
ksort($content, SORT_NATURAL | SORT_FLAG_CASE); | |
file_put_contents($file, json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); | |
} | |
} catch (\Throwable $e) { | |
$this->warn("\nSomething went wrong: {$e->getMessage()}\n"); | |
return Command::FAILURE; | |
} | |
$this->info("\nTranslations updated successfully\n"); | |
return Command::SUCCESS; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment