Passed
Pull Request — master (#19)
by Alexander
01:17
created

Translator::setDefaultLocale()   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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\I18n\Translator;
6
7
use Yiisoft\I18n\MessageFormatterInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Yiisoft\I18n\Event\MissingTranslationEvent;
10
use Yiisoft\I18n\Locale;
11
use Yiisoft\I18n\MessageReaderInterface;
12
use Yiisoft\I18n\TranslatorInterface;
13
14
class Translator implements TranslatorInterface
15
{
16
    private EventDispatcherInterface $eventDispatcher;
17
    private MessageReaderInterface $messageReader;
18
    private ?MessageFormatterInterface $messageFormatter;
19
    private array $messages = [];
20
    private ?string $locale = null;
21
    private ?string $defaultLocale = null;
22
23 5
    public function __construct(
24
        EventDispatcherInterface $eventDispatcher,
25
        MessageReaderInterface $messageReader,
26
        MessageFormatterInterface $messageFormatter = null
27
    ) {
28 5
        $this->messageReader = $messageReader;
29 5
        $this->eventDispatcher = $eventDispatcher;
30 5
        $this->messageFormatter = $messageFormatter;
31
    }
32
33
    /**
34
     * Sets the current locale.
35
     *
36
     * @param string $locale The locale
37
     */
38
    public function setLocale(string $locale): void
39
    {
40
        $this->locale = $locale;
41
    }
42
43
    /**
44
     * Returns the current locale.
45
     *
46
     * @return string The locale
47
     */
48 3
    public function getLocale(): string
49
    {
50 3
        return $this->locale ?? $this->getDefaultLocale();
51
    }
52
53
    /**
54
     * @param string $locale
55
     */
56 2
    public function setDefaultLocale(string $locale): void
57
    {
58 2
        $this->defaultLocale = $locale;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 5
    public function translate(
65
        ?string $message,
66
        array $parameters = [],
67
        string $category = null,
68
        string $localeString = null
69
    ): ?string {
70 5
        if ($localeString === null) {
71 3
            $localeString = $this->getLocale();
72
        }
73
74 5
        if ($category === null) {
75 3
            $category = $this->getDefaultCategory();
76
        }
77
78 5
        $messages = $this->getMessages($category, $localeString);
79
80 5
        if (!array_key_exists($message, $messages)) {
81 2
            $missingTranslation = new MissingTranslationEvent($category, $localeString, $message);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type null; however, parameter $message of Yiisoft\I18n\Event\Missi...ionEvent::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
            $missingTranslation = new MissingTranslationEvent($category, $localeString, /** @scrutinizer ignore-type */ $message);
Loading history...
82 2
            $this->eventDispatcher->dispatch($missingTranslation);
83
84 2
            $locale = new Locale($localeString);
85 2
            $fallback = $locale->fallbackLocale();
86
87 2
            if ($fallback->asString() !== $locale->asString()) {
88
                return $this->translate($message, $parameters, $category, $fallback->asString());
89
            }
90
91 2
            $defaultFallback = (new Locale($this->getDefaultLocale()))->fallbackLocale();
92
93 2
            if ($defaultFallback->asString() !== $fallback->asString()) {
94 2
                return $this->translate($message, $parameters, $category, $this->getDefaultLocale());
95
            }
96
97 1
            $messages[$message] = $message;
98
        }
99
100 5
        if ($this->messageFormatter === null) {
101 4
            return $messages[$message];
102
        }
103
104 1
        return $this->messageFormatter->format($messages[$message], $parameters, $localeString);
105
    }
106
107 5
    private function getMessages(string $category, string $language): array
108
    {
109 5
        $key = $language . '/' . $category;
110
111 5
        if (!array_key_exists($key, $this->messages)) {
112 5
            $this->messages[$key] = $this->messageReader->all($key);
113
        }
114
115 5
        return $this->messages[$key];
116
    }
117
118 3
    protected function getDefaultCategory(): string
119
    {
120 3
        return 'default';
121
    }
122
123 5
    protected function getDefaultLocale(): string
124
    {
125 5
        return $this->defaultLocale ?? \Locale::getDefault();
126
    }
127
}
128