Created
May 7, 2017 19:06
-
-
Save fgreinus/c7718d9a9f2125643fdd256c44f4715a to your computer and use it in GitHub Desktop.
Laravel: convert php-language files to json style
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
$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