forked from cakephp/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslatorRegistry.php
301 lines (265 loc) · 8.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;
use Aura\Intl\Exception;
use Aura\Intl\FormatterLocator;
use Aura\Intl\PackageLocator;
use Aura\Intl\TranslatorLocator;
use Cake\Cache\CacheEngine;
use Cake\I18n\TranslatorFactory;
/**
* Constructs and stores instances of translators that can be
* retrieved by name and locale.
*/
class TranslatorRegistry extends TranslatorLocator
{
/**
* 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 = [];
/**
* Fallback loader name
*
* @var string
*/
protected $_fallbackLoader = '_fallback';
/**
* 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 \Cake\Cache\CacheEngine
*/
protected $_cacher;
/**
* Constructor.
*
* @param \Aura\Intl\PackageLocator $packages The package locator.
* @param \Aura\Intl\FormatterLocator $formatters The formatter locator.
* @param \Cake\I18n\TranslatorFactory $factory A translator factory to
* create translator objects for the locale and package.
* @param string $locale The default locale code to use.
*/
public function __construct(
PackageLocator $packages,
FormatterLocator $formatters,
TranslatorFactory $factory,
$locale
) {
parent::__construct($packages, $formatters, $factory, $locale);
$this->registerLoader($this->_fallbackLoader, function ($name, $locale) {
$chain = new ChainMessagesLoader([
new MessagesFileLoader($name, $locale, 'mo'),
new MessagesFileLoader($name, $locale, 'po')
]);
// \Aura\Intl\Package by default uses formatter configured with key "basic".
// and we want to make sure the cake domain always uses the default formatter
$formatter = $name === 'cake' ? 'default' : $this->_defaultFormatter;
$chain = function () use ($formatter, $chain) {
$package = $chain();
$package->setFormatter($formatter);
return $package;
};
return $chain;
});
}
/**
* Sets the CacheEngine instance used to remember translators across
* requests.
*
* @param \Cake\Cache\CacheEngine $cacher The cacher instance.
* @return void
*/
public function setCacher(CacheEngine $cacher)
{
$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 \Aura\Intl\TranslatorInterface|null A translator object.
* @throws \Aura\Intl\Exception If no translator with that name could be found
* for the given locale.
*/
public function get($name, $locale = null)
{
if (!$name) {
return null;
}
if ($locale === null) {
$locale = $this->getLocale();
}
if (isset($this->registry[$name][$locale])) {
return $this->registry[$name][$locale];
}
if (!$this->_cacher) {
return $this->registry[$name][$locale] = $this->_getTranslator($name, $locale);
}
$key = "translations.$name.$locale";
$translator = $this->_cacher->read($key);
if (!$translator) {
$translator = $this->_getTranslator($name, $locale);
$this->_cacher->write($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|null $locale The locale to use; if empty, uses the default
* locale.
* @return \Aura\Intl\TranslatorInterface A translator object.
*/
protected function _getTranslator($name, $locale)
{
try {
return parent::get($name, $locale);
} catch (Exception $e) {
}
if (!isset($this->_loaders[$name])) {
$this->registerLoader($name, $this->_partialLoader());
}
return $this->_getFromLoader($name, $locale);
}
/**
* 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($name, callable $loader)
{
$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($name = null)
{
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($enable = true)
{
$this->_useFallback = $enable;
}
/**
* Returns a new translator instance for the given name and locale
* based of conventions.
*
* @param string $name The translation package name.
* @param string $locale The locale to create the translator for.
* @return \Aura\Intl\Translator
*/
protected function _fallbackLoader($name, $locale)
{
return $this->_loaders[$this->_fallbackLoader]($name, $locale);
}
/**
* Returns a function that can be used as a loader for the registerLoaderMethod
*
* @return callable
*/
protected function _partialLoader()
{
return function ($name, $locale) {
return $this->_fallbackLoader($name, $locale);
};
}
/**
* Registers a new package by passing the register loaded function for the
* package name.
*
* @param string $name The name of the translator package
* @param string $locale The locale that should be built the package for
* @return \Aura\Intl\TranslatorInterface A translator object.
*/
protected function _getFromLoader($name, $locale)
{
$loader = $this->_loaders[$name]($name, $locale);
$package = $loader;
if (!is_callable($loader)) {
$loader = function () use ($package) {
return $package;
};
}
$loader = $this->setLoaderFallback($name, $loader);
$this->packages->set($name, $locale, $loader);
return parent::get($name, $locale);
}
/**
* 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($name, callable $loader)
{
$fallbackDomain = 'default';
if (!$this->_useFallback || $name === $fallbackDomain) {
return $loader;
}
$loader = function () use ($loader, $fallbackDomain) {
/* @var \Aura\Intl\Package $package */
$package = $loader();
if (!$package->getFallback()) {
$package->setFallback($fallbackDomain);
}
return $package;
};
return $loader;
}
}