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