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 Zend\I18n\Translator\Translator as ZendTranslator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Translator (I18n) |
17
|
|
|
* |
18
|
|
|
* @package Slick\I18n |
19
|
|
|
* @author Filipe Silva <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @property ZendTranslator $translatorService |
22
|
|
|
* |
23
|
|
|
* @property string $type |
24
|
|
|
* @property string $basePath |
25
|
|
|
* @property string $domain |
26
|
|
|
* @property string $fallbackLocale |
27
|
|
|
* |
28
|
|
|
* @method Translator setBasePath(string $basePath) |
29
|
|
|
* @method Translator setDomain(string $domainName) |
30
|
|
|
* @method Translator setType(string $type) |
31
|
|
|
* @method Translator setLocale(string $locale) |
32
|
|
|
* |
33
|
|
|
* @method string getLocale() |
34
|
|
|
* @method string getDomain() |
35
|
|
|
* @method string getBasePath() |
36
|
|
|
*/ |
37
|
|
|
class Translator |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
const TYPE_PHP_ARRAY = 'phparray'; |
41
|
|
|
const TYPE_GETTEXT = 'gettext'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @readwrite |
45
|
|
|
* @var ZendTranslator |
46
|
|
|
*/ |
47
|
|
|
protected $translatorService; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @readwrite |
51
|
|
|
* @var string The message domain |
52
|
|
|
*/ |
53
|
|
|
protected $domain = 'default'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @readwrite |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $basePath = './I18n'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @readwrite |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $type = self::TYPE_PHP_ARRAY; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @readwrite |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $locale = 'en_US'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $types = [ |
77
|
|
|
self::TYPE_GETTEXT => '.mo', |
78
|
|
|
self::TYPE_PHP_ARRAY => '.php' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var Translator |
83
|
|
|
*/ |
84
|
|
|
private static $instance; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
private $loadedFiles = []; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Trait with method for base class |
93
|
|
|
*/ |
94
|
|
|
use BaseMethods; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Protected constructor to prevent creating a new instance of the |
98
|
|
|
* *Singleton* via the `new` operator from outside of this class. |
99
|
|
|
* |
100
|
|
|
* @param array $options A list of properties for this connector |
101
|
|
|
*/ |
102
|
2 |
|
protected function __construct($options = array()) |
103
|
|
|
{ |
104
|
2 |
|
$this->hydrate($options); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Private clone method to prevent cloning of the instance of the |
109
|
|
|
* *Singleton* instance. |
110
|
|
|
* |
111
|
|
|
* @codeCoverageIgnore |
112
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
private function __clone() |
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Lazy loads zend translator |
121
|
|
|
* |
122
|
|
|
* @return ZendTranslator |
123
|
|
|
*/ |
124
|
6 |
|
public function getTranslatorService() |
125
|
|
|
{ |
126
|
6 |
|
if (is_null($this->translatorService)) { |
127
|
2 |
|
$this->translatorService = new ZendTranslator(); |
128
|
2 |
|
} |
129
|
6 |
|
return $this->translatorService; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the messages file name based on domain |
134
|
|
|
* |
135
|
|
|
* @param string $domain |
136
|
|
|
* @param string $locale |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
4 |
|
protected function loadFile($domain = null, $locale = null) |
141
|
|
|
{ |
142
|
4 |
|
$domain = $domain ?: $this->domain; |
143
|
4 |
|
$locale = $locale ?: $this->locale; |
144
|
4 |
|
$key = "{$this->basePath}::{$this->type}::{$locale}::{$domain}"; |
145
|
|
|
|
146
|
4 |
|
if (!array_key_exists($key, $this->loadedFiles)) { |
147
|
2 |
|
$name = "{$domain}{$this->types[$this->type]}"; |
148
|
2 |
|
$this->loadedFiles[$key] = $name; |
149
|
|
|
|
150
|
2 |
|
$this->getTranslatorService()->addTranslationFilePattern( |
151
|
2 |
|
$this->type, |
152
|
2 |
|
$this->basePath, |
153
|
2 |
|
"%s/{$name}", |
154
|
|
|
$domain |
155
|
2 |
|
); |
156
|
2 |
|
} |
157
|
|
|
|
158
|
4 |
|
return [$domain, $locale]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the translation for the provided message |
163
|
|
|
* |
164
|
|
|
* @param string $message |
165
|
|
|
* @param string $domain |
166
|
|
|
* @param string $locale |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
2 |
|
public function translate($message, $domain = null, $locale = null) |
171
|
|
|
{ |
172
|
2 |
|
list($domain, $locale) = $this->loadFile($domain, $locale); |
173
|
2 |
|
return $this->getTranslatorService() |
174
|
2 |
|
->translate($message, $domain, $locale); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Translate a plural message. |
179
|
|
|
* |
180
|
|
|
* @param string $singular |
181
|
|
|
* @param string $plural |
182
|
|
|
* @param int $number |
183
|
|
|
* @param string $domain |
184
|
|
|
* @param string $locale |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
2 |
|
public function translatePlural( |
189
|
|
|
$singular, $plural, $number,$domain = null, $locale = null |
190
|
|
|
) { |
191
|
2 |
|
list($domain, $locale) = $this->loadFile($domain, $locale); |
192
|
2 |
|
return $this->getTranslatorService() |
193
|
2 |
|
->translatePlural($singular, $plural, $number, $domain, $locale); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the *Singleton* instance of this class. |
198
|
|
|
* |
199
|
|
|
* @param array $options The list of property values of this instance. |
200
|
|
|
* |
201
|
|
|
* @return Translator The *Singleton* instance. |
202
|
|
|
*/ |
203
|
6 |
|
public static function getInstance($options = array()) |
204
|
|
|
{ |
205
|
6 |
|
if (is_null(self::$instance)) { |
206
|
2 |
|
self::$instance = new Translator($options); |
207
|
2 |
|
} |
208
|
6 |
|
return self::$instance; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|