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

Commit

Permalink
Named translation fix for JSON files (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoszika authored and martinlindhe committed Jul 19, 2017
1 parent 59a43f2 commit de57fec
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ private function allocateLocaleJSON($path)
if (pathinfo($path, PATHINFO_EXTENSION) !== 'json') {
return null;
}
$tmp = (array)json_decode(file_get_contents($path));
$tmp = (array)json_decode(file_get_contents($path), true);
if (gettype($tmp) !== "array") {
throw new Exception('Unexpected data while processing ' . $path);
}

return $tmp;
return $this->adjustArray($tmp);
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ private function generateLocaleFilesFrom(array $arr)
return $root;
}

private function generateLocaleFilesFromJSON(array $arr)
{
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand());

if (!is_dir($root)) {
mkdir($root, 0777, true);
}

foreach ($arr as $key => $val) {

if (!is_dir($root . '/' . $key)) {
mkdir($root . '/' . $key);
}

$outFile = $root . '/'. $key . '.json';
file_put_contents($outFile, json_encode($val));
}

return $root;
}

private function destroyLocaleFilesFrom(array $arr, $root)
{
foreach ($arr as $key => $val) {
Expand All @@ -49,6 +70,27 @@ private function destroyLocaleFilesFrom(array $arr, $root)
}
}

private function destroyLocaleFilesFromJSON(array $arr, $root)
{
foreach ($arr as $key => $val) {

foreach ($val as $group => $content) {
$outFile = $root . '/'. $key . '.json';
if (file_exists($outFile)) {
unlink($outFile);
}
}

if (is_dir($root . '/' . $key)) {
rmdir($root . '/' . $key);
}
}

if (is_dir($root)) {
rmdir($root);
}
}

function testBasic()
{
$arr = [
Expand Down Expand Up @@ -117,6 +159,23 @@ function testNamed()
(new Generator)->generateFromPath($root));

$this->destroyLocaleFilesFrom($arr, $root);

$root = $this->generateLocaleFilesFromJSON($arr);

$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "yes": "see {link} y {lonk}",' . PHP_EOL
. ' "no": {' . PHP_EOL
. ' "one": "see {link}"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator)->generateFromPath($root));

$this->destroyLocaleFilesFromJSON($arr, $root);
}

function testShouldNotTouchHtmlTags()
Expand Down Expand Up @@ -144,5 +203,20 @@ function testShouldNotTouchHtmlTags()
(new Generator)->generateFromPath($root));

$this->destroyLocaleFilesFrom($arr, $root);

$root = $this->generateLocaleFilesFromJSON($arr);

$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "yes": "see <a href=\"mailto:mail@com\">",' . PHP_EOL
. ' "no": "see <a href=\"{link}\">"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator)->generateFromPath($root));

$this->destroyLocaleFilesFromJSON($arr, $root);
}
}

0 comments on commit de57fec

Please sign in to comment.