|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/i18n package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\I18n; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\BaseMethods; |
|
13
|
|
|
use Slick\Configuration\Configuration; |
|
14
|
|
|
use Slick\Configuration\ConfigurationInterface as DriverInterface; |
|
15
|
|
|
use Zend\I18n\Translator\Translator as ZendTranslator; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Translator (I18n) |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\I18n |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property DriverInterface $configuration |
|
24
|
|
|
* @property ZendTranslator $translatorService |
|
25
|
|
|
* |
|
26
|
|
|
* @property string $type |
|
27
|
|
|
* @property string $basePath |
|
28
|
|
|
* @property string $domain |
|
29
|
|
|
* @property string $fallbackLocale |
|
30
|
|
|
*/ |
|
31
|
|
|
class Translator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @readwrite |
|
35
|
|
|
* @var ZendTranslator |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_translatorService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @readwrite |
|
41
|
|
|
* @var DriverInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $_configuration; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @readwrite |
|
47
|
|
|
* @var string The message domain |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_domain = 'default'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @readwrite |
|
53
|
|
|
* @var string Default fallback locale |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $_fallbackLocale = 'en_US'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @readwrite |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $_basePath = './I18n'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @readwrite |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $_type = 'gettext'; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
private $_types = [ |
|
73
|
|
|
'gettext' => '.mo', |
|
74
|
|
|
'phparray' => '.php' |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var Translator |
|
79
|
|
|
*/ |
|
80
|
|
|
private static $_instance; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Trait with method for base class |
|
84
|
|
|
*/ |
|
85
|
|
|
use BaseMethods; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Protected constructor to prevent creating a new instance of the |
|
89
|
|
|
* *Singleton* via the `new` operator from outside of this class. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $options A list of properties for this connector |
|
92
|
|
|
*/ |
|
93
|
2 |
|
protected function __construct($options = array()) |
|
94
|
|
|
{ |
|
95
|
2 |
|
$this->hydrate($options); |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Private clone method to prevent cloning of the instance of the |
|
100
|
|
|
* *Singleton* instance. |
|
101
|
|
|
* |
|
102
|
|
|
* @codeCoverageIgnore |
|
103
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
private function __clone() |
|
107
|
|
|
{ |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Private unserialize method to prevent unserializing of the *Singleton* |
|
112
|
|
|
* instance. |
|
113
|
|
|
* |
|
114
|
|
|
* @codeCoverageIgnore |
|
115
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
private function __wakeup() |
|
119
|
|
|
{ |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Lazy loads configuration |
|
124
|
|
|
* |
|
125
|
|
|
* @return DriverInterface |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function getConfiguration() |
|
128
|
|
|
{ |
|
129
|
2 |
|
if (is_null($this->_configuration)) { |
|
130
|
2 |
|
$this->_configuration = Configuration::get('config'); |
|
131
|
2 |
|
} |
|
132
|
2 |
|
return $this->_configuration; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Lazy loads zend translator |
|
137
|
|
|
* |
|
138
|
|
|
* @return ZendTranslator |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function getTranslatorService() |
|
141
|
|
|
{ |
|
142
|
2 |
|
if (is_null($this->_translatorService)) { |
|
143
|
2 |
|
$translator = new ZendTranslator(); |
|
144
|
2 |
|
$translator->addTranslationFilePattern( |
|
145
|
2 |
|
$this->type, |
|
146
|
2 |
|
$this->basePath, |
|
147
|
2 |
|
'%s/'.$this->getMessageFile(), |
|
148
|
2 |
|
$this->domain |
|
149
|
2 |
|
); |
|
150
|
2 |
|
$this->_translatorService = $translator; |
|
151
|
2 |
|
} |
|
152
|
2 |
|
return $this->_translatorService; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns the messages file name based on domain |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function getMessageFile() |
|
161
|
|
|
{ |
|
162
|
2 |
|
$name = $this->domain; |
|
163
|
2 |
|
$name .= $this->_types[$this->type]; |
|
164
|
2 |
|
return $name; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Returns the translation for the provided message |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $message |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
2 |
|
public function translate($message) |
|
175
|
|
|
{ |
|
176
|
2 |
|
return $this->getTranslatorService() |
|
177
|
2 |
|
->translate($message, $this->domain, $this->getLocale()); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Translate a plural message. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $singular |
|
184
|
|
|
* @param string $plural |
|
185
|
|
|
* @param int $number |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
2 |
|
public function translatePlural($singular, $plural, $number) |
|
190
|
|
|
{ |
|
191
|
2 |
|
return $this->getTranslatorService() |
|
192
|
2 |
|
->translatePlural( |
|
193
|
2 |
|
$singular, |
|
194
|
2 |
|
$plural, |
|
195
|
2 |
|
$number, |
|
196
|
2 |
|
$this->domain, |
|
197
|
2 |
|
$this->getLocale() |
|
198
|
2 |
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns the *Singleton* instance of this class. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $options The list of property values of this instance. |
|
205
|
|
|
* |
|
206
|
|
|
* @return Translator The *Singleton* instance. |
|
207
|
|
|
*/ |
|
208
|
2 |
|
public static function getInstance($options = array()) |
|
209
|
|
|
{ |
|
210
|
2 |
|
if (is_null(self::$_instance)) { |
|
211
|
2 |
|
self::$_instance = new Translator($options); |
|
212
|
2 |
|
} |
|
213
|
2 |
|
return self::$_instance; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Sets locale |
|
218
|
|
|
* |
|
219
|
|
|
* @param $locale |
|
220
|
|
|
* |
|
221
|
|
|
* @returns Translator |
|
222
|
|
|
*/ |
|
223
|
2 |
|
public function setLocale($locale) |
|
224
|
|
|
{ |
|
225
|
2 |
|
$this->getConfiguration()->set('i18n.locale', $locale); |
|
226
|
2 |
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Gets current configured locale |
|
231
|
|
|
* |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
2 |
|
public function getLocale() |
|
235
|
|
|
{ |
|
236
|
2 |
|
return $this->configuration-> |
|
237
|
2 |
|
get('i18n.locale', $this->fallbackLocale); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|