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