-
Notifications
You must be signed in to change notification settings - Fork 7
/
TranslatorRegistry.php
351 lines (310 loc) · 9.87 KB
/
TranslatorRegistry.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;
/**
* Constructs and stores instances of translators that can be
* retrieved by name and locale.
*/
class TranslatorRegistry
{
/**
* Fallback loader name.
*
* @var string
*/
public const FALLBACK_LOADER = '_fallback';
/**
* A registry to retain translator objects.
*
* @var array
* @psalm-var array<string, array<string, \Cake\I18n\Translator>>
*/
protected $registry = [];
/**
* The current locale code.
*
* @var string
*/
protected $locale;
/**
* A package locator.
*
* @var \Cake\I18n\PackageLocator
*/
protected $packages;
/**
* A formatter locator.
*
* @var \Cake\I18n\FormatterLocator
*/
protected $formatters;
/**
* A list of loader functions indexed by domain name. Loaders are
* callables that are invoked as a default for building translation
* packages where none can be found for the combination of translator
* name and locale.
*
* @var callable[]
*/
protected $_loaders = [];
/**
* The name of the default formatter to use for newly created
* translators from the fallback loader
*
* @var string
*/
protected $_defaultFormatter = 'default';
/**
* Use fallback-domain for translation loaders.
*
* @var bool
*/
protected $_useFallback = true;
/**
* A CacheEngine object that is used to remember translator across
* requests.
*
* @var (\Psr\SimpleCache\CacheInterface&\Cake\Cache\CacheEngineInterface)|null
*/
protected $_cacher;
/**
* Constructor.
*
* @param \Cake\I18n\PackageLocator $packages The package locator.
* @param \Cake\I18n\FormatterLocator $formatters The formatter locator.
* @param string $locale The default locale code to use.
*/
public function __construct(
PackageLocator $packages,
FormatterLocator $formatters,
string $locale
) {
$this->packages = $packages;
$this->formatters = $formatters;
$this->setLocale($locale);
$this->registerLoader(static::FALLBACK_LOADER, function ($name, $locale) {
$loader = new ChainMessagesLoader([
new MessagesFileLoader($name, $locale, 'mo'),
new MessagesFileLoader($name, $locale, 'po'),
]);
$formatter = $name === 'cake' ? 'default' : $this->_defaultFormatter;
$package = $loader();
$package->setFormatter($formatter);
return $package;
});
}
/**
* Sets the default locale code.
*
* @param string $locale The new locale code.
* @return void
*/
public function setLocale(string $locale): void
{
$this->locale = $locale;
}
/**
* Returns the default locale code.
*
* @return string
*/
public function getLocale(): string
{
return $this->locale;
}
/**
* Returns the translator packages
*
* @return \Cake\I18n\PackageLocator
*/
public function getPackages(): PackageLocator
{
return $this->packages;
}
/**
* An object of type FormatterLocator
*
* @return \Cake\I18n\FormatterLocator
*/
public function getFormatters(): FormatterLocator
{
return $this->formatters;
}
/**
* Sets the CacheEngine instance used to remember translators across
* requests.
*
* @param \Psr\SimpleCache\CacheInterface&\Cake\Cache\CacheEngineInterface $cacher The cacher instance.
* @return void
*/
public function setCacher($cacher): void
{
$this->_cacher = $cacher;
}
/**
* Gets a translator from the registry by package for a locale.
*
* @param string $name The translator package to retrieve.
* @param string|null $locale The locale to use; if empty, uses the default
* locale.
* @return \Cake\I18n\Translator|null A translator object.
* @throws \Cake\I18n\Exception\I18nException If no translator with that name could be found
* for the given locale.
*/
public function get(string $name, ?string $locale = null): ?Translator
{
if ($locale === null) {
$locale = $this->getLocale();
}
if (isset($this->registry[$name][$locale])) {
return $this->registry[$name][$locale];
}
if ($this->_cacher === null) {
return $this->registry[$name][$locale] = $this->_getTranslator($name, $locale);
}
// Cache keys cannot contain / if they go to file engine.
$keyName = str_replace('/', '.', $name);
$key = "translations.{$keyName}.{$locale}";
$translator = $this->_cacher->get($key);
if (!$translator || !$translator->getPackage()) {
$translator = $this->_getTranslator($name, $locale);
$this->_cacher->set($key, $translator);
}
return $this->registry[$name][$locale] = $translator;
}
/**
* Gets a translator from the registry by package for a locale.
*
* @param string $name The translator package to retrieve.
* @param string $locale The locale to use; if empty, uses the default
* locale.
* @return \Cake\I18n\Translator A translator object.
*/
protected function _getTranslator(string $name, string $locale): Translator
{
if ($this->packages->has($name, $locale)) {
return $this->createInstance($name, $locale);
}
if (isset($this->_loaders[$name])) {
$package = $this->_loaders[$name]($name, $locale);
} else {
$package = $this->_loaders[static::FALLBACK_LOADER]($name, $locale);
}
$package = $this->setFallbackPackage($name, $package);
$this->packages->set($name, $locale, $package);
return $this->createInstance($name, $locale);
}
/**
* Create translator instance.
*
* @param string $name The translator package to retrieve.
* @param string $locale The locale to use; if empty, uses the default locale.
* @return \Cake\I18n\Translator A translator object.
*/
protected function createInstance(string $name, string $locale): Translator
{
$package = $this->packages->get($name, $locale);
$fallback = $package->getFallback();
if ($fallback !== null) {
$fallback = $this->get($fallback, $locale);
}
$formatter = $this->formatters->get($package->getFormatter());
return new Translator($locale, $package, $formatter, $fallback);
}
/**
* Registers a loader function for a package name that will be used as a fallback
* in case no package with that name can be found.
*
* Loader callbacks will get as first argument the package name and the locale as
* the second argument.
*
* @param string $name The name of the translator package to register a loader for
* @param callable $loader A callable object that should return a Package
* @return void
*/
public function registerLoader(string $name, callable $loader): void
{
$this->_loaders[$name] = $loader;
}
/**
* Sets the name of the default messages formatter to use for future
* translator instances.
*
* If called with no arguments, it will return the currently configured value.
*
* @param string|null $name The name of the formatter to use.
* @return string The name of the formatter.
*/
public function defaultFormatter(?string $name = null): string
{
if ($name === null) {
return $this->_defaultFormatter;
}
return $this->_defaultFormatter = $name;
}
/**
* Set if the default domain fallback is used.
*
* @param bool $enable flag to enable or disable fallback
* @return void
*/
public function useFallback(bool $enable = true): void
{
$this->_useFallback = $enable;
}
/**
* Set fallback domain for package.
*
* @param string $name The name of the package.
* @param \Cake\I18n\Package $package Package instance
* @return \Cake\I18n\Package
*/
public function setFallbackPackage(string $name, Package $package): Package
{
if ($package->getFallback()) {
return $package;
}
$fallbackDomain = null;
if ($this->_useFallback && $name !== 'default') {
$fallbackDomain = 'default';
}
$package->setFallback($fallbackDomain);
return $package;
}
/**
* Set domain fallback for loader.
*
* @param string $name The name of the loader domain
* @param callable $loader invokable loader
* @return callable loader
*/
public function setLoaderFallback(string $name, callable $loader): callable
{
$fallbackDomain = 'default';
if (!$this->_useFallback || $name === $fallbackDomain) {
return $loader;
}
$loader = function () use ($loader, $fallbackDomain) {
/** @var \Cake\I18n\Package $package */
$package = $loader();
if (!$package->getFallback()) {
$package->setFallback($fallbackDomain);
}
return $package;
};
return $loader;
}
}