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

Commit

Permalink
Revert "Fix multi-file output (#96)"
Browse files Browse the repository at this point in the history
This reverts commit f546c3f.
  • Loading branch information
martinlindhe committed Feb 9, 2020
1 parent 95fcfa0 commit 7978916
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/Commands/GenerateInclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handle()

if ($multipleFiles || $multipleLocales) {
$files = (new Generator($config))
->generateMultiple($root, $format);
->generateMultiple($root, $format, $multipleLocales);

if ($config['showOutputMessages']) {
$this->info("Written to : " . $files);
Expand Down
59 changes: 37 additions & 22 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
}

if (isset($locales[$noExt])) {
$locales[$noExt] = array_merge_recursive($local, $locales[$noExt]);
$locales[$noExt] = array_merge($local, $locales[$noExt]);
} else {
$locales[$noExt] = $local;
}
Expand All @@ -93,7 +93,22 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l

$locales = $this->adjustVendor($locales);

return $this->encodeJson($locales, $format);
$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;
}

/**
Expand All @@ -102,7 +117,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
* @return string
* @throws Exception
*/
public function generateMultiple($path, $format = 'es6')
public function generateMultiple($path, $format = 'es6', $multiLocales = false)
{
if (!is_dir($path)) {
throw new Exception('Directory not found: ' . $path);
Expand All @@ -127,7 +142,7 @@ public function generateMultiple($path, $format = 'es6')
$this->availableLocales[] = $noExt;
}
if ($fileinfo->isDir()) {
$local = $this->allocateLocaleArray($fileinfo->getRealPath());
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
} else {
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
if ($local === null) continue;
Expand All @@ -141,10 +156,20 @@ public function generateMultiple($path, $format = 'es6')
}
}
}
foreach ($locales as $fileName => $data) {
foreach ($this->filesToCreate as $fileName => $data) {
$fileToCreate = $jsPath . $fileName . '.js';
$createdFiles .= $fileToCreate . PHP_EOL;
$jsBody = $this->encodeJson([$fileName => $data], $format);
$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;
}

if (!is_dir(dirname($fileToCreate))) {
mkdir(dirname($fileToCreate), 0777, true);
Expand Down Expand Up @@ -178,7 +203,7 @@ private function allocateLocaleJSON($path)
* @param string $path
* @return array
*/
private function allocateLocaleArray($path)
private function allocateLocaleArray($path, $multiLocales = false)
{
$data = [];
$dir = new DirectoryIterator($path);
Expand Down Expand Up @@ -218,6 +243,11 @@ private function allocateLocaleArray($path)
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);
Expand Down Expand Up @@ -374,19 +404,4 @@ 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;
}
}
118 changes: 2 additions & 116 deletions tests/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@

class GenerateTest extends \PHPUnit_Framework_TestCase
{
/**
* @return string
*/
private function getRootDir()
private function generateLocaleFilesFrom(array $arr)
{
$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) {

Expand All @@ -36,18 +27,6 @@ private function generateLocaleFilesFrom(array $arr, $root = null)
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) {
Expand All @@ -59,11 +38,6 @@ private function destroyLocaleFilesFrom(array $arr, $root)
}
}

$jsonFile = $root . '/'. $key . '.json';
if (file_exists($jsonFile)) {
unlink($jsonFile);
}

if (is_dir($root . '/' . $key)) {
rmdir($root . '/' . $key);
}
Expand Down Expand Up @@ -112,43 +86,6 @@ 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';
Expand Down Expand Up @@ -607,55 +544,4 @@ 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);
}
}

3 comments on commit 7978916

@Jellyfrog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this reverted? Worked fine for us at least

@martinlindhe
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jellyfrog This was reverted as it changed expected behavior. For discussion see #99

@lk77
Copy link

@lk77 lk77 commented on 7978916 Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
so the --multi-locale is not working anymore now ?

Please sign in to comment.