Passed
Push — master ( 994262...329e97 )
by
unknown
01:27
created

Translator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 86
ccs 34
cts 36
cp 0.9444
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A addCategorySource() 0 3 1
A withLocale() 0 5 1
A getLocale() 0 3 1
A translate() 0 39 6
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 $defaultLocale;
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 $defaultLocale,
25
        EventDispatcherInterface $eventDispatcher,
26
        string $fallbackLocale = null
27
    ) {
28 17
        $this->defaultCategory = $defaultCategory->getName();
29 17
        $this->eventDispatcher = $eventDispatcher;
30 17
        $this->defaultLocale = $defaultLocale;
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 withLocale(string $locale): self
47
    {
48 2
        $new = clone $this;
49 2
        $new->defaultLocale = $locale;
50 2
        return $new;
51
    }
52
53
    public function getLocale(): string
54
    {
55
        return $this->defaultLocale;
56
    }
57
58 17
    public function translate(
59
        string $id,
60
        array $parameters = [],
61
        string $category = null,
62
        string $locale = null
63
    ): string {
64 17
        $locale = $locale ?? $this->defaultLocale;
65
66 17
        $category = $category ?? $this->defaultCategory;
67
68 17
        if (empty($this->categories[$category])) {
69 1
            $this->eventDispatcher->dispatch(new MissingTranslationCategoryEvent($category));
70 1
            return $id;
71
        }
72
73 16
        $sourceCategory = $this->categories[$category];
74 16
        $message = $sourceCategory->getMessage($id, $locale, $parameters);
75
76 16
        if ($message === null) {
77 8
            $this->eventDispatcher->dispatch(new MissingTranslationEvent($sourceCategory->getName(), $locale, $id));
78
79 8
            $localeObject = new Locale($locale);
80 8
            $fallback = $localeObject->fallbackLocale();
81
82 8
            if ($fallback->asString() !== $localeObject->asString()) {
83 4
                return $this->translate($id, $parameters, $category, $fallback->asString());
84
            }
85
86 5
            if (!empty($this->fallbackLocale)) {
87 3
                $fallbackLocaleObject = (new Locale($this->fallbackLocale))->fallbackLocale();
88 3
                if ($fallbackLocaleObject->asString() !== $localeObject->asString()) {
89 3
                    return $this->translate($id, $parameters, $category, $fallbackLocaleObject->asString());
90
                }
91
            }
92
93 2
            $message = $id;
94
        }
95
96 16
        return $sourceCategory->format($message, $parameters, $locale);
97
    }
98
}
99