Skip to content

Instantly share code, notes, and snippets.

@loranger
Last active March 8, 2018 13:41
Show Gist options
  • Save loranger/61a1f6b750dc25a9d542b5e44ff36f0f to your computer and use it in GitHub Desktop.
Save loranger/61a1f6b750dc25a9d542b5e44ff36f0f to your computer and use it in GitHub Desktop.
Migrate laravel language files to json

Download file

Make it executable and launch it :

chmod +x migrate_lang
./migrate_lang /path/to/laravel/project

Or you can use PHP to launch it :

php ./migrate_lang /path/to/laravel/project

On macOS, in order to replace all trans( to __(, you should force a gnu-sed install :

brew install gnu-sed --with-default-names
#!/usr/bin/env php
<?php
if ($argc <=1) {
echo "Please specify a project path:\n $argv[0] /path/to/laravel/project";
exit(1);
} elseif (!file_exists($argv[1])) {
echo "Please specify a valid project path:\n $argv[1] does not exists";
exit(1);
} elseif (!file_exists(sprintf('%s/composer.json', $argv[1])) || !property_exists((json_decode(file_get_contents(sprintf('%s/composer.json', $argv[1]))))->require, 'laravel/framework')) {
echo "Please specify a valid Laravel project path:\n Laravel not found inside $argv[1]";
exit(1);
}
require sprintf('%s/bootstrap/autoload.php', $argv[1]);
$app = require_once sprintf('%s/bootstrap/app.php', $argv[1]);
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$kernel->handle(
$request = Illuminate\Http\Request::capture()
);
echo "Searching for localization array to convert to JSON\n";
foreach (glob(resource_path('lang/*'), GLOB_ONLYDIR) as $directory) {
$lang = basename($directory);
$obj = (object) [];
foreach (glob(resource_path(sprintf('lang/%s/*.php', $lang))) as $file) {
$obj->{basename($file, '.php')} = include($file);
}
file_put_contents(resource_path(sprintf('lang/%s.json', $lang)), json_encode(array_dot($obj)));
printf(" lang/%s.json built\n", $lang);
}
echo "Searching for “trans()” calls\n";
$command = sprintf("grep --exclude-dir={public,vendor,storage,node_modules} --exclude=%s -rwl '%s' -e 'trans('", basename($argv[0]), $argv[1]);
$process = new \Symfony\Component\Process\Process($command);
$process->setTimeout(3600);
$process->run();
$file_list = array_filter(explode("\n", $process->getOutput()));
$nb_files = count($file_list);
if ($nb_files > 0) {
foreach ($file_list as $file) {
printf(" Migrating %s\n", $file);
$command = sprintf("sed -i 's/trans(/__(/g' %s", $file);
$process = new \Symfony\Component\Process\Process($command);
$process->run();
}
printf("Migrated %d files\n", $nb_files);
} else {
echo " None found\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment