Skip to content

Commit 4370cf7

Browse files
ferlealmartinlindhe
authored andcommitted
Individual locales files (martinlindhe#22)
* Added Option to create Multiple Files with multiple languages so it can be implemented via Component based localization * Added option to change Locales so we use Lang:get() on our language files on laravel and recive a pure js fully translated , example in php 'error' => Lang::get('errors.please_enter_valid',['label' => Lang::get('words.email')]), on js "en": { "email": { "error": "Please enter a valid Email Address", }, }, "es": { "email": { "error": "Por favor inserta un Correo Electronico correcto", }, } * Fixed multi line on privates Added validation on allocateLocaleArray to prevent creation of fileToCreate if no Locale is defined
1 parent 914f8e9 commit 4370cf7

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed

src/Commands/GenerateInclude.php

Lines changed: 11 additions & 2 deletions
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}';
14+
protected $signature = 'vue-i18n:generate {--umd} {--multi}';
1515

1616
/**
1717
* The console command description.
@@ -30,11 +30,20 @@ public function handle()
3030

3131
$umd = $this->option('umd');
3232

33+
$multipleFiles = $this->option('multi');
34+
35+
if ($multipleFiles) {
36+
$files = (new Generator)
37+
->generateMultiple($root, $umd);
38+
echo "Written to :" . PHP_EOL . $files . PHP_EOL;
39+
exit();
40+
}
41+
3342
$data = (new Generator)
3443
->generateFromPath($root, $umd);
3544

36-
$jsFile = base_path() . config('vue-i18n-generator.jsFile');
3745

46+
$jsFile = base_path() . config('vue-i18n-generator.jsFile');
3847
file_put_contents($jsFile, $data);
3948

4049
echo "Written to " . $jsFile . PHP_EOL;

src/Generator.php

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
use DirectoryIterator;
44
use Exception;
5+
use App;
56

67
class Generator
78
{
9+
10+
private $availableLocales = [];
11+
private $filesToCreate = [];
12+
813
/**
914
* @param string $path
1015
* @param boolean $umd
@@ -14,13 +19,12 @@ class Generator
1419
public function generateFromPath($path, $umd = null)
1520
{
1621
if (!is_dir($path)) {
17-
throw new Exception('Directory not found: '.$path);
22+
throw new Exception('Directory not found: ' . $path);
1823
}
1924

2025
$locales = [];
2126
$dir = new DirectoryIterator($path);
2227
$jsBody = '';
23-
2428
foreach ($dir as $fileinfo) {
2529
if (!$fileinfo->isDot()
2630
&& !in_array($fileinfo->getFilename(), ['vendor'])
@@ -39,20 +43,79 @@ public function generateFromPath($path, $umd = null)
3943
} else {
4044
$locales[$noExt] = $local;
4145
}
46+
47+
4248
}
4349
}
4450

4551
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
4652

47-
if(!$umd) {
48-
$jsBody = $this->getES6Module($jsonLocales);
53+
if (!$umd) {
54+
$jsBody = $this->getES6Module($jsonLocales);
4955
} else {
5056
$jsBody = $this->getUMDModule($jsonLocales);
5157
}
52-
5358
return $jsBody;
5459
}
5560

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+
56119
/**
57120
* @param string $path
58121
* @return array
@@ -63,9 +126,9 @@ private function allocateLocaleJSON($path)
63126
if (pathinfo($path, PATHINFO_EXTENSION) !== 'json') {
64127
return null;
65128
}
66-
$tmp = (array) json_decode(file_get_contents($path));
129+
$tmp = (array)json_decode(file_get_contents($path));
67130
if (gettype($tmp) !== "array") {
68-
throw new Exception('Unexpected data while processing '.$path);
131+
throw new Exception('Unexpected data while processing ' . $path);
69132
}
70133

71134
return $tmp;
@@ -78,8 +141,8 @@ private function allocateLocaleJSON($path)
78141
private function allocateLocaleArray($path)
79142
{
80143
$data = [];
81-
82144
$dir = new DirectoryIterator($path);
145+
$lastLocale = last($this->availableLocales);
83146
foreach ($dir as $fileinfo) {
84147
// Do not mess with dotfiles at all.
85148
if ($fileinfo->isDot()) {
@@ -88,6 +151,7 @@ private function allocateLocaleArray($path)
88151

89152
if ($fileinfo->isDir()) {
90153
// Recursivley iterate through subdirs, until everything is allocated.
154+
91155
$data[$fileinfo->getFilename()] =
92156
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
93157
} else {
@@ -98,16 +162,24 @@ private function allocateLocaleArray($path)
98162
if (pathinfo($fileName, PATHINFO_EXTENSION) !== 'php') {
99163
continue;
100164
}
165+
166+
101167
$tmp = include($fileName);
168+
102169
if (gettype($tmp) !== "array") {
103-
throw new Exception('Unexpected data while processing '.$fileName);
170+
throw new Exception('Unexpected data while processing ' . $fileName);
104171
continue;
105172
}
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+
}
106178

107179
$data[$noExt] = $this->adjustArray($tmp);
180+
108181
}
109182
}
110-
111183
return $data;
112184
}
113185

@@ -118,15 +190,14 @@ private function allocateLocaleArray($path)
118190
private function adjustArray(array $arr)
119191
{
120192
$res = [];
121-
122193
foreach ($arr as $key => $val) {
194+
123195
if (is_string($val)) {
124196
$res[$key] = $this->adjustString($val);
125197
} else {
126198
$res[$key] = $this->adjustArray($val);
127199
}
128200
}
129-
130201
return $res;
131202
}
132203

src/config/vue-i18n-generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
| Note: the path will be prepended to point to the App directory.
2424
|
2525
*/
26-
26+
'jsPath' => '/resources/assets/js/langs/',
2727
'jsFile' => '/resources/assets/js/vue-i18n-locales.generated.js'
2828
];

0 commit comments

Comments
 (0)