@@ -7,17 +7,19 @@ class Generator
7
7
{
8
8
/**
9
9
* @param string $path
10
+ * @param boolean $umd
10
11
* @return string
11
12
* @throws Exception
12
13
*/
13
- public function generateFromPath ($ path )
14
+ public function generateFromPath ($ path, $ umd = null )
14
15
{
15
16
if (!is_dir ($ path )) {
16
17
throw new Exception ('Directory not found: ' .$ path );
17
18
}
18
19
19
20
$ locales = [];
20
21
$ dir = new DirectoryIterator ($ path );
22
+ $ jsBody = '' ;
21
23
22
24
foreach ($ dir as $ fileinfo ) {
23
25
if (!$ fileinfo ->isDot ()
@@ -40,8 +42,15 @@ public function generateFromPath($path)
40
42
}
41
43
}
42
44
43
- return 'export default '
44
- . json_encode ($ locales , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) . PHP_EOL ;
45
+ $ jsonLocales = json_encode ($ locales , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) . PHP_EOL ;
46
+
47
+ if (!$ umd ) {
48
+ $ jsBody = $ this ->getES6Module ($ jsonLocales );
49
+ } else {
50
+ $ jsBody = $ this ->getUMDModule ($ jsonLocales );
51
+ }
52
+
53
+ return $ jsBody ;
45
54
}
46
55
47
56
/**
@@ -155,4 +164,33 @@ private function removeExtension($filename)
155
164
156
165
return mb_substr ($ filename , 0 , $ pos );
157
166
}
158
- }
167
+
168
+ /**
169
+ * Returns an UMD style module
170
+ * @param string $body
171
+ * @return string
172
+ */
173
+ private function getUMDModule ($ body )
174
+ {
175
+ $ js = <<<HEREDOC
176
+ (function (global, factory) {
177
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
178
+ typeof define === 'function' && define.amd ? define(factory) :
179
+ (global.vuei18nLocales = factory());
180
+ }(this, (function () { 'use strict';
181
+ return {$ body }
182
+ })));
183
+ HEREDOC ;
184
+ return $ js ;
185
+ }
186
+
187
+ /**
188
+ * Returns an ES6 style module
189
+ * @param string $body
190
+ * @return string
191
+ */
192
+ private function getES6Module ($ body )
193
+ {
194
+ return "export default {$ body }" ;
195
+ }
196
+ }
0 commit comments