Skip to content

Instantly share code, notes, and snippets.

@fgreinus
Created May 7, 2017 19:06
Show Gist options
  • Save fgreinus/c7718d9a9f2125643fdd256c44f4715a to your computer and use it in GitHub Desktop.
Save fgreinus/c7718d9a9f2125643fdd256c44f4715a to your computer and use it in GitHub Desktop.
Laravel: convert php-language files to json style
$files = collect(File::files(resource_path('lang/*')));
function dive(array $curr) {
$result = [];
$values = array_values($curr);
foreach ($values as $value) {
if (is_array($value)) {
$result = array_merge($result, dive($value));
continue;
}
$result[$value] = $value;
}
return $result;
}
$trans = $files->reduce(function($trans, $file) {
$folder = basename(dirname($file));
$translations = require($file);
if (!array_key_exists($folder, $trans)) {
$trans[$folder] = [];
}
$trans[$folder] = array_merge($trans[$folder], dive($translations));
return $trans;
}, []);
collect($trans)->each(function($item, $lang) {
$fileContents = json_encode($item);
File::put(resource_path('lang/' . $lang . '.json'), $fileContents);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment