Completed
Push — master ( d5e216...b4d558 )
by Alexander
12:07
created

Translator::translate()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 27
rs 9.4888
cc 5
nc 12
nop 3
1
<?php
2
3
namespace Yiisoft\I18n\Translator;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Yiisoft\I18n\Event\MissingTranslationEvent;
7
use Yiisoft\I18n\Locale;
8
use Yiisoft\I18n\MessageReaderInterface;
9
use Yiisoft\I18n\TranslatorInterface;
10
11
class Translator implements TranslatorInterface
12
{
13
    /**
14
     * @var \Yiisoft\I18n\MessageReaderInterface
15
     */
16
    private $messageReader;
17
    /**
18
     * @var \Psr\EventDispatcher\EventDispatcherInterface
19
     */
20
    private $eventDispatcher;
21
22
    /**
23
     * @var array
24
     */
25
    private $messages = [];
26
27
    public function __construct(
28
        EventDispatcherInterface $eventDispatcher,
29
        MessageReaderInterface $messageReader
30
    ) {
31
        $this->messageReader = $messageReader;
32
        $this->eventDispatcher = $eventDispatcher;
33
    }
34
35
    /**
36
     * Translates a message to the specified language.
37
     * If a translation is not found, a {{@see \Yiisoft\I18n\Event\MissingTranslationEvent} event will be triggered.
38
     *
39
     * @param string $message the message to be translated
40
     * @param string $category the message category
41
     * @param string $localeString the target locale
42
     * @return string|null the translated message or false if translation wasn't found or isn't required
43
     */
44
    public function translate(?string $message, string $category = null, string $localeString = null): ?string
45
    {
46
        if ($localeString === null) {
47
            $localeString = $this->getDefaultLocale();
48
        }
49
50
        if ($category === null) {
51
            $category = $this->getDefaultCategory();
52
        }
53
54
        $messages = $this->getMessages($category, $localeString);
55
56
        if (array_key_exists($message, $messages)) {
57
            return $messages[$message];
58
        }
59
60
        $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

60
        $missingTranslation = new MissingTranslationEvent($category, $localeString, /** @scrutinizer ignore-type */ $message);
Loading history...
61
        $this->eventDispatcher->dispatch($missingTranslation);
62
63
        $locale = new Locale($localeString);
64
        $fallback = $locale->fallbackLocale();
65
66
        if ($fallback->asString() !== $locale->asString()) {
67
            return $messages[$message] = $this->translate($message, $category, $fallback->asString());
68
        }
69
70
        return $messages[$message] = $message;
71
    }
72
73
    private function getMessages(string $category, string $language): array
74
    {
75
        $key = $language . '/' . $category;
76
77
        if (!array_key_exists($key, $this->messages)) {
78
            $this->messages[$key] = $this->messageReader->all($key);
79
        }
80
81
        return $this->messages[$key];
82
    }
83
84
    protected function getDefaultCategory(): string
85
    {
86
        return 'default';
87
    }
88
89
    protected function getDefaultLocale(): string
90
    {
91
        return \Locale::getDefault();
92
    }
93
}
94