Skip to content

Instantly share code, notes, and snippets.

@pboivin
Last active June 13, 2023 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pboivin/3bc6bf943301ef4640f984c9aba0d0ce to your computer and use it in GitHub Desktop.
Save pboivin/3bc6bf943301ef4640f984c9aba0d0ce to your computer and use it in GitHub Desktop.
Laravel command to sort JSON translations alphabetically
<?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