File size: 4,184 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace Mautic\CoreBundle\Translation;

use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * Extended Translator service.
 */
class Translator implements TranslatorInterface, WarmableInterface, TranslatorBagInterface, LocaleAwareInterface
{
    /**
     * @var TranslatorInterface&WarmableInterface&TranslatorBagInterface&LocaleAwareInterface
     */
    private TranslatorInterface $translator;

    public function __construct(TranslatorInterface $translator)
    {
        if (!$translator instanceof WarmableInterface) {
            throw new \InvalidArgumentException('Passed $translator must implement '.WarmableInterface::class);
        }

        if (!$translator instanceof TranslatorBagInterface) {
            throw new \InvalidArgumentException('Passed $translator must implement '.TranslatorBagInterface::class);
        }

        if (!$translator instanceof LocaleAwareInterface) {
            throw new \InvalidArgumentException('Passed $translator must implement '.LocaleAwareInterface::class);
        }

        $this->translator = $translator;
    }

    /**
     * @param array<mixed> $parameters
     */
    public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
    {
        return $this->translator->trans($id, $parameters, $domain, $locale);
    }

    public function warmUp(string $cacheDir)
    {
        $this->translator->warmUp($cacheDir);

        return [];
    }

    public function getCatalogue($locale = null): MessageCatalogueInterface
    {
        return $this->translator->getCatalogue($locale);
    }

    public function getLocale(): string
    {
        return $this->translator->getLocale();
    }

    public function setLocale(string $locale): void
    {
        $this->translator->setLocale($locale);
    }

    /**
     * Check if the specified message ID exists.
     *
     * @param string      $id     The message id (may also be an object that can be cast to string)
     * @param string|null $domain The domain for the message or null to use the default
     * @param string|null $locale The locale or null to use the default
     *
     * @return bool true if the message has a translation, false otherwise
     */
    public function hasId(string $id, ?string $domain = null, ?string $locale = null): bool
    {
        if (null === $domain) {
            $domain = 'messages';
        }

        return $this->getCatalogue($locale)->has($id, $domain);
    }

    /**
     * Checks for $preferred string existence and returns translation if it does.  Otherwise, returns translation for
     * $alternative.
     *
     * @param array<mixed> $parameters
     */
    public function transConditional(string $preferred, string $alternative, array $parameters = [], ?string $domain = null, ?string $locale = null): string
    {
        if ($this->hasId($preferred, $domain, $locale)) {
            return $this->trans($preferred, $parameters, $domain, $locale);
        }

        return $this->trans($alternative, $parameters, $domain, $locale);
    }

    public function getJsLang(): string
    {
        $defaultMessages = $this->translator->getCatalogue('en_US')->all('javascript');
        $messages        = $this->translator->getCatalogue()->all('javascript');

        $oldKeys = [
            'chosenChooseOne'     => $this->translator->trans('mautic.core.form.chooseone'),
            'chosenChooseMore'    => $this->translator->trans('mautic.core.form.choosemultiple'),
            'chosenNoResults'     => $this->translator->trans('mautic.core.form.nomatches'),
            'pleaseWait'          => $this->translator->trans('mautic.core.wait'),
            'popupBlockerMessage' => $this->translator->trans('mautic.core.popupblocked'),
        ];
        $jsLang = array_merge($defaultMessages, $messages, $oldKeys);

        return json_encode($jsLang, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
    }
}