|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use League\Flysystem\Filesystem; |
|
7
|
|
|
use Thinktomorrow\Squanto\Exceptions\EmptyTranslationFileException; |
|
8
|
|
|
|
|
9
|
|
|
class LaravelTranslationsReader |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Illuminate\Support\Collection |
|
13
|
|
|
*/ |
|
14
|
|
|
private $translations; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Local filesystem. Already contains the path to our translation files |
|
18
|
|
|
* e.g. storage/app/trans |
|
19
|
|
|
* |
|
20
|
|
|
* @var Filesystem |
|
21
|
|
|
*/ |
|
22
|
|
|
private $filesystem; |
|
23
|
|
|
private $path; |
|
24
|
|
|
|
|
25
|
9 |
|
public function __construct(Filesystem $filesystem) |
|
26
|
|
|
{ |
|
27
|
9 |
|
$this->filesystem = $filesystem; |
|
28
|
9 |
|
$this->path = $this->getSquantoLangPath(); |
|
29
|
9 |
|
$this->translations = collect(); |
|
30
|
9 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Read the lang files for a specific locale |
|
34
|
|
|
* |
|
35
|
|
|
* @param $locale |
|
36
|
|
|
* @param array $excluded filenames that should be excluded from the read |
|
37
|
|
|
* @return \Illuminate\Support\Collection |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function read($locale, array $excluded = []) |
|
40
|
|
|
{ |
|
41
|
|
|
// Empty our translations for a new read |
|
42
|
9 |
|
$this->translations = collect(); |
|
43
|
|
|
|
|
44
|
9 |
|
$files = $this->filesystem->listContents($locale); |
|
45
|
|
|
|
|
46
|
9 |
|
foreach ($files as $file) { |
|
47
|
8 |
|
$filename = substr($file['path'], strrpos($file['path'], '/')); |
|
48
|
8 |
|
$filename = ltrim($filename, '/'); |
|
49
|
8 |
|
$filename = str_replace('.php', '', $filename); |
|
50
|
|
|
|
|
51
|
8 |
|
if (in_array($filename, $excluded)) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
$this->translations[$filename] = require $this->path . DIRECTORY_SEPARATOR . $file['path']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
9 |
|
$this->validateTranslations(); |
|
59
|
|
|
|
|
60
|
8 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return \Illuminate\Support\Collection |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function get() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->translations; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Flatten per file a multi-dimensional associative array with dots. |
|
73
|
|
|
|
|
74
|
|
|
* @return \Illuminate\Support\Collection |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function flattenPerFile() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->translations->map(function ($values) { |
|
79
|
1 |
|
return Arr::dot($values); |
|
80
|
1 |
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Flatten all files which also flattens the filenames (groups) with dots. |
|
85
|
|
|
* Note: This will remove any empty language files |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Illuminate\Support\Collection |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function flatten() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->translations->map(function ($values, $groupkey) { |
|
92
|
5 |
|
return $this->prependGroupkey($values, $groupkey); |
|
93
|
6 |
|
})->collapse(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $values |
|
98
|
|
|
* @param $groupkey |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
5 |
|
private function prependGroupkey($values, $groupkey) |
|
102
|
|
|
{ |
|
103
|
5 |
|
$values = Arr::dot($values); |
|
104
|
5 |
|
$combined = []; |
|
105
|
|
|
|
|
106
|
5 |
|
foreach ($values as $key => $value) { |
|
107
|
|
|
// Empty arrays will be ignored in our flattening |
|
108
|
5 |
|
if (is_array($value) && empty($value)) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
$combined[$groupkey . '.' . $key] = $value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
5 |
|
return $combined; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
9 |
|
private function getSquantoLangPath() |
|
119
|
|
|
{ |
|
120
|
9 |
|
$path = config('squanto.lang_path'); |
|
121
|
9 |
|
return is_null($path) ? app('path.lang') : $path; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
9 |
|
public function validateTranslations() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->translations->each(function($translation, $key){ |
|
127
|
8 |
|
if(!is_array($translation)) |
|
128
|
|
|
{ |
|
129
|
1 |
|
throw new EmptyTranslationFileException('The file "' . $key . '.php" seems empty. Make sure every lang file returns an array.'); |
|
130
|
|
|
} |
|
131
|
9 |
|
}); |
|
132
|
8 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|