| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of the Carbon package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Brian Nesbitt <brian@nesbot.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Carbon;
|
|
|
13 |
|
|
|
14 |
use Carbon\MessageFormatter\MessageFormatterMapper;
|
|
|
15 |
use Closure;
|
|
|
16 |
use ReflectionException;
|
|
|
17 |
use ReflectionFunction;
|
|
|
18 |
use Symfony\Component\Translation;
|
|
|
19 |
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
|
|
|
20 |
use Symfony\Component\Translation\Loader\ArrayLoader;
|
|
|
21 |
|
|
|
22 |
abstract class AbstractTranslator extends Translation\Translator
|
|
|
23 |
{
|
|
|
24 |
/**
|
|
|
25 |
* Translator singletons for each language.
|
|
|
26 |
*
|
|
|
27 |
* @var array
|
|
|
28 |
*/
|
|
|
29 |
protected static $singletons = [];
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* List of custom localized messages.
|
|
|
33 |
*
|
|
|
34 |
* @var array
|
|
|
35 |
*/
|
|
|
36 |
protected $messages = [];
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* List of custom directories that contain translation files.
|
|
|
40 |
*
|
|
|
41 |
* @var string[]
|
|
|
42 |
*/
|
|
|
43 |
protected $directories = [];
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Set to true while constructing.
|
|
|
47 |
*
|
|
|
48 |
* @var bool
|
|
|
49 |
*/
|
|
|
50 |
protected $initializing = false;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* List of locales aliases.
|
|
|
54 |
*
|
|
|
55 |
* @var array<string, string>
|
|
|
56 |
*/
|
|
|
57 |
protected $aliases = [
|
|
|
58 |
'me' => 'sr_Latn_ME',
|
|
|
59 |
'scr' => 'sh',
|
|
|
60 |
];
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Return a singleton instance of Translator.
|
|
|
64 |
*
|
|
|
65 |
* @param string|null $locale optional initial locale ("en" - english by default)
|
|
|
66 |
*
|
|
|
67 |
* @return static
|
|
|
68 |
*/
|
|
|
69 |
public static function get($locale = null)
|
|
|
70 |
{
|
|
|
71 |
$locale = $locale ?: 'en';
|
|
|
72 |
$key = static::class === Translator::class ? $locale : static::class.'|'.$locale;
|
|
|
73 |
|
|
|
74 |
if (!isset(static::$singletons[$key])) {
|
|
|
75 |
static::$singletons[$key] = new static($locale);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return static::$singletons[$key];
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
|
|
|
82 |
{
|
|
|
83 |
parent::setLocale($locale);
|
|
|
84 |
$this->initializing = true;
|
|
|
85 |
$this->directories = [__DIR__.'/Lang'];
|
|
|
86 |
$this->addLoader('array', new ArrayLoader());
|
|
|
87 |
parent::__construct($locale, new MessageFormatterMapper($formatter), $cacheDir, $debug);
|
|
|
88 |
$this->initializing = false;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Returns the list of directories translation files are searched in.
|
|
|
93 |
*
|
|
|
94 |
* @return array
|
|
|
95 |
*/
|
|
|
96 |
public function getDirectories(): array
|
|
|
97 |
{
|
|
|
98 |
return $this->directories;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Set list of directories translation files are searched in.
|
|
|
103 |
*
|
|
|
104 |
* @param array $directories new directories list
|
|
|
105 |
*
|
|
|
106 |
* @return $this
|
|
|
107 |
*/
|
|
|
108 |
public function setDirectories(array $directories)
|
|
|
109 |
{
|
|
|
110 |
$this->directories = $directories;
|
|
|
111 |
|
|
|
112 |
return $this;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Add a directory to the list translation files are searched in.
|
|
|
117 |
*
|
|
|
118 |
* @param string $directory new directory
|
|
|
119 |
*
|
|
|
120 |
* @return $this
|
|
|
121 |
*/
|
|
|
122 |
public function addDirectory(string $directory)
|
|
|
123 |
{
|
|
|
124 |
$this->directories[] = $directory;
|
|
|
125 |
|
|
|
126 |
return $this;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Remove a directory from the list translation files are searched in.
|
|
|
131 |
*
|
|
|
132 |
* @param string $directory directory path
|
|
|
133 |
*
|
|
|
134 |
* @return $this
|
|
|
135 |
*/
|
|
|
136 |
public function removeDirectory(string $directory)
|
|
|
137 |
{
|
|
|
138 |
$search = rtrim(strtr($directory, '\\', '/'), '/');
|
|
|
139 |
|
|
|
140 |
return $this->setDirectories(array_filter($this->getDirectories(), function ($item) use ($search) {
|
|
|
141 |
return rtrim(strtr($item, '\\', '/'), '/') !== $search;
|
|
|
142 |
}));
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Reset messages of a locale (all locale if no locale passed).
|
|
|
147 |
* Remove custom messages and reload initial messages from matching
|
|
|
148 |
* file in Lang directory.
|
|
|
149 |
*
|
|
|
150 |
* @param string|null $locale
|
|
|
151 |
*
|
|
|
152 |
* @return bool
|
|
|
153 |
*/
|
|
|
154 |
public function resetMessages($locale = null)
|
|
|
155 |
{
|
|
|
156 |
if ($locale === null) {
|
|
|
157 |
$this->messages = [];
|
|
|
158 |
|
|
|
159 |
return true;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
foreach ($this->getDirectories() as $directory) {
|
|
|
163 |
$data = @include sprintf('%s/%s.php', rtrim($directory, '\\/'), $locale);
|
|
|
164 |
|
|
|
165 |
if ($data !== false) {
|
|
|
166 |
$this->messages[$locale] = $data;
|
|
|
167 |
$this->addResource('array', $this->messages[$locale], $locale);
|
|
|
168 |
|
|
|
169 |
return true;
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
return false;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Returns the list of files matching a given locale prefix (or all if empty).
|
|
|
178 |
*
|
|
|
179 |
* @param string $prefix prefix required to filter result
|
|
|
180 |
*
|
|
|
181 |
* @return array
|
|
|
182 |
*/
|
|
|
183 |
public function getLocalesFiles($prefix = '')
|
|
|
184 |
{
|
|
|
185 |
$files = [];
|
|
|
186 |
|
|
|
187 |
foreach ($this->getDirectories() as $directory) {
|
|
|
188 |
$directory = rtrim($directory, '\\/');
|
|
|
189 |
|
|
|
190 |
foreach (glob("$directory/$prefix*.php") as $file) {
|
|
|
191 |
$files[] = $file;
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
return array_unique($files);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Returns the list of internally available locales and already loaded custom locales.
|
|
|
200 |
* (It will ignore custom translator dynamic loading.)
|
|
|
201 |
*
|
|
|
202 |
* @param string $prefix prefix required to filter result
|
|
|
203 |
*
|
|
|
204 |
* @return array
|
|
|
205 |
*/
|
|
|
206 |
public function getAvailableLocales($prefix = '')
|
|
|
207 |
{
|
|
|
208 |
$locales = [];
|
|
|
209 |
foreach ($this->getLocalesFiles($prefix) as $file) {
|
|
|
210 |
$locales[] = substr($file, strrpos($file, '/') + 1, -4);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
return array_unique(array_merge($locales, array_keys($this->messages)));
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
protected function translate(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
|
|
|
217 |
{
|
|
|
218 |
if ($domain === null) {
|
|
|
219 |
$domain = 'messages';
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$catalogue = $this->getCatalogue($locale);
|
|
|
223 |
$format = $this instanceof TranslatorStrongTypeInterface
|
|
|
224 |
? $this->getFromCatalogue($catalogue, (string) $id, $domain)
|
|
|
225 |
: $this->getCatalogue($locale)->get((string) $id, $domain); // @codeCoverageIgnore
|
|
|
226 |
|
|
|
227 |
if ($format instanceof Closure) {
|
|
|
228 |
// @codeCoverageIgnoreStart
|
|
|
229 |
try {
|
|
|
230 |
$count = (new ReflectionFunction($format))->getNumberOfRequiredParameters();
|
|
|
231 |
} catch (ReflectionException $exception) {
|
|
|
232 |
$count = 0;
|
|
|
233 |
}
|
|
|
234 |
// @codeCoverageIgnoreEnd
|
|
|
235 |
|
|
|
236 |
return $format(
|
|
|
237 |
...array_values($parameters),
|
|
|
238 |
...array_fill(0, max(0, $count - \count($parameters)), null)
|
|
|
239 |
);
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
return parent::trans($id, $parameters, $domain, $locale);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Init messages language from matching file in Lang directory.
|
|
|
247 |
*
|
|
|
248 |
* @param string $locale
|
|
|
249 |
*
|
|
|
250 |
* @return bool
|
|
|
251 |
*/
|
|
|
252 |
protected function loadMessagesFromFile($locale)
|
|
|
253 |
{
|
|
|
254 |
return isset($this->messages[$locale]) || $this->resetMessages($locale);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Set messages of a locale and take file first if present.
|
|
|
259 |
*
|
|
|
260 |
* @param string $locale
|
|
|
261 |
* @param array $messages
|
|
|
262 |
*
|
|
|
263 |
* @return $this
|
|
|
264 |
*/
|
|
|
265 |
public function setMessages($locale, $messages)
|
|
|
266 |
{
|
|
|
267 |
$this->loadMessagesFromFile($locale);
|
|
|
268 |
$this->addResource('array', $messages, $locale);
|
|
|
269 |
$this->messages[$locale] = array_merge(
|
|
|
270 |
$this->messages[$locale] ?? [],
|
|
|
271 |
$messages
|
|
|
272 |
);
|
|
|
273 |
|
|
|
274 |
return $this;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Set messages of the current locale and take file first if present.
|
|
|
279 |
*
|
|
|
280 |
* @param array $messages
|
|
|
281 |
*
|
|
|
282 |
* @return $this
|
|
|
283 |
*/
|
|
|
284 |
public function setTranslations($messages)
|
|
|
285 |
{
|
|
|
286 |
return $this->setMessages($this->getLocale(), $messages);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Get messages of a locale, if none given, return all the
|
|
|
291 |
* languages.
|
|
|
292 |
*
|
|
|
293 |
* @param string|null $locale
|
|
|
294 |
*
|
|
|
295 |
* @return array
|
|
|
296 |
*/
|
|
|
297 |
public function getMessages($locale = null)
|
|
|
298 |
{
|
|
|
299 |
return $locale === null ? $this->messages : $this->messages[$locale];
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Set the current translator locale and indicate if the source locale file exists
|
|
|
304 |
*
|
|
|
305 |
* @param string $locale locale ex. en
|
|
|
306 |
*
|
|
|
307 |
* @return bool
|
|
|
308 |
*/
|
|
|
309 |
public function setLocale($locale)
|
|
|
310 |
{
|
|
|
311 |
$locale = preg_replace_callback('/[-_]([a-z]{2,}|\d{2,})/', function ($matches) {
|
|
|
312 |
// _2-letters or YUE is a region, _3+-letters is a variant
|
|
|
313 |
$upper = strtoupper($matches[1]);
|
|
|
314 |
|
|
|
315 |
if ($upper === 'YUE' || $upper === 'ISO' || \strlen($upper) < 3) {
|
|
|
316 |
return "_$upper";
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
return '_'.ucfirst($matches[1]);
|
|
|
320 |
}, strtolower($locale));
|
|
|
321 |
|
|
|
322 |
$previousLocale = $this->getLocale();
|
|
|
323 |
|
|
|
324 |
if ($previousLocale === $locale && isset($this->messages[$locale])) {
|
|
|
325 |
return true;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
unset(static::$singletons[$previousLocale]);
|
|
|
329 |
|
|
|
330 |
if ($locale === 'auto') {
|
|
|
331 |
$completeLocale = setlocale(LC_TIME, '0');
|
|
|
332 |
$locale = preg_replace('/^([^_.-]+).*$/', '$1', $completeLocale);
|
|
|
333 |
$locales = $this->getAvailableLocales($locale);
|
|
|
334 |
|
|
|
335 |
$completeLocaleChunks = preg_split('/[_.-]+/', $completeLocale);
|
|
|
336 |
|
|
|
337 |
$getScore = function ($language) use ($completeLocaleChunks) {
|
|
|
338 |
return self::compareChunkLists($completeLocaleChunks, preg_split('/[_.-]+/', $language));
|
|
|
339 |
};
|
|
|
340 |
|
|
|
341 |
usort($locales, function ($first, $second) use ($getScore) {
|
|
|
342 |
return $getScore($second) <=> $getScore($first);
|
|
|
343 |
});
|
|
|
344 |
|
|
|
345 |
$locale = $locales[0];
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
if (isset($this->aliases[$locale])) {
|
|
|
349 |
$locale = $this->aliases[$locale];
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
// If subtag (ex: en_CA) first load the macro (ex: en) to have a fallback
|
|
|
353 |
if (str_contains($locale, '_') &&
|
|
|
354 |
$this->loadMessagesFromFile($macroLocale = preg_replace('/^([^_]+).*$/', '$1', $locale))
|
|
|
355 |
) {
|
|
|
356 |
parent::setLocale($macroLocale);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
if (!$this->loadMessagesFromFile($locale) && !$this->initializing) {
|
|
|
360 |
return false;
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
parent::setLocale($locale);
|
|
|
364 |
|
|
|
365 |
return true;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
/**
|
|
|
369 |
* Show locale on var_dump().
|
|
|
370 |
*
|
|
|
371 |
* @return array
|
|
|
372 |
*/
|
|
|
373 |
public function __debugInfo()
|
|
|
374 |
{
|
|
|
375 |
return [
|
|
|
376 |
'locale' => $this->getLocale(),
|
|
|
377 |
];
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
private static function compareChunkLists($referenceChunks, $chunks)
|
|
|
381 |
{
|
|
|
382 |
$score = 0;
|
|
|
383 |
|
|
|
384 |
foreach ($referenceChunks as $index => $chunk) {
|
|
|
385 |
if (!isset($chunks[$index])) {
|
|
|
386 |
$score++;
|
|
|
387 |
|
|
|
388 |
continue;
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
if (strtolower($chunks[$index]) === strtolower($chunk)) {
|
|
|
392 |
$score += 10;
|
|
|
393 |
}
|
|
|
394 |
}
|
|
|
395 |
|
|
|
396 |
return $score;
|
|
|
397 |
}
|
|
|
398 |
}
|