1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelLang; |
6
|
|
|
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Translation\FileLoader as IlluminateFileLoader; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FileLoader |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class FileLoader extends IlluminateFileLoader |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Vendor directory path. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $vendorPaths; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Supported locales. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $locales; |
35
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
37
|
|
|
| Constructor |
38
|
|
|
| ----------------------------------------------------------------- |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new file loader instance. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
45
|
|
|
* @param string $path |
46
|
|
|
* @param array $vendorPaths |
47
|
|
|
* @param array $locales |
48
|
|
|
*/ |
49
|
66 |
|
public function __construct(Filesystem $files, string $path, array $vendorPaths, array $locales = []) |
50
|
|
|
{ |
51
|
66 |
|
parent::__construct($files, $path); |
52
|
|
|
|
53
|
66 |
|
$this->setVendorPaths($vendorPaths); |
54
|
66 |
|
$this->setSupportedLocales($locales); |
55
|
66 |
|
} |
56
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
58
|
|
|
| Getters & Setters |
59
|
|
|
| ----------------------------------------------------------------- |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the vendor path. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
6 |
|
private function getVendorPaths(): array |
68
|
|
|
{ |
69
|
6 |
|
return $this->vendorPaths; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the vendor paths. |
74
|
|
|
* |
75
|
|
|
* @param array $vendorPaths |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
66 |
|
private function setVendorPaths(array $vendorPaths): self |
80
|
|
|
{ |
81
|
66 |
|
$this->vendorPaths = $vendorPaths; |
82
|
|
|
|
83
|
66 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the supported locales. |
88
|
|
|
* |
89
|
|
|
* @param array $locales |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
66 |
|
private function setSupportedLocales(array $locales): self |
94
|
|
|
{ |
95
|
66 |
|
$this->locales = $locales; |
96
|
|
|
|
97
|
66 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/* ----------------------------------------------------------------- |
101
|
|
|
| Main Methods |
102
|
|
|
| ----------------------------------------------------------------- |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Load the messages for the given locale. |
107
|
|
|
* |
108
|
|
|
* @param string $locale |
109
|
|
|
* @param string $group |
110
|
|
|
* @param string $namespace |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
12 |
|
public function load($locale, $group, $namespace = null): array |
115
|
|
|
{ |
116
|
12 |
|
$defaults = []; |
117
|
|
|
|
118
|
12 |
|
if (empty($this->locales) || $this->isSupported($locale)) { |
119
|
6 |
|
foreach ($this->getVendorPaths() as $path) { |
120
|
6 |
|
if ( ! empty($defaults = $this->loadPath($path, $locale, $group))) |
121
|
6 |
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
12 |
|
return array_replace_recursive($defaults, parent::load($locale, $group, $namespace)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/* ----------------------------------------------------------------- |
129
|
|
|
| Check Methods |
130
|
|
|
| ----------------------------------------------------------------- |
131
|
|
|
*/ |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check if the locale is supported or use the fallback. |
135
|
|
|
* |
136
|
|
|
* @param string $locale |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
12 |
|
private function isSupported($locale): bool |
141
|
|
|
{ |
142
|
12 |
|
return in_array($locale, $this->locales); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|