diff --git a/src/Commands/GenerateInclude.php b/src/Commands/GenerateInclude.php index 30fb23d..70435db 100644 --- a/src/Commands/GenerateInclude.php +++ b/src/Commands/GenerateInclude.php @@ -50,7 +50,7 @@ public function handle() if ($multipleFiles || $multipleLocales) { $files = (new Generator($config)) - ->generateMultiple($root, $format, $multipleLocales); + ->generateMultiple($root, $format); if ($config['showOutputMessages']) { $this->info("Written to : " . $files); diff --git a/src/Generator.php b/src/Generator.php index b529633..f5d854e 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -84,7 +84,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l } if (isset($locales[$noExt])) { - $locales[$noExt] = array_merge($local, $locales[$noExt]); + $locales[$noExt] = array_merge_recursive($local, $locales[$noExt]); } else { $locales[$noExt] = $local; } @@ -93,22 +93,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l $locales = $this->adjustVendor($locales); - $jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; - - if (json_last_error() !== JSON_ERROR_NONE) { - throw new Exception('Could not generate JSON, error code '.json_last_error()); - } - - // formats other than 'es6' and 'umd' will become plain JSON - if ($format === 'es6') { - $jsBody = $this->getES6Module($jsonLocales); - } elseif ($format === 'umd') { - $jsBody = $this->getUMDModule($jsonLocales); - } else { - $jsBody = $jsonLocales; - } - - return $jsBody; + return $this->encodeJson($locales, $format); } /** @@ -117,7 +102,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l * @return string * @throws Exception */ - public function generateMultiple($path, $format = 'es6', $multiLocales = false) + public function generateMultiple($path, $format = 'es6') { if (!is_dir($path)) { throw new Exception('Directory not found: ' . $path); @@ -142,7 +127,7 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false) $this->availableLocales[] = $noExt; } if ($fileinfo->isDir()) { - $local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales); + $local = $this->allocateLocaleArray($fileinfo->getRealPath()); } else { $local = $this->allocateLocaleJSON($fileinfo->getRealPath()); if ($local === null) continue; @@ -156,20 +141,10 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false) } } } - foreach ($this->filesToCreate as $fileName => $data) { + foreach ($locales as $fileName => $data) { $fileToCreate = $jsPath . $fileName . '.js'; $createdFiles .= $fileToCreate . PHP_EOL; - $jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; - if (json_last_error() !== JSON_ERROR_NONE) { - throw new Exception('Could not generate JSON, error code '.json_last_error()); - } - if ($format === 'es6') { - $jsBody = $this->getES6Module($jsonLocales); - } elseif ($format === 'umd') { - $jsBody = $this->getUMDModule($jsonLocales); - } else { - $jsBody = $jsonLocales; - } + $jsBody = $this->encodeJson([$fileName => $data], $format); if (!is_dir(dirname($fileToCreate))) { mkdir(dirname($fileToCreate), 0777, true); @@ -203,7 +178,7 @@ private function allocateLocaleJSON($path) * @param string $path * @return array */ - private function allocateLocaleArray($path, $multiLocales = false) + private function allocateLocaleArray($path) { $data = []; $dir = new DirectoryIterator($path); @@ -243,11 +218,6 @@ private function allocateLocaleArray($path, $multiLocales = false) if($filePath[0] === DIRECTORY_SEPARATOR) { $filePath = substr($filePath, 1); } - if ($multiLocales) { - $this->filesToCreate[$lastLocale][$lastLocale][$filePath] = $this->adjustArray($tmp); - } else { - $this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp); - } } $data[$noExt] = $this->adjustArray($tmp); @@ -404,4 +374,19 @@ private function getES6Module($body) { return "export default {$body}"; } + + private function encodeJson($data, $format = 'es6') + { + $jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('Could not generate JSON, error code '.json_last_error()); + } + if ($format === 'es6') { + return $this->getES6Module($jsonLocales); + } elseif ($format === 'umd') { + return $this->getUMDModule($jsonLocales); + } + + return $jsonLocales; + } } diff --git a/tests/GenerateTest.php b/tests/GenerateTest.php index e8cd620..85eac85 100644 --- a/tests/GenerateTest.php +++ b/tests/GenerateTest.php @@ -4,13 +4,22 @@ class GenerateTest extends \PHPUnit_Framework_TestCase { - private function generateLocaleFilesFrom(array $arr) + /** + * @return string + */ + private function getRootDir() { $root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand()); - + if (!is_dir($root)) { mkdir($root, 0777, true); } + return $root; + } + + private function generateLocaleFilesFrom(array $arr, $root = null) + { + $root = $root ?: $this->getRootDir(); foreach ($arr as $key => $val) { @@ -27,6 +36,18 @@ private function generateLocaleFilesFrom(array $arr) return $root; } + private function generateJsonLocaleFilesFrom(array $arr, $root = null) + { + $root = $root ?: $this->getRootDir(); + + foreach ($arr as $lang => $data) { + $outFile = $root . '/' . $lang . '.json'; + file_put_contents($outFile, json_encode($data)); + } + + return $root; + } + private function destroyLocaleFilesFrom(array $arr, $root) { foreach ($arr as $key => $val) { @@ -38,6 +59,11 @@ private function destroyLocaleFilesFrom(array $arr, $root) } } + $jsonFile = $root . '/'. $key . '.json'; + if (file_exists($jsonFile)) { + unlink($jsonFile); + } + if (is_dir($root . '/' . $key)) { rmdir($root . '/' . $key); } @@ -86,6 +112,43 @@ function testBasic() $this->destroyLocaleFilesFrom($arr, $root); } + function testBasicJsonFiles() + { + $arr = [ + 'en' => [ + 'help' => [ + 'yes' => 'yes', + 'no' => 'no', + ] + ], + 'sv' => [ + 'help' => [ + 'yes' => 'ja', + 'no' => 'nej', + ] + ] + ]; + + $root = $this->generateJsonLocaleFilesFrom($arr); + $this->assertEquals( + 'export default {' . PHP_EOL + . ' "en": {' . PHP_EOL + . ' "help": {' . PHP_EOL + . ' "yes": "yes",' . PHP_EOL + . ' "no": "no"' . PHP_EOL + . ' }' . PHP_EOL + . ' },' . PHP_EOL + . ' "sv": {' . PHP_EOL + . ' "help": {' . PHP_EOL + . ' "yes": "ja",' . PHP_EOL + . ' "no": "nej"' . PHP_EOL + . ' }' . PHP_EOL + . ' }' . PHP_EOL + . '}' . PHP_EOL, + (new Generator([]))->generateFromPath($root)); + $this->destroyLocaleFilesFrom($arr, $root); + } + function testBasicES6Format() { $format = 'es6'; @@ -544,4 +607,55 @@ function testPluralization() $this->destroyLocaleFilesFrom($arr, $root); } + + function testBothJsonAndPhpFiles() + { + $root = $this->getRootDir(); + $jsonArr = [ + 'en' => [ + 'help' => [ + 'no' => 'no', + ] + ], + 'sv' => [ + 'help' => [ + 'no' => 'nej', + ] + ] + ]; + $this->generateJsonLocaleFilesFrom($jsonArr, $root); + + $phpArr = [ + 'en' => [ + 'help' => [ + 'yes' => 'yes', + ] + ], + 'sv' => [ + 'help' => [ + 'yes' => 'ja', + ] + ] + ]; + $this->generateLocaleFilesFrom($phpArr, $root); + + $this->assertEquals( + 'export default {' . PHP_EOL + . ' "en": {' . PHP_EOL + . ' "help": {' . PHP_EOL + . ' "no": "no",' . PHP_EOL + . ' "yes": "yes"' . PHP_EOL + . ' }' . PHP_EOL + . ' },' . PHP_EOL + . ' "sv": {' . PHP_EOL + . ' "help": {' . PHP_EOL + . ' "no": "nej",' . PHP_EOL + . ' "yes": "ja"' . PHP_EOL + . ' }' . PHP_EOL + . ' }' . PHP_EOL + . '}' . PHP_EOL, + (new Generator([]))->generateFromPath($root)); + + $this->destroyLocaleFilesFrom($jsonArr, $root); + } }