Skip to content

Commit de57fec

Browse files
ottoszikamartinlindhe
authored andcommitted
Named translation fix for JSON files (martinlindhe#23)
1 parent 59a43f2 commit de57fec

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/Generator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ private function allocateLocaleJSON($path)
126126
if (pathinfo($path, PATHINFO_EXTENSION) !== 'json') {
127127
return null;
128128
}
129-
$tmp = (array)json_decode(file_get_contents($path));
129+
$tmp = (array)json_decode(file_get_contents($path), true);
130130
if (gettype($tmp) !== "array") {
131131
throw new Exception('Unexpected data while processing ' . $path);
132132
}
133133

134-
return $tmp;
134+
return $this->adjustArray($tmp);
135135
}
136136

137137
/**

tests/GenerateTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ private function generateLocaleFilesFrom(array $arr)
2727
return $root;
2828
}
2929

30+
private function generateLocaleFilesFromJSON(array $arr)
31+
{
32+
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand());
33+
34+
if (!is_dir($root)) {
35+
mkdir($root, 0777, true);
36+
}
37+
38+
foreach ($arr as $key => $val) {
39+
40+
if (!is_dir($root . '/' . $key)) {
41+
mkdir($root . '/' . $key);
42+
}
43+
44+
$outFile = $root . '/'. $key . '.json';
45+
file_put_contents($outFile, json_encode($val));
46+
}
47+
48+
return $root;
49+
}
50+
3051
private function destroyLocaleFilesFrom(array $arr, $root)
3152
{
3253
foreach ($arr as $key => $val) {
@@ -49,6 +70,27 @@ private function destroyLocaleFilesFrom(array $arr, $root)
4970
}
5071
}
5172

73+
private function destroyLocaleFilesFromJSON(array $arr, $root)
74+
{
75+
foreach ($arr as $key => $val) {
76+
77+
foreach ($val as $group => $content) {
78+
$outFile = $root . '/'. $key . '.json';
79+
if (file_exists($outFile)) {
80+
unlink($outFile);
81+
}
82+
}
83+
84+
if (is_dir($root . '/' . $key)) {
85+
rmdir($root . '/' . $key);
86+
}
87+
}
88+
89+
if (is_dir($root)) {
90+
rmdir($root);
91+
}
92+
}
93+
5294
function testBasic()
5395
{
5496
$arr = [
@@ -117,6 +159,23 @@ function testNamed()
117159
(new Generator)->generateFromPath($root));
118160

119161
$this->destroyLocaleFilesFrom($arr, $root);
162+
163+
$root = $this->generateLocaleFilesFromJSON($arr);
164+
165+
$this->assertEquals(
166+
'export default {' . PHP_EOL
167+
. ' "en": {' . PHP_EOL
168+
. ' "help": {' . PHP_EOL
169+
. ' "yes": "see {link} y {lonk}",' . PHP_EOL
170+
. ' "no": {' . PHP_EOL
171+
. ' "one": "see {link}"' . PHP_EOL
172+
. ' }' . PHP_EOL
173+
. ' }' . PHP_EOL
174+
. ' }' . PHP_EOL
175+
. '}' . PHP_EOL,
176+
(new Generator)->generateFromPath($root));
177+
178+
$this->destroyLocaleFilesFromJSON($arr, $root);
120179
}
121180

122181
function testShouldNotTouchHtmlTags()
@@ -144,5 +203,20 @@ function testShouldNotTouchHtmlTags()
144203
(new Generator)->generateFromPath($root));
145204

146205
$this->destroyLocaleFilesFrom($arr, $root);
206+
207+
$root = $this->generateLocaleFilesFromJSON($arr);
208+
209+
$this->assertEquals(
210+
'export default {' . PHP_EOL
211+
. ' "en": {' . PHP_EOL
212+
. ' "help": {' . PHP_EOL
213+
. ' "yes": "see <a href=\"mailto:mail@com\">",' . PHP_EOL
214+
. ' "no": "see <a href=\"{link}\">"' . PHP_EOL
215+
. ' }' . PHP_EOL
216+
. ' }' . PHP_EOL
217+
. '}' . PHP_EOL,
218+
(new Generator)->generateFromPath($root));
219+
220+
$this->destroyLocaleFilesFromJSON($arr, $root);
147221
}
148222
}

0 commit comments

Comments
 (0)