Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Add support for JSON translation files (#16)
Browse files Browse the repository at this point in the history
* Add support for JSON translation files

* fixed array merge

* refactored code
  • Loading branch information
RSchwan authored and martinlindhe committed Jun 12, 2017
1 parent ae202b8 commit 7cf4b8a
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,50 @@ public function generateFromPath($path)

$locales = [];
$dir = new DirectoryIterator($path);

foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()
&& $fileinfo->isDir()
&& !in_array($fileinfo->getFilename(), ['vendor'])
) {
$locales[$fileinfo->getFilename()] =
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
$noExt = $this->removeExtension($fileinfo->getFilename());

if ($fileinfo->isDir()) {
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
} else {
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
if ($local === null) continue;
}

if (isset($locales[$noExt])) {
$locales[$noExt] = array_merge($local, $locales[$noExt]);
} else {
$locales[$noExt] = $local;
}
}
}

return 'export default '
. json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
}

/**
* @param string $path
* @return array
*/
private function allocateLocaleJSON($path)
{
// Ignore non *.json files (ex.: .gitignore, vim swap files etc.)
if (pathinfo($path, PATHINFO_EXTENSION) !== 'json') {
return null;
}
$tmp = (array) json_decode(file_get_contents($path));
if (gettype($tmp) !== "array") {
throw new Exception('Unexpected data while processing '.$path);
}

return $tmp;
}

/**
* @param string $path
* @return array
Expand Down

0 comments on commit 7cf4b8a

Please sign in to comment.