FileLoader.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Illuminate\Translation;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
class FileLoader implements Loader
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The default path for the loader.
*
* @var string
*/
protected $path;
/**
* All of the registered paths to JSON translation files.
*
* @var array
*/
protected $jsonPaths = [];
/**
* All of the namespace hints.
*
* @var array
*/
protected $hints = [];
/**
* Create a new file loader instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $path
* @return void
*/
public function __construct(Filesystem $files, $path)
{
$this->path = $path;
$this->files = $files;
}
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string|null $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if ($group === '*' && $namespace === '*') {
return $this->loadJsonPaths($locale);
}
if (is_null($namespace) || $namespace === '*') {
return $this->loadPath($this->path, $locale, $group);
}
return $this->loadNamespaced($locale, $group, $namespace);
}
/**
* Load a namespaced translation group.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaced($locale, $group, $namespace)
{
if (isset($this->hints[$namespace])) {
$lines = $this->loadPath($this->hints[$namespace], $locale, $group);
return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
}
return [];
}
/**
* Load a local namespaced translation group for overrides.
*
* @param array $lines
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
{
$file = "{$this->path}/vendor/{$namespace}/{$locale}/{$group}.php";
if ($this->files->exists($file)) {
return array_replace_recursive($lines, $this->files->getRequire($file));
}
return $lines;
}
/**
* Load a locale from a given path.
*
* @param string $path
* @param string $locale
* @param string $group
* @return array
*/
protected function loadPath($path, $locale, $group)
{
if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
return $this->files->getRequire($full);
}
return [];
}
/**
* Load a locale from the given JSON file path.
*
* @param string $locale
* @return array
*
* @throws \RuntimeException
*/
protected function loadJsonPaths($locale)
{
return collect(array_merge($this->jsonPaths, [$this->path]))
->reduce(function ($output, $path) use ($locale) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}
$output = array_merge($output, $decoded);
}
return $output;
}, []);
}
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint)
{
$this->hints[$namespace] = $hint;
}
/**
* Get an array of all the registered namespaces.
*
* @return array
*/
public function namespaces()
{
return $this->hints;
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->jsonPaths[] = $path;
}
/**
* Get an array of all the registered paths to JSON translation files.
*
* @return array
*/
public function jsonPaths()
{
return $this->jsonPaths;
}
}