| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\CarbonInterface; |
| 17 |
use Carbon\Exceptions\InvalidTypeException; |
| 18 |
use Carbon\Exceptions\NotLocaleAwareException; |
| 19 |
use Carbon\Language; |
| 20 |
use Carbon\Translator; |
| 21 |
use Carbon\TranslatorStrongTypeInterface; |
| 22 |
use Closure; |
| 23 |
use Symfony\Component\Translation\TranslatorBagInterface; |
| 24 |
use Symfony\Contracts\Translation\LocaleAwareInterface; |
| 25 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
trait Localization |
| 33 |
{ |
| 34 |
use StaticLocalization; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected ?TranslatorInterface $localTranslator = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function hasLocalTranslator(): bool |
| 45 |
{ |
| 46 |
return isset($this->localTranslator); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function getLocalTranslator(): TranslatorInterface |
| 53 |
{ |
| 54 |
return $this->localTranslator ?? $this->transmitFactory(static fn () => static::getTranslator()); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function setLocalTranslator(TranslatorInterface $translator): self |
| 61 |
{ |
| 62 |
$this->localTranslator = $translator; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static function getTranslationMessageWith($translator, string $key, ?string $locale = null, ?string $default = null) |
| 78 |
{ |
| 79 |
if (!($translator instanceof TranslatorBagInterface && $translator instanceof TranslatorInterface)) { |
| 80 |
throw new InvalidTypeException( |
| 81 |
'Translator does not implement '.TranslatorInterface::class.' and '.TranslatorBagInterface::class.'. '. |
| 82 |
(\is_object($translator) ? \get_class($translator) : \gettype($translator)).' has been given.', |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
if (!$locale && $translator instanceof LocaleAwareInterface) { |
| 87 |
$locale = $translator->getLocale(); |
| 88 |
} |
| 89 |
|
| 90 |
$result = self::getFromCatalogue($translator, $translator->getCatalogue($locale), $key); |
| 91 |
|
| 92 |
return $result === $key ? $default : $result; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getTranslationMessage(string $key, ?string $locale = null, ?string $default = null, $translator = null) |
| 106 |
{ |
| 107 |
return static::getTranslationMessageWith($translator ?? $this->getLocalTranslator(), $key, $locale, $default); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static function translateWith(TranslatorInterface $translator, string $key, array $parameters = [], $number = null): string |
| 121 |
{ |
| 122 |
$message = static::getTranslationMessageWith($translator, $key, null, $key); |
| 123 |
if ($message instanceof Closure) { |
| 124 |
return (string) $message(...array_values($parameters)); |
| 125 |
} |
| 126 |
|
| 127 |
if ($number !== null) { |
| 128 |
$parameters['%count%'] = $number; |
| 129 |
} |
| 130 |
if (isset($parameters['%count%'])) { |
| 131 |
$parameters[':count'] = $parameters['%count%']; |
| 132 |
} |
| 133 |
|
| 134 |
return (string) $translator->trans($key, $parameters); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function translate( |
| 149 |
string $key, |
| 150 |
array $parameters = [], |
| 151 |
string|int|float|null $number = null, |
| 152 |
?TranslatorInterface $translator = null, |
| 153 |
bool $altNumbers = false, |
| 154 |
): string { |
| 155 |
$translation = static::translateWith($translator ?? $this->getLocalTranslator(), $key, $parameters, $number); |
| 156 |
|
| 157 |
if ($number !== null && $altNumbers) { |
| 158 |
return str_replace((string) $number, $this->translateNumber((int) $number), $translation); |
| 159 |
} |
| 160 |
|
| 161 |
return $translation; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function translateNumber(int $number): string |
| 172 |
{ |
| 173 |
$translateKey = "alt_numbers.$number"; |
| 174 |
$symbol = $this->translate($translateKey); |
| 175 |
|
| 176 |
if ($symbol !== $translateKey) { |
| 177 |
return $symbol; |
| 178 |
} |
| 179 |
|
| 180 |
if ($number > 99 && $this->translate('alt_numbers.99') !== 'alt_numbers.99') { |
| 181 |
$start = ''; |
| 182 |
foreach ([10000, 1000, 100] as $exp) { |
| 183 |
$key = "alt_numbers_pow.$exp"; |
| 184 |
if ($number >= $exp && $number < $exp * 10 && ($pow = $this->translate($key)) !== $key) { |
| 185 |
$unit = floor($number / $exp); |
| 186 |
$number -= $unit * $exp; |
| 187 |
$start .= ($unit > 1 ? $this->translate("alt_numbers.$unit") : '').$pow; |
| 188 |
} |
| 189 |
} |
| 190 |
$result = ''; |
| 191 |
while ($number) { |
| 192 |
$chunk = $number % 100; |
| 193 |
$result = $this->translate("alt_numbers.$chunk").$result; |
| 194 |
$number = floor($number / 100); |
| 195 |
} |
| 196 |
|
| 197 |
return "$start$result"; |
| 198 |
} |
| 199 |
|
| 200 |
if ($number > 9 && $this->translate('alt_numbers.9') !== 'alt_numbers.9') { |
| 201 |
$result = ''; |
| 202 |
while ($number) { |
| 203 |
$chunk = $number % 10; |
| 204 |
$result = $this->translate("alt_numbers.$chunk").$result; |
| 205 |
$number = floor($number / 10); |
| 206 |
} |
| 207 |
|
| 208 |
return $result; |
| 209 |
} |
| 210 |
|
| 211 |
return (string) $number; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static function translateTimeString( |
| 231 |
string $timeString, |
| 232 |
?string $from = null, |
| 233 |
?string $to = null, |
| 234 |
int $mode = CarbonInterface::TRANSLATE_ALL, |
| 235 |
): string { |
| 236 |
|
| 237 |
$from = $from ?: static::getLocale(); |
| 238 |
$to = $to ?: CarbonInterface::DEFAULT_LOCALE; |
| 239 |
|
| 240 |
if ($from === $to) { |
| 241 |
return $timeString; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
$timeString = strtr($timeString, ['’' => "'"]); |
| 246 |
|
| 247 |
$fromTranslations = []; |
| 248 |
$toTranslations = []; |
| 249 |
|
| 250 |
foreach (['from', 'to'] as $key) { |
| 251 |
$language = $$key; |
| 252 |
$translator = Translator::get($language); |
| 253 |
$translations = $translator->getMessages(); |
| 254 |
|
| 255 |
if (!isset($translations[$language])) { |
| 256 |
return $timeString; |
| 257 |
} |
| 258 |
|
| 259 |
$translationKey = $key.'Translations'; |
| 260 |
$messages = $translations[$language]; |
| 261 |
$months = $messages['months'] ?? []; |
| 262 |
$weekdays = $messages['weekdays'] ?? []; |
| 263 |
$meridiem = $messages['meridiem'] ?? ['AM', 'PM']; |
| 264 |
|
| 265 |
if (isset($messages['ordinal_words'])) { |
| 266 |
$timeString = self::replaceOrdinalWords( |
| 267 |
$timeString, |
| 268 |
$key === 'from' ? array_flip($messages['ordinal_words']) : $messages['ordinal_words'] |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
if ($key === 'from') { |
| 273 |
foreach (['months', 'weekdays'] as $variable) { |
| 274 |
$list = $messages[$variable.'_standalone'] ?? null; |
| 275 |
|
| 276 |
if ($list) { |
| 277 |
foreach ($$variable as $index => &$name) { |
| 278 |
$name .= '|'.$list[$index]; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
$$translationKey = array_merge( |
| 285 |
$mode & CarbonInterface::TRANSLATE_MONTHS ? self::getTranslationArray($months, static::MONTHS_PER_YEAR, $timeString) : [], |
| 286 |
$mode & CarbonInterface::TRANSLATE_MONTHS ? self::getTranslationArray($messages['months_short'] ?? [], static::MONTHS_PER_YEAR, $timeString) : [], |
| 287 |
$mode & CarbonInterface::TRANSLATE_DAYS ? self::getTranslationArray($weekdays, static::DAYS_PER_WEEK, $timeString) : [], |
| 288 |
$mode & CarbonInterface::TRANSLATE_DAYS ? self::getTranslationArray($messages['weekdays_short'] ?? [], static::DAYS_PER_WEEK, $timeString) : [], |
| 289 |
$mode & CarbonInterface::TRANSLATE_DIFF ? self::translateWordsByKeys([ |
| 290 |
'diff_now', |
| 291 |
'diff_today', |
| 292 |
'diff_yesterday', |
| 293 |
'diff_tomorrow', |
| 294 |
'diff_before_yesterday', |
| 295 |
'diff_after_tomorrow', |
| 296 |
], $messages, $key) : [], |
| 297 |
$mode & CarbonInterface::TRANSLATE_UNITS ? self::translateWordsByKeys([ |
| 298 |
'year', |
| 299 |
'month', |
| 300 |
'week', |
| 301 |
'day', |
| 302 |
'hour', |
| 303 |
'minute', |
| 304 |
'second', |
| 305 |
], $messages, $key) : [], |
| 306 |
$mode & CarbonInterface::TRANSLATE_MERIDIEM ? array_map(function ($hour) use ($meridiem) { |
| 307 |
if (\is_array($meridiem)) { |
| 308 |
return $meridiem[$hour < static::HOURS_PER_DAY / 2 ? 0 : 1]; |
| 309 |
} |
| 310 |
|
| 311 |
return $meridiem($hour, 0, false); |
| 312 |
}, range(0, 23)) : [], |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
return substr(preg_replace_callback('/(?<=[\d\s+.\/,_-])('.implode('|', $fromTranslations).')(?=[\d\s+.\/,_-])/iu', function ($match) use ($fromTranslations, $toTranslations) { |
| 317 |
[$chunk] = $match; |
| 318 |
|
| 319 |
foreach ($fromTranslations as $index => $word) { |
| 320 |
if (preg_match("/^$word\$/iu", $chunk)) { |
| 321 |
return $toTranslations[$index] ?? ''; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return $chunk; |
| 326 |
}, " $timeString "), 1, -1); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function translateTimeStringTo(string $timeString, ?string $to = null): string |
| 338 |
{ |
| 339 |
return static::translateTimeString($timeString, $this->getTranslatorLocale(), $to); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function locale(?string $locale = null, string ...$fallbackLocales): static|string |
| 351 |
{ |
| 352 |
if ($locale === null) { |
| 353 |
return $this->getTranslatorLocale(); |
| 354 |
} |
| 355 |
|
| 356 |
if (!$this->localTranslator || $this->getTranslatorLocale($this->localTranslator) !== $locale) { |
| 357 |
$translator = Translator::get($locale); |
| 358 |
|
| 359 |
if (!empty($fallbackLocales)) { |
| 360 |
$translator->setFallbackLocales($fallbackLocales); |
| 361 |
|
| 362 |
foreach ($fallbackLocales as $fallbackLocale) { |
| 363 |
$messages = Translator::get($fallbackLocale)->getMessages(); |
| 364 |
|
| 365 |
if (isset($messages[$fallbackLocale])) { |
| 366 |
$translator->setMessages($fallbackLocale, $messages[$fallbackLocale]); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
$this->localTranslator = $translator; |
| 372 |
} |
| 373 |
|
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public static function getLocale(): string |
| 383 |
{ |
| 384 |
return static::getLocaleAwareTranslator()->getLocale(); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public static function setLocale(string $locale): void |
| 394 |
{ |
| 395 |
static::getLocaleAwareTranslator()->setLocale($locale); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public static function setFallbackLocale(string $locale): void |
| 406 |
{ |
| 407 |
$translator = static::getTranslator(); |
| 408 |
|
| 409 |
if (method_exists($translator, 'setFallbackLocales')) { |
| 410 |
$translator->setFallbackLocales([$locale]); |
| 411 |
|
| 412 |
if ($translator instanceof Translator) { |
| 413 |
$preferredLocale = $translator->getLocale(); |
| 414 |
$fallbackMessages = []; |
| 415 |
$preferredMessages = $translator->getMessages($preferredLocale); |
| 416 |
|
| 417 |
foreach (Translator::get($locale)->getMessages()[$locale] ?? [] as $key => $value) { |
| 418 |
if ( |
| 419 |
preg_match('/^(?:a_)?(.+)_(?:standalone|ago|from_now|before|after|short|min)$/', $key, $match) |
| 420 |
&& isset($preferredMessages[$match[1]]) |
| 421 |
) { |
| 422 |
continue; |
| 423 |
} |
| 424 |
|
| 425 |
$fallbackMessages[$key] = $value; |
| 426 |
} |
| 427 |
|
| 428 |
$translator->setMessages($preferredLocale, array_replace_recursive( |
| 429 |
$translator->getMessages()[$locale] ?? [], |
| 430 |
$fallbackMessages, |
| 431 |
$preferredMessages, |
| 432 |
)); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public static function getFallbackLocale(): ?string |
| 443 |
{ |
| 444 |
$translator = static::getTranslator(); |
| 445 |
|
| 446 |
if (method_exists($translator, 'getFallbackLocales')) { |
| 447 |
return $translator->getFallbackLocales()[0] ?? null; |
| 448 |
} |
| 449 |
|
| 450 |
return null; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public static function executeWithLocale(string $locale, callable $func): mixed |
| 463 |
{ |
| 464 |
$currentLocale = static::getLocale(); |
| 465 |
static::setLocale($locale); |
| 466 |
$newLocale = static::getLocale(); |
| 467 |
$result = $func( |
| 468 |
$newLocale === 'en' && strtolower(substr((string) $locale, 0, 2)) !== 'en' |
| 469 |
? false |
| 470 |
: $newLocale, |
| 471 |
static::getTranslator(), |
| 472 |
); |
| 473 |
static::setLocale($currentLocale); |
| 474 |
|
| 475 |
return $result; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public static function localeHasShortUnits(string $locale): bool |
| 487 |
{ |
| 488 |
return static::executeWithLocale($locale, function ($newLocale, TranslatorInterface $translator) { |
| 489 |
return ($newLocale && (($y = static::translateWith($translator, 'y')) !== 'y' && $y !== static::translateWith($translator, 'year'))) || ( |
| 490 |
($y = static::translateWith($translator, 'd')) !== 'd' && |
| 491 |
$y !== static::translateWith($translator, 'day') |
| 492 |
) || ( |
| 493 |
($y = static::translateWith($translator, 'h')) !== 'h' && |
| 494 |
$y !== static::translateWith($translator, 'hour') |
| 495 |
); |
| 496 |
}); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
public static function localeHasDiffSyntax(string $locale): bool |
| 508 |
{ |
| 509 |
return static::executeWithLocale($locale, function ($newLocale, TranslatorInterface $translator) { |
| 510 |
if (!$newLocale) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
foreach (['ago', 'from_now', 'before', 'after'] as $key) { |
| 515 |
if ($translator instanceof TranslatorBagInterface && |
| 516 |
self::getFromCatalogue($translator, $translator->getCatalogue($newLocale), $key) instanceof Closure |
| 517 |
) { |
| 518 |
continue; |
| 519 |
} |
| 520 |
|
| 521 |
if ($translator->trans($key) === $key) { |
| 522 |
return false; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
return true; |
| 527 |
}); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public static function localeHasDiffOneDayWords(string $locale): bool |
| 539 |
{ |
| 540 |
return static::executeWithLocale($locale, function ($newLocale, TranslatorInterface $translator) { |
| 541 |
return $newLocale && |
| 542 |
$translator->trans('diff_now') !== 'diff_now' && |
| 543 |
$translator->trans('diff_yesterday') !== 'diff_yesterday' && |
| 544 |
$translator->trans('diff_tomorrow') !== 'diff_tomorrow'; |
| 545 |
}); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
public static function localeHasDiffTwoDayWords(string $locale): bool |
| 557 |
{ |
| 558 |
return static::executeWithLocale($locale, function ($newLocale, TranslatorInterface $translator) { |
| 559 |
return $newLocale && |
| 560 |
$translator->trans('diff_before_yesterday') !== 'diff_before_yesterday' && |
| 561 |
$translator->trans('diff_after_tomorrow') !== 'diff_after_tomorrow'; |
| 562 |
}); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
public static function localeHasPeriodSyntax($locale) |
| 574 |
{ |
| 575 |
return static::executeWithLocale($locale, function ($newLocale, TranslatorInterface $translator) { |
| 576 |
return $newLocale && |
| 577 |
$translator->trans('period_recurrences') !== 'period_recurrences' && |
| 578 |
$translator->trans('period_interval') !== 'period_interval' && |
| 579 |
$translator->trans('period_start_date') !== 'period_start_date' && |
| 580 |
$translator->trans('period_end_date') !== 'period_end_date'; |
| 581 |
}); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public static function getAvailableLocales(): array |
| 591 |
{ |
| 592 |
$translator = static::getLocaleAwareTranslator(); |
| 593 |
|
| 594 |
return $translator instanceof Translator |
| 595 |
? $translator->getAvailableLocales() |
| 596 |
: [$translator->getLocale()]; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
public static function getAvailableLocalesInfo(): array |
| 606 |
{ |
| 607 |
$languages = []; |
| 608 |
|
| 609 |
foreach (static::getAvailableLocales() as $id) { |
| 610 |
$languages[$id] = new Language($id); |
| 611 |
} |
| 612 |
|
| 613 |
return $languages; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
protected function getTranslatorLocale($translator = null): ?string |
| 623 |
{ |
| 624 |
if (\func_num_args() === 0) { |
| 625 |
$translator = $this->getLocalTranslator(); |
| 626 |
} |
| 627 |
|
| 628 |
$translator = static::getLocaleAwareTranslator($translator); |
| 629 |
|
| 630 |
return $translator?->getLocale(); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
protected static function getLocaleAwareTranslator($translator = null) |
| 641 |
{ |
| 642 |
if (\func_num_args() === 0) { |
| 643 |
$translator = static::getTranslator(); |
| 644 |
} |
| 645 |
|
| 646 |
if ($translator && !($translator instanceof LocaleAwareInterface || method_exists($translator, 'getLocale'))) { |
| 647 |
throw new NotLocaleAwareException($translator); |
| 648 |
} |
| 649 |
|
| 650 |
return $translator; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
private static function getFromCatalogue($translator, $catalogue, string $id, string $domain = 'messages') |
| 660 |
{ |
| 661 |
return $translator instanceof TranslatorStrongTypeInterface |
| 662 |
? $translator->getFromCatalogue($catalogue, $id, $domain) |
| 663 |
: $catalogue->get($id, $domain); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
private static function cleanWordFromTranslationString($word) |
| 674 |
{ |
| 675 |
$word = str_replace([':count', '%count', ':time'], '', $word); |
| 676 |
$word = strtr($word, ['’' => "'"]); |
| 677 |
$word = preg_replace( |
| 678 |
'/\{(?:-?\d+(?:\.\d+)?|-?Inf)(?:,(?:-?\d+|-?Inf))?}|[\[\]](?:-?\d+(?:\.\d+)?|-?Inf)(?:,(?:-?\d+|-?Inf))?[\[\]]/', |
| 679 |
'', |
| 680 |
$word, |
| 681 |
); |
| 682 |
|
| 683 |
return trim($word); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
private static function translateWordsByKeys($keys, $messages, $key): array |
| 696 |
{ |
| 697 |
return array_map(function ($wordKey) use ($messages, $key) { |
| 698 |
$message = $key === 'from' && isset($messages[$wordKey.'_regexp']) |
| 699 |
? $messages[$wordKey.'_regexp'] |
| 700 |
: ($messages[$wordKey] ?? null); |
| 701 |
|
| 702 |
if (!$message) { |
| 703 |
return '>>DO NOT REPLACE<<'; |
| 704 |
} |
| 705 |
|
| 706 |
$parts = explode('|', $message); |
| 707 |
|
| 708 |
return $key === 'to' |
| 709 |
? self::cleanWordFromTranslationString(end($parts)) |
| 710 |
: '(?:'.implode('|', array_map(static::cleanWordFromTranslationString(...), $parts)).')'; |
| 711 |
}, $keys); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
private static function getTranslationArray($translation, $length, $timeString): array |
| 724 |
{ |
| 725 |
$filler = '>>DO NOT REPLACE<<'; |
| 726 |
|
| 727 |
if (\is_array($translation)) { |
| 728 |
return array_pad($translation, $length, $filler); |
| 729 |
} |
| 730 |
|
| 731 |
$list = []; |
| 732 |
$date = static::now(); |
| 733 |
|
| 734 |
for ($i = 0; $i < $length; $i++) { |
| 735 |
$list[] = $translation($date, $timeString, $i) ?? $filler; |
| 736 |
} |
| 737 |
|
| 738 |
return $list; |
| 739 |
} |
| 740 |
|
| 741 |
private static function replaceOrdinalWords(string $timeString, array $ordinalWords): string |
| 742 |
{ |
| 743 |
return preg_replace_callback('/(?<![a-z])[a-z]+(?![a-z])/i', function (array $match) use ($ordinalWords) { |
| 744 |
return $ordinalWords[mb_strtolower($match[0])] ?? $match[0]; |
| 745 |
}, $timeString); |
| 746 |
} |
| 747 |
} |