Skip to content

Instantly share code, notes, and snippets.

@Rene-Roscher
Created June 12, 2023 00:34
Show Gist options
  • Select an option

  • Save Rene-Roscher/ac04627ae6357a05848ebb36ae3e98ca to your computer and use it in GitHub Desktop.

Select an option

Save Rene-Roscher/ac04627ae6357a05848ebb36ae3e98ca to your computer and use it in GitHub Desktop.
Laravel / Lang Fetch (For a structured lang folder / json)
<?php
$dir = 'resources/lang';
$exportFileName = 'export-en';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$files = array();
foreach ($iterator as $file) {
if ($file->isFile() && in_array($file->getExtension(), ['json', 'php'])) {
if ($file->getFilename() !== $exportFileName . '.json') {
$files[] = $file->getPathname();
}
}
}
$localeData = [];
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'json') {
$json = file_get_contents($file);
$array = json_decode($json, true);
foreach ($array as $key => $value) {
if (is_string($value)) {
$localeData[$key] = $value;
}
}
} elseif (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$array = include $file;
foreach ($array as $key => $value) {
if (is_string($value)) {
$localeData[$key] = $value;
}
}
}
}
$localeFile = sprintf('%s/%s.json', $dir, $exportFileName);
file_put_contents($localeFile, json_encode($localeData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo "Mapping completed. Locale file '{$localeFile}' created.";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment