1 | <?php |
||
20 | class Translator implements TranslatorInterface |
||
21 | { |
||
22 | /** |
||
23 | * |
||
24 | * A fallback translator. |
||
25 | * |
||
26 | * @var TranslatorInterface |
||
27 | * |
||
28 | */ |
||
29 | protected $fallback; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * The formatter to use when translating messages. |
||
34 | * |
||
35 | * @var FormatterInterface |
||
36 | * |
||
37 | */ |
||
38 | protected $formatter; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * The locale being used for translations. |
||
43 | * |
||
44 | * @var string |
||
45 | * |
||
46 | */ |
||
47 | protected $locale; |
||
48 | |||
49 | /** |
||
50 | * The Package containing keys and translations. |
||
51 | * |
||
52 | * @var Package |
||
53 | * |
||
54 | */ |
||
55 | protected $package; |
||
56 | |||
57 | /** |
||
58 | * |
||
59 | * Constructor |
||
60 | * |
||
61 | * @param string $locale The locale being used. |
||
62 | * |
||
63 | * @param Package $package The Package containing keys and translations. |
||
64 | * |
||
65 | * @param FormatterInterface $formatter A message formatter. |
||
66 | * |
||
67 | * @param TranslatorInterface $fallback A fallback translator. |
||
68 | * |
||
69 | */ |
||
70 | 5 | public function __construct( |
|
81 | |||
82 | /** |
||
83 | * |
||
84 | * Gets the message translation by its key. |
||
85 | * |
||
86 | * @param string $key The message key. |
||
87 | * |
||
88 | * @return mixed The message translation string, or false if not found. |
||
89 | * |
||
90 | */ |
||
91 | 4 | protected function getMessage($key) |
|
92 | { |
||
93 | 4 | $message = $this->package->getMessage($key); |
|
94 | 4 | if ($message) { |
|
95 | 3 | return $message; |
|
96 | } |
||
97 | |||
98 | 3 | if ($this->fallback) { |
|
99 | // get the message from the fallback translator |
||
100 | 1 | $message = $this->fallback->getMessage($key); |
|
101 | 1 | if ($message) { |
|
102 | // speed optimization: retain locally |
||
103 | 1 | $this->package->addMessage($key, $message); |
|
104 | // done! |
||
105 | 1 | return $message; |
|
106 | } |
||
107 | } |
||
108 | |||
109 | // no local message, no fallback |
||
110 | 2 | return false; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * |
||
115 | * Translates the message indicated by they key, replacing token values |
||
116 | * along the way. |
||
117 | * |
||
118 | * @param string $key The message key. |
||
119 | * |
||
120 | * @param array $tokens_values Token values to interpolate into the |
||
121 | * message. |
||
122 | * |
||
123 | * @return string The translated message with tokens replaced. |
||
124 | * |
||
125 | */ |
||
126 | 4 | public function translate($key, array $tokens_values = []) |
|
127 | { |
||
128 | // get the message string |
||
129 | 4 | $message = $this->getMessage($key); |
|
130 | |||
131 | // do we have a message string? |
||
132 | 4 | if (! $message) { |
|
133 | // no, return the message key as-is |
||
134 | 2 | $message = $key; |
|
135 | } |
||
136 | |||
137 | // are there token replacement values? |
||
138 | 4 | if (! $tokens_values) { |
|
|
|||
139 | // no, return the message string as-is |
||
140 | 3 | return $message; |
|
141 | } |
||
142 | |||
143 | // run message string through formatter to replace tokens with values |
||
144 | 2 | return $this->formatter->format($this->locale, $message, $tokens_values); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * |
||
149 | * @return Package |
||
150 | * |
||
151 | */ |
||
152 | public function getPackage() |
||
156 | } |
||
157 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.