1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Bootstrappers\I18n; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Env; |
8
|
|
|
use AbterPhp\Framework\Constant\Session; |
9
|
|
|
use AbterPhp\Framework\Environments\Environment; |
10
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
11
|
|
|
use AbterPhp\Framework\I18n\Translator; |
12
|
|
|
use Opulence\Framework\Configuration\Config; |
13
|
|
|
use Opulence\Ioc\Bootstrappers\Bootstrapper; |
14
|
|
|
use Opulence\Ioc\IContainer; |
15
|
|
|
use Opulence\Ioc\IocException; |
16
|
|
|
use Opulence\Sessions\ISession; |
17
|
|
|
use Opulence\Views\Compilers\Fortune\ITranspiler; |
18
|
|
|
|
19
|
|
|
class I18nBootstrapper extends Bootstrapper |
20
|
|
|
{ |
21
|
|
|
protected const LANG_PATH = 'lang'; |
22
|
|
|
|
23
|
|
|
protected ?array $resourcePaths = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getResourcePaths(): array |
29
|
|
|
{ |
30
|
|
|
global $abterModuleManager; |
31
|
|
|
|
32
|
|
|
if ($this->resourcePaths !== null) { |
33
|
|
|
return $this->resourcePaths; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$paths = $abterModuleManager->getResourcePaths() ?: []; |
37
|
|
|
|
38
|
|
|
$this->resourcePaths = $paths; |
39
|
|
|
|
40
|
|
|
return $paths; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $resourcePaths |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setResourcePaths(array $resourcePaths): self |
49
|
|
|
{ |
50
|
|
|
$this->resourcePaths = $resourcePaths; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
* @throws IocException |
58
|
|
|
*/ |
59
|
|
|
public function registerBindings(IContainer $container): void |
60
|
|
|
{ |
61
|
|
|
$this->registerTranslator($container); |
62
|
|
|
$this->registerViewFunction($container); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param IContainer $container |
67
|
|
|
* |
68
|
|
|
* @throws IocException |
69
|
|
|
*/ |
70
|
|
|
private function registerTranslator(IContainer $container): void |
71
|
|
|
{ |
72
|
|
|
$lang = $this->getLang($container); |
73
|
|
|
|
74
|
|
|
$translations = $this->getTranslations($lang); |
75
|
|
|
|
76
|
|
|
$translator = new Translator($translations); |
77
|
|
|
|
78
|
|
|
$container->bindInstance(Translator::class, $translator); |
79
|
|
|
$container->bindInstance(ITranslator::class, $translator); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param IContainer $container |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* @throws IocException |
87
|
|
|
*/ |
88
|
|
|
protected function getLang(IContainer $container): string |
89
|
|
|
{ |
90
|
|
|
/** @var ISession $session */ |
91
|
|
|
$session = $container->resolve(ISession::class); |
92
|
|
|
|
93
|
|
|
if ($session->has(Session::LANGUAGE_IDENTIFIER)) { |
94
|
|
|
return (string)$session->get(Session::LANGUAGE_IDENTIFIER); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return Environment::mustGetVar(Env::DEFAULT_LANGUAGE); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $lang |
102
|
|
|
* |
103
|
|
|
* @return array<string,array<string,string>> |
104
|
|
|
*/ |
105
|
|
|
protected function getTranslations(string $lang): array |
106
|
|
|
{ |
107
|
|
|
$translations = []; |
108
|
|
|
foreach ($this->getLangDirs($lang) as $dir) { |
109
|
|
|
$translations = array_merge($translations, $this->scanDir($dir)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $translations; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $dir |
117
|
|
|
* |
118
|
|
|
* @return array<string,array<string,string>> |
119
|
|
|
*/ |
120
|
|
|
protected function scanDir(string $dir): array |
121
|
|
|
{ |
122
|
|
|
if (!is_dir($dir)) { |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$translations = []; |
127
|
|
|
foreach (scandir($dir) as $file) { |
128
|
|
|
if (strlen($file) < 4 || substr($file, -4) !== '.php') { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$key = substr($file, 0, -4); |
133
|
|
|
$value = require rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file; |
134
|
|
|
|
135
|
|
|
$translations[$key] = $value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $translations; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $lang |
143
|
|
|
* |
144
|
|
|
* @return string[] |
145
|
|
|
*/ |
146
|
|
|
protected function getLangDirs(string $lang): array |
147
|
|
|
{ |
148
|
|
|
$paths = []; |
149
|
|
|
|
150
|
|
|
$globalPath = Config::get('paths', 'resources.lang'); |
151
|
|
|
if ($globalPath) { |
152
|
|
|
$paths[] = sprintf('%s/%s/', $globalPath, $lang); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach ($this->getResourcePaths() as $path) { |
156
|
|
|
$a = rtrim($path, DIRECTORY_SEPARATOR); |
157
|
|
|
$b = static::LANG_PATH; |
158
|
|
|
$c = rtrim($lang, DIRECTORY_SEPARATOR); |
159
|
|
|
|
160
|
|
|
$paths[] = $a . DIRECTORY_SEPARATOR . $b . DIRECTORY_SEPARATOR . $c; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $paths; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param IContainer $container |
168
|
|
|
* |
169
|
|
|
* @throws IocException |
170
|
|
|
*/ |
171
|
|
|
private function registerViewFunction(IContainer $container): void |
172
|
|
|
{ |
173
|
|
|
/** @var Translator $translator */ |
174
|
|
|
$translator = $container->resolve(Translator::class); |
175
|
|
|
|
176
|
|
|
/** @var ITranspiler $transpiler */ |
177
|
|
|
$transpiler = $container->resolve(ITranspiler::class); |
178
|
|
|
$transpiler->registerViewFunction( |
179
|
|
|
'tr', |
180
|
|
|
fn (string $key, ...$args) => $translator->translate($key, ...$args) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|