@@ -27,6 +27,27 @@ private function generateLocaleFilesFrom(array $arr)
27
27
return $ root ;
28
28
}
29
29
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
+
30
51
private function destroyLocaleFilesFrom (array $ arr , $ root )
31
52
{
32
53
foreach ($ arr as $ key => $ val ) {
@@ -49,6 +70,27 @@ private function destroyLocaleFilesFrom(array $arr, $root)
49
70
}
50
71
}
51
72
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
+
52
94
function testBasic ()
53
95
{
54
96
$ arr = [
@@ -117,6 +159,23 @@ function testNamed()
117
159
(new Generator )->generateFromPath ($ root ));
118
160
119
161
$ 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 );
120
179
}
121
180
122
181
function testShouldNotTouchHtmlTags ()
@@ -144,5 +203,20 @@ function testShouldNotTouchHtmlTags()
144
203
(new Generator )->generateFromPath ($ root ));
145
204
146
205
$ 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 );
147
221
}
148
222
}
0 commit comments