Skip to content

Commit 4fd4bf8

Browse files
Mohammad Roseffendimartinlindhe
Mohammad Roseffendi
authored andcommitted
Add published vendor translation (martinlindhe#37)
* Added vendor directory parsing * Added with-vendor command options * Added with-vendor command options * fixed test temp directory path
1 parent 4b9c4c9 commit 4fd4bf8

File tree

4 files changed

+101
-10
lines changed

4 files changed

+101
-10
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/vendor
2-
/.idea
2+
/.idea

src/Commands/GenerateInclude.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GenerateInclude extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'vue-i18n:generate {--umd} {--multi}';
14+
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor}';
1515

1616
/**
1717
* The console command description.
@@ -32,6 +32,8 @@ public function handle()
3232

3333
$multipleFiles = $this->option('multi');
3434

35+
$withVendor = $this->option('with-vendor');
36+
3537
$i18nLib = config('vue-i18n-generator.i18nLib');
3638

3739
if ($multipleFiles) {
@@ -42,7 +44,7 @@ public function handle()
4244
}
4345

4446
$data = (new Generator($i18nLib))
45-
->generateFromPath($root, $umd);
47+
->generateFromPath($root, $umd, $withVendor);
4648

4749

4850

src/Generator.php

+33-6
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public function __construct($i18nLib = self::VUE_I18N)
2828
/**
2929
* @param string $path
3030
* @param boolean $umd
31+
* @param boolean $withVendor
3132
* @return string
3233
* @throws Exception
3334
*/
34-
public function generateFromPath($path, $umd = null)
35+
public function generateFromPath($path, $umd = null, $withVendor = false)
3536
{
3637
if (!is_dir($path)) {
3738
throw new Exception('Directory not found: ' . $path);
@@ -41,9 +42,11 @@ public function generateFromPath($path, $umd = null)
4142
$dir = new DirectoryIterator($path);
4243
$jsBody = '';
4344
foreach ($dir as $fileinfo) {
44-
if (!$fileinfo->isDot()
45-
&& !in_array($fileinfo->getFilename(), ['vendor'])
46-
) {
45+
if (!$fileinfo->isDot()) {
46+
if(!$withVendor && in_array($fileinfo->getFilename(), ['vendor'])) {
47+
continue;
48+
}
49+
4750
$noExt = $this->removeExtension($fileinfo->getFilename());
4851

4952
if ($fileinfo->isDir()) {
@@ -58,11 +61,11 @@ public function generateFromPath($path, $umd = null)
5861
} else {
5962
$locales[$noExt] = $local;
6063
}
61-
62-
6364
}
6465
}
6566

67+
$locales = $this->adjustVendor($locales);
68+
6669
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
6770

6871
if (!$umd) {
@@ -221,6 +224,30 @@ private function adjustArray(array $arr)
221224
return $res;
222225
}
223226

227+
/**
228+
* Adjus vendor index placement.
229+
*
230+
* @param array $locales
231+
*
232+
* @return array
233+
*/
234+
private function adjustVendor($locales)
235+
{
236+
if(isset($locales['vendor'])) {
237+
foreach($locales['vendor'] as $vendor => $data) {
238+
foreach($data as $key => $group) {
239+
foreach($group as $locale => $lang) {
240+
$locales[$locale]['vendor'][$vendor][$key] = $lang;
241+
}
242+
}
243+
}
244+
245+
unset($locales['vendor']);
246+
}
247+
248+
return $locales;
249+
}
250+
224251
/**
225252
* Turn Laravel style ":link" into vue-i18n style "{link}" and
226253
* turn Laravel style "|" into vuex-i18n style ":::" when using vuex-i18n.

tests/GenerateTest.php

+63-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,54 @@ function testBasic()
107107
]
108108
]
109109
];
110+
$root = $this->generateLocaleFilesFrom($arr);
111+
$this->assertEquals(
112+
'export default {' . PHP_EOL
113+
. ' "en": {' . PHP_EOL
114+
. ' "help": {' . PHP_EOL
115+
. ' "yes": "yes",' . PHP_EOL
116+
. ' "no": "no"' . PHP_EOL
117+
. ' }' . PHP_EOL
118+
. ' },' . PHP_EOL
119+
. ' "sv": {' . PHP_EOL
120+
. ' "help": {' . PHP_EOL
121+
. ' "yes": "ja",' . PHP_EOL
122+
. ' "no": "nej"' . PHP_EOL
123+
. ' }' . PHP_EOL
124+
. ' }' . PHP_EOL
125+
. '}' . PHP_EOL,
126+
(new Generator)->generateFromPath($root));
127+
$this->destroyLocaleFilesFrom($arr, $root);
128+
}
129+
130+
function testBasicWithVendor()
131+
{
132+
$arr = [
133+
'en' => [
134+
'help' => [
135+
'yes' => 'yes',
136+
'no' => 'no',
137+
]
138+
],
139+
'sv' => [
140+
'help' => [
141+
'yes' => 'ja',
142+
'no' => 'nej',
143+
]
144+
],
145+
'vendor' => [
146+
'test-vendor' => [
147+
'test-lang' => [
148+
'en' => [
149+
'maybe' => 'maybe'
150+
],
151+
'sv' => [
152+
'maybe' => 'kanske'
153+
]
154+
]
155+
]
156+
],
157+
];
110158

111159
$root = $this->generateLocaleFilesFrom($arr);
112160

@@ -116,16 +164,30 @@ function testBasic()
116164
. ' "help": {' . PHP_EOL
117165
. ' "yes": "yes",' . PHP_EOL
118166
. ' "no": "no"' . PHP_EOL
167+
. ' },' . PHP_EOL
168+
. ' "vendor": {' . PHP_EOL
169+
. ' "test-vendor": {' . PHP_EOL
170+
. ' "test-lang": {' . PHP_EOL
171+
. ' "maybe": "maybe"' . PHP_EOL
172+
. ' }' . PHP_EOL
173+
. ' }' . PHP_EOL
119174
. ' }' . PHP_EOL
120175
. ' },' . PHP_EOL
121176
. ' "sv": {' . PHP_EOL
122177
. ' "help": {' . PHP_EOL
123178
. ' "yes": "ja",' . PHP_EOL
124179
. ' "no": "nej"' . PHP_EOL
180+
. ' },' . PHP_EOL
181+
. ' "vendor": {' . PHP_EOL
182+
. ' "test-vendor": {' . PHP_EOL
183+
. ' "test-lang": {' . PHP_EOL
184+
. ' "maybe": "kanske"' . PHP_EOL
185+
. ' }' . PHP_EOL
186+
. ' }' . PHP_EOL
125187
. ' }' . PHP_EOL
126188
. ' }' . PHP_EOL
127189
. '}' . PHP_EOL,
128-
(new Generator)->generateFromPath($root));
190+
(new Generator)->generateFromPath($root, false, true));
129191

130192
$this->destroyLocaleFilesFrom($arr, $root);
131193
}

0 commit comments

Comments
 (0)