2
2
3
3
use DirectoryIterator ;
4
4
use Exception ;
5
+ use App ;
5
6
6
7
class Generator
7
8
{
9
+
10
+ private $ availableLocales = [];
11
+ private $ filesToCreate = [];
12
+
8
13
/**
9
14
* @param string $path
10
15
* @param boolean $umd
@@ -14,13 +19,12 @@ class Generator
14
19
public function generateFromPath ($ path , $ umd = null )
15
20
{
16
21
if (!is_dir ($ path )) {
17
- throw new Exception ('Directory not found: ' . $ path );
22
+ throw new Exception ('Directory not found: ' . $ path );
18
23
}
19
24
20
25
$ locales = [];
21
26
$ dir = new DirectoryIterator ($ path );
22
27
$ jsBody = '' ;
23
-
24
28
foreach ($ dir as $ fileinfo ) {
25
29
if (!$ fileinfo ->isDot ()
26
30
&& !in_array ($ fileinfo ->getFilename (), ['vendor ' ])
@@ -39,20 +43,79 @@ public function generateFromPath($path, $umd = null)
39
43
} else {
40
44
$ locales [$ noExt ] = $ local ;
41
45
}
46
+
47
+
42
48
}
43
49
}
44
50
45
51
$ jsonLocales = json_encode ($ locales , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) . PHP_EOL ;
46
52
47
- if (!$ umd ) {
48
- $ jsBody = $ this ->getES6Module ($ jsonLocales );
53
+ if (!$ umd ) {
54
+ $ jsBody = $ this ->getES6Module ($ jsonLocales );
49
55
} else {
50
56
$ jsBody = $ this ->getUMDModule ($ jsonLocales );
51
57
}
52
-
53
58
return $ jsBody ;
54
59
}
55
60
61
+ /**
62
+ * @param string $path
63
+ * @param boolean $umd
64
+ * @return string
65
+ * @throws Exception
66
+ */
67
+ public function generateMultiple ($ path , $ umd = null )
68
+ {
69
+ if (!is_dir ($ path )) {
70
+ throw new Exception ('Directory not found: ' . $ path );
71
+ }
72
+ $ jsPath = base_path () . config ('vue-i18n-generator.jsPath ' );
73
+ $ locales = [];
74
+ $ fileToCreate = '' ;
75
+ $ createdFiles = '' ;
76
+ $ dir = new DirectoryIterator ($ path );
77
+ $ jsBody = '' ;
78
+ foreach ($ dir as $ fileinfo ) {
79
+ if (!$ fileinfo ->isDot ()
80
+ && !in_array ($ fileinfo ->getFilename (), ['vendor ' ])
81
+ ) {
82
+ $ noExt = $ this ->removeExtension ($ fileinfo ->getFilename ());
83
+ if (!in_array ($ noExt , $ this ->availableLocales )) {
84
+ App::setLocale ($ noExt );
85
+ $ this ->availableLocales [] = $ noExt ;
86
+ }
87
+ if ($ fileinfo ->isDir ()) {
88
+ $ local = $ this ->allocateLocaleArray ($ fileinfo ->getRealPath ());
89
+ } else {
90
+ $ local = $ this ->allocateLocaleJSON ($ fileinfo ->getRealPath ());
91
+ if ($ local === null ) continue ;
92
+ }
93
+
94
+ if (isset ($ locales [$ noExt ])) {
95
+ $ locales [$ noExt ] = array_merge ($ local , $ locales [$ noExt ]);
96
+ } else {
97
+ $ locales [$ noExt ] = $ local ;
98
+ }
99
+
100
+
101
+ }
102
+ }
103
+ foreach ($ this ->filesToCreate as $ fileName => $ data ) {
104
+ $ fileToCreate = $ jsPath . $ fileName . '.js ' ;
105
+ $ createdFiles .= $ fileToCreate . PHP_EOL ;
106
+ $ jsonLocales = json_encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) . PHP_EOL ;
107
+
108
+ if (!$ umd ) {
109
+ $ jsBody = $ this ->getES6Module ($ jsonLocales );
110
+ } else {
111
+ $ jsBody = $ this ->getUMDModule ($ jsonLocales );
112
+ }
113
+ file_put_contents ($ fileToCreate , $ jsBody );
114
+ }
115
+ return $ createdFiles ;
116
+ }
117
+
118
+
56
119
/**
57
120
* @param string $path
58
121
* @return array
@@ -63,9 +126,9 @@ private function allocateLocaleJSON($path)
63
126
if (pathinfo ($ path , PATHINFO_EXTENSION ) !== 'json ' ) {
64
127
return null ;
65
128
}
66
- $ tmp = (array ) json_decode (file_get_contents ($ path ));
129
+ $ tmp = (array )json_decode (file_get_contents ($ path ));
67
130
if (gettype ($ tmp ) !== "array " ) {
68
- throw new Exception ('Unexpected data while processing ' . $ path );
131
+ throw new Exception ('Unexpected data while processing ' . $ path );
69
132
}
70
133
71
134
return $ tmp ;
@@ -78,8 +141,8 @@ private function allocateLocaleJSON($path)
78
141
private function allocateLocaleArray ($ path )
79
142
{
80
143
$ data = [];
81
-
82
144
$ dir = new DirectoryIterator ($ path );
145
+ $ lastLocale = last ($ this ->availableLocales );
83
146
foreach ($ dir as $ fileinfo ) {
84
147
// Do not mess with dotfiles at all.
85
148
if ($ fileinfo ->isDot ()) {
@@ -88,6 +151,7 @@ private function allocateLocaleArray($path)
88
151
89
152
if ($ fileinfo ->isDir ()) {
90
153
// Recursivley iterate through subdirs, until everything is allocated.
154
+
91
155
$ data [$ fileinfo ->getFilename ()] =
92
156
$ this ->allocateLocaleArray ($ path . '/ ' . $ fileinfo ->getFilename ());
93
157
} else {
@@ -98,16 +162,24 @@ private function allocateLocaleArray($path)
98
162
if (pathinfo ($ fileName , PATHINFO_EXTENSION ) !== 'php ' ) {
99
163
continue ;
100
164
}
165
+
166
+
101
167
$ tmp = include ($ fileName );
168
+
102
169
if (gettype ($ tmp ) !== "array " ) {
103
- throw new Exception ('Unexpected data while processing ' . $ fileName );
170
+ throw new Exception ('Unexpected data while processing ' . $ fileName );
104
171
continue ;
105
172
}
173
+ if ($ lastLocale !== false ){
174
+ $ root = realpath (base_path () . config ('vue-i18n-generator.langPath ' ) . '/ ' . $ lastLocale );
175
+ $ filePath = $ this ->removeExtension (str_replace ('\\' , '_ ' , ltrim (str_replace ($ root , '' , realpath ($ fileName )), '\\' )));
176
+ $ this ->filesToCreate [$ filePath ][$ lastLocale ] = $ this ->adjustArray ($ tmp );
177
+ }
106
178
107
179
$ data [$ noExt ] = $ this ->adjustArray ($ tmp );
180
+
108
181
}
109
182
}
110
-
111
183
return $ data ;
112
184
}
113
185
@@ -118,15 +190,14 @@ private function allocateLocaleArray($path)
118
190
private function adjustArray (array $ arr )
119
191
{
120
192
$ res = [];
121
-
122
193
foreach ($ arr as $ key => $ val ) {
194
+
123
195
if (is_string ($ val )) {
124
196
$ res [$ key ] = $ this ->adjustString ($ val );
125
197
} else {
126
198
$ res [$ key ] = $ this ->adjustArray ($ val );
127
199
}
128
200
}
129
-
130
201
return $ res ;
131
202
}
132
203
0 commit comments