MessageSource   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 104
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 7 3
A getMessages() 0 8 2
A __construct() 0 3 1
A translateMessage() 0 17 4
1
<?php
2
namespace Yii\I18n\Resource;
3
4
use Psr\EventDispatcher\EventDispatcherInterface;
0 ignored issues
show
Bug introduced by Alexander Makarov
The type Psr\EventDispatcher\EventDispatcherInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use yii\base\Component;
0 ignored issues
show
Bug introduced by Alexander Makarov
The type yii\base\Component was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use yii\i18n\event\OnMissingTranslation;
0 ignored issues
show
Bug introduced by Alexander Makarov
The type yii\i18n\event\OnMissingTranslation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * MessageSource is the base class for message translation repository classes.
10
 *
11
 * A message source stores message translations in some persistent storage.
12
 *
13
 * Child classes should override [[loadMessages()]] to provide translated messages.
14
 *
15
 * @author Qiang Xue <[email protected]>
16
 * @since 2.0
17
 */
18
abstract class MessageSource extends Component
19
{
20
    private $eventDispatcher;
21
22
    /**
23
     * @var bool whether to force message translation when the source and target languages are the same.
24
     * Defaults to false, meaning translation is only performed when source and target languages are different.
25
     */
26
    public $forceTranslation = false;
27
    /**
28
     * @var string the language that the original messages are in. If not set, it will use the value of
29
     * [[\yii\base\Application::sourceLanguage]].
30
     */
31
    public $sourceLanguage = 'en-US';
32
33
    private $messages = [];
34
35
    public function __construct(EventDispatcherInterface $eventDispatcher)
36
    {
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
41
    /**
42
     * Loads the message translation for the specified language and category.
43
     * If translation for specific locale code such as `en-US` isn't found it
44
     * tries more generic `en`.
45
     *
46
     * @param string $category the message category
47
     * @param string $language the target language
48
     * @return array the loaded messages. The keys are original messages, and the values
49
     * are translated messages.
50
     */
51
    abstract protected function loadMessages($category, $language): array;
52
53
    /**
54
     * Returns all messages for a given category in a given language.
55
     * Returned value is a result of {@see loadMessages()}
56
     *
57
     * @param string $category
58
     * @param string $language
59
     *
60
     * @return array
61
     */
62
    public function getMessages($category, $language)
63
    {
64
        $key = $language . '/' . $category;
65
        if (!isset($this->messages[$key])) {
66
            $this->messages[$key] = $this->loadMessages($category, $language);
67
        }
68
69
        return $this->messages[$key];
70
    }
71
72
    /**
73
     * Translates a message to the specified language.
74
     *
75
     * Note that unless [[forceTranslation]] is true, if the target language
76
     * is the same as the [[sourceLanguage|source language]], the message
77
     * will NOT be translated.
78
     *
79
     * If a translation is not found, a [[TranslationEvent::MISSING|missingTranslation]] event will be triggered.
80
     *
81
     * @param string $category the message category
82
     * @param string $message the message to be translated
83
     * @param string $language the target language
84
     * @return string|null the translated message or false if translation wasn't found or isn't required
85
     */
86
    public function translate($category, $message, $language): ?string
87
    {
88
        if ($this->forceTranslation || $language !== $this->sourceLanguage) {
89
            return $this->translateMessage($category, $message, $language);
90
        }
91
92
        return null;
93
    }
94
95
    /**
96
     * Translates the specified message.
97
     * If the message is not found, a [[TranslationEvent::MISSING|missingTranslation]] event will be triggered.
98
     * If there is an event handler, it may provide a [[MissingTranslationEvent::$translatedMessage|fallback translation]].
99
     * If no fallback translation is provided this method will return `false`.
100
     * @param string $category the category that the message belongs to.
101
     * @param string $message the message to be translated.
102
     * @param string $language the target language.
103
     * @return string|null the translated message or null if translation wasn't found.
104
     */
105
    protected function translateMessage($category, $message, $language): ?string
106
    {
107
        $messages = $this->getMessages($category, $language);
108
        if (isset($messages[$message]) && $messages[$message] !== '') {
109
            return $messages[$message];
110
        }
111
112
        $missingTranslation = new OnMissingTranslation($category, $language, $message);
113
        $this->eventDispatcher->dispatch($missingTranslation);
114
115
116
        if ($missingTranslation->hasFallback()) {
117
            return $messages[$message] = $missingTranslation->fallback();
118
        }
119
120
121
        return $messages[$message] = null;
122
    }
123
}
124