|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SolumDeSignum\PackageTranslatorLoader; |
|
6
|
|
|
|
|
7
|
|
|
use function app; |
|
8
|
|
|
use function config; |
|
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
10
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
11
|
|
|
use Illuminate\Translation\FileLoader; |
|
12
|
|
|
use Illuminate\Translation\Translator; |
|
13
|
|
|
|
|
14
|
|
|
class PackageTranslatorLoader |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
public array $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string|null |
|
23
|
|
|
*/ |
|
24
|
|
|
public ?string $locale = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private string $translator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Application |
|
33
|
|
|
*/ |
|
34
|
|
|
private Application $app; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* PackageTranslatorLoader constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Application $app |
|
40
|
|
|
* @param array|string[] $config |
|
41
|
|
|
* @param string|null $locale |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
Application $app, |
|
45
|
|
|
array $config = [ |
|
46
|
|
|
'translator' => 'package-translation-loader.translator', |
|
47
|
|
|
'nameSpace' => 'solumdesignum/package-translation-loader', |
|
48
|
|
|
'packageRootPath' => __DIR__.'/..', |
|
49
|
|
|
'loadLangPath' => '/../resources/lang', |
|
50
|
|
|
'loaderLangPath' => '/resources/lang', |
|
51
|
|
|
], |
|
52
|
|
|
?string $locale = null |
|
53
|
|
|
) { |
|
54
|
|
|
$this->app = $app; |
|
55
|
|
|
$this->config = $config; |
|
56
|
|
|
$this->translator = $this->config['translator']; |
|
57
|
|
|
$this->localeSetter($locale); |
|
58
|
|
|
$this->loadTranslations(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Custom Translation Loader |
|
63
|
|
|
* When registering the translator component, we'll need to set the default |
|
64
|
|
|
* locale as well as the fallback locale. So, we'll grab the application |
|
65
|
|
|
* configuration so we can easily get both of these values from there. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
final public function loadTranslations(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->app->bind( |
|
72
|
|
|
$this->translator, |
|
73
|
|
|
function ($app) { |
|
74
|
|
|
$trans = new Translator( |
|
75
|
|
|
$this->loader(), |
|
76
|
|
|
(string) ($this->locale !== null ? |
|
77
|
|
|
$this->locale : |
|
78
|
|
|
$this->locale( |
|
79
|
|
|
$app |
|
80
|
|
|
)) |
|
81
|
|
|
); |
|
82
|
|
|
$this->locale !== null ? $this->locale : $trans->setFallback( |
|
83
|
|
|
$app['config']['app.fallback_locale'] |
|
84
|
|
|
); |
|
85
|
|
|
$trans->addNamespace( |
|
86
|
|
|
$this->config['nameSpace'], |
|
87
|
|
|
__DIR__. |
|
88
|
|
|
$this->config['loadLangPath'] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
return $trans; |
|
92
|
|
|
} |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string|null $locale |
|
98
|
|
|
* |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
|
|
final public function setLocale(?string $locale = null): self |
|
102
|
|
|
{ |
|
103
|
|
|
$this->locale = $locale; |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return Application|mixed|string |
|
110
|
|
|
*/ |
|
111
|
|
|
final public function trans() |
|
112
|
|
|
{ |
|
113
|
|
|
return app($this->translator); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string|null $locale |
|
118
|
|
|
*/ |
|
119
|
|
|
private function localeSetter(?string $locale): void |
|
120
|
|
|
{ |
|
121
|
|
|
if ($locale !== null) { |
|
122
|
|
|
$this->locale = $locale; |
|
123
|
|
|
} else { |
|
124
|
|
|
$this->setLocale(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param Application $app |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
private function locale(Application $app): string |
|
134
|
|
|
{ |
|
135
|
|
|
return $app |
|
136
|
|
|
->get('request') |
|
137
|
|
|
->segment( |
|
138
|
|
|
config('package-translator-loader.segment', 1) |
|
139
|
|
|
) ?: $app->getLocale(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return FileLoader |
|
144
|
|
|
*/ |
|
145
|
|
|
private function loader(): FileLoader |
|
146
|
|
|
{ |
|
147
|
|
|
$filesystem = new Filesystem(); |
|
148
|
|
|
$resourcesLangPath = $this->config['packageRootPath'].$this->config['loaderLangPath']; |
|
149
|
|
|
$filesystem->allFiles($resourcesLangPath); |
|
150
|
|
|
|
|
151
|
|
|
return new FileLoader($filesystem, $resourcesLangPath); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|