1 | <?php |
||
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 | 24 | public function __construct(Filesystem $filesystem) |
|
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 | 24 | public function read($locale, array $excluded = []) |
|
39 | { |
||
40 | // Empty our translations for a new read |
||
41 | 24 | $this->translations = collect(); |
|
42 | |||
43 | 24 | $files = $this->filesystem->listContents($locale); |
|
44 | |||
45 | 24 | foreach ($files as $file) { |
|
46 | 21 | $filename = substr($file['path'], strrpos($file['path'], '/')); |
|
47 | 21 | $filename = ltrim($filename, '/'); |
|
48 | 21 | $filename = str_replace('.php', '', $filename); |
|
49 | |||
50 | 21 | if (in_array($filename, $excluded)) { |
|
51 | 3 | continue; |
|
52 | } |
||
53 | |||
54 | 21 | $this->translations[$filename] = require $this->path . DIRECTORY_SEPARATOR . $file['path']; |
|
55 | } |
||
56 | |||
57 | 24 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return \Illuminate\Support\Collection |
||
62 | */ |
||
63 | 3 | public function get() |
|
67 | |||
68 | /** |
||
69 | * Flatten per file a multi-dimensional associative array with dots. |
||
70 | |||
71 | * @return \Illuminate\Support\Collection |
||
72 | */ |
||
73 | 1 | public function flattenPerFile() |
|
74 | { |
||
75 | 2 | return $this->translations->map(function ($values) { |
|
76 | 3 | return Arr::dot($values); |
|
77 | 3 | }); |
|
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 | * @return \Illuminate\Support\Collection |
||
85 | */ |
||
86 | public function flatten() |
||
87 | { |
||
88 | 18 | return $this->translations->map(function ($values, $groupkey) { |
|
89 | 15 | return $this->prependGroupkey($values, $groupkey); |
|
90 | 18 | })->collapse(); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $values |
||
95 | * @param $groupkey |
||
96 | * @return array |
||
97 | */ |
||
98 | 15 | private function prependGroupkey($values, $groupkey) |
|
99 | { |
||
100 | 15 | $values = Arr::dot($values); |
|
101 | 15 | $combined = []; |
|
102 | |||
103 | 15 | foreach ($values as $key => $value) { |
|
104 | // Empty arrays will be ignored in our flattening |
||
105 | 15 | if (is_array($value) && empty($value)) { |
|
106 | continue; |
||
107 | } |
||
108 | |||
109 | 15 | $combined[$groupkey . '.' . $key] = $value; |
|
110 | } |
||
111 | |||
112 | 15 | return $combined; |
|
113 | } |
||
114 | |||
115 | 24 | private function getSquantoLangPath() |
|
120 | } |
||
121 |