blob: d35308a66c27071de138b99d40d0d039f0485b18 [file] [log] [blame]
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001<?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
12namespace Carbon;
13
14use Symfony\Component\Translation\MessageCatalogueInterface;
15
16if (!class_exists(LazyTranslator::class, false)) {
17 class LazyTranslator extends AbstractTranslator implements TranslatorStrongTypeInterface
18 {
19 public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
20 {
21 return $this->translate($id, $parameters, $domain, $locale);
22 }
23
24 public function getFromCatalogue(MessageCatalogueInterface $catalogue, string $id, string $domain = 'messages')
25 {
26 $messages = $this->getPrivateProperty($catalogue, 'messages');
27
28 if (isset($messages[$domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX][$id])) {
29 return $messages[$domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX][$id];
30 }
31
32 if (isset($messages[$domain][$id])) {
33 return $messages[$domain][$id];
34 }
35
36 $fallbackCatalogue = $this->getPrivateProperty($catalogue, 'fallbackCatalogue');
37
38 if ($fallbackCatalogue !== null) {
39 return $this->getFromCatalogue($fallbackCatalogue, $id, $domain);
40 }
41
42 return $id;
43 }
44
45 private function getPrivateProperty($instance, string $field)
46 {
47 return (function (string $field) {
48 return $this->$field;
49 })->call($instance, $field);
50 }
51 }
52}