TranslatorTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setTranslator() 0 3 1
A getTranslator() 0 7 2
A trans() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Translation;
15
16
use Symfony\Contracts\Translation\TranslatorInterface;
17
18
trait TranslatorTrait
19
{
20
    private TranslatorInterface $translator;
21
22
    public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
23
    {
24
        return $this->getTranslator()->trans($id, $parameters, $domain, $locale);
25
    }
26
27
    public function getTranslator(): TranslatorInterface
28
    {
29
        if (null === $this->translator) {
30
            throw new \ErrorException('Translator must be set in __TRAIT__ before it can be used.');
31
        }
32
33
        return $this->translator;
34
    }
35
36
    public function setTranslator(TranslatorInterface $translator)
37
    {
38
        $this->translator = $translator;
39
    }
40
}
41