Passed
Push — master ( 329e97...901043 )
by
unknown
01:18
created

Translator::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Translator;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Yiisoft\Translator\Event\MissingTranslationCategoryEvent;
9
use Yiisoft\Translator\Event\MissingTranslationEvent;
10
11
class Translator implements TranslatorInterface
12
{
13
    private string $defaultCategory;
14
    private string $locale;
15
    private EventDispatcherInterface $eventDispatcher;
16
    private ?string $fallbackLocale;
17
    /**
18
     * @var Category[]
19
     */
20
    private array $categories = [];
21
22 17
    public function __construct(
23
        Category $defaultCategory,
24
        string $locale,
25
        EventDispatcherInterface $eventDispatcher,
26
        string $fallbackLocale = null
27
    ) {
28 17
        $this->defaultCategory = $defaultCategory->getName();
29 17
        $this->eventDispatcher = $eventDispatcher;
30 17
        $this->locale = $locale;
31 17
        $this->fallbackLocale = $fallbackLocale;
32
33 17
        $this->addCategorySource($defaultCategory);
34 17
    }
35
36 17
    public function addCategorySource(Category $category): void
37
    {
38 17
        $this->categories[$category->getName()] = $category;
39 17
    }
40
41
    /**
42
     * Sets the current application locale.
43
     *
44
     * @param string $locale
45
     */
46 2
    public function setLocale(string $locale): void
47
    {
48 2
        $this->locale = $locale;
49 2
    }
50
51
    public function getLocale(): string
52
    {
53
        return $this->locale;
54
    }
55
56 17
    public function translate(
57
        string $id,
58
        array $parameters = [],
59
        string $category = null,
60
        string $locale = null
61
    ): string {
62 17
        $locale = $locale ?? $this->locale;
63
64 17
        $category = $category ?? $this->defaultCategory;
65
66 17
        if (empty($this->categories[$category])) {
67 1
            $this->eventDispatcher->dispatch(new MissingTranslationCategoryEvent($category));
68 1
            return $id;
69
        }
70
71 16
        $sourceCategory = $this->categories[$category];
72 16
        $message = $sourceCategory->getMessage($id, $locale, $parameters);
73
74 16
        if ($message === null) {
75 8
            $this->eventDispatcher->dispatch(new MissingTranslationEvent($sourceCategory->getName(), $locale, $id));
76
77 8
            $localeObject = new Locale($locale);
78 8
            $fallback = $localeObject->fallbackLocale();
79
80 8
            if ($fallback->asString() !== $localeObject->asString()) {
81 4
                return $this->translate($id, $parameters, $category, $fallback->asString());
82
            }
83
84 5
            if (!empty($this->fallbackLocale)) {
85 3
                $fallbackLocaleObject = (new Locale($this->fallbackLocale))->fallbackLocale();
86 3
                if ($fallbackLocaleObject->asString() !== $localeObject->asString()) {
87 3
                    return $this->translate($id, $parameters, $category, $fallbackLocaleObject->asString());
88
                }
89
            }
90
91 2
            $message = $id;
92
        }
93
94 16
        return $sourceCategory->format($message, $parameters, $locale);
95
    }
96
}
97