|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TGlobalization class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @copyright Copyright © 2005-2016 The PRADO Group |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
* @package Prado\I18N |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Prado\I18N; |
|
13
|
|
|
use Prado\Exceptions\TConfigurationException; |
|
14
|
|
|
use Prado\Prado; |
|
15
|
|
|
use Prado\TApplication; |
|
16
|
|
|
use Prado\TPropertyValue; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* TGlobalization contains settings for Culture, Charset |
|
20
|
|
|
* and TranslationConfiguration. |
|
21
|
|
|
* |
|
22
|
|
|
* TGlobalization can be subclassed to change how the Culture, Charset |
|
23
|
|
|
* are determined. See TGlobalizationAutoDetect for example of |
|
24
|
|
|
* setting the Culture based on browser settings. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
|
27
|
|
|
* @package Prado\I18N |
|
28
|
|
|
* @since 3.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
class TGlobalization extends \Prado\TModule |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Default character set is 'UTF-8'. |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $_defaultCharset = 'UTF-8'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Default culture is 'en'. |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $_defaultCulture = 'en'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The current charset. |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $_charset=null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The current culture. |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $_culture=null; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Translation source parameters. |
|
58
|
|
|
* @var TMap |
|
59
|
|
|
*/ |
|
60
|
|
|
private $_translation; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var boolean whether we should translate the default culture |
|
64
|
|
|
*/ |
|
65
|
|
|
private $_translateDefaultCulture=true; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Initialize the Culture and Charset for this application. |
|
69
|
|
|
* You should override this method if you want a different way of |
|
70
|
|
|
* setting the Culture and/or Charset for your application. |
|
71
|
|
|
* If you override this method, call parent::init($xml) first. |
|
72
|
|
|
* @param mixed application configuration |
|
73
|
|
|
*/ |
|
74
|
|
|
public function init($config) |
|
75
|
|
|
{ |
|
76
|
|
|
if($this->_charset===null) |
|
77
|
|
|
$this->_charset=$this->getDefaultCharset(); |
|
78
|
|
|
if($this->_culture===null) |
|
79
|
|
|
$this->_culture=$this->getDefaultCulture(); |
|
80
|
|
|
|
|
81
|
|
|
if($config!==null) |
|
82
|
|
|
{ |
|
83
|
|
|
if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP) |
|
84
|
|
|
$translation = isset($config['translate'])?$config['translate']:null; |
|
85
|
|
|
else |
|
86
|
|
|
{ |
|
87
|
|
|
$t = $config->getElementByTagName('translation'); |
|
88
|
|
|
$translation = ($t)?$t->getAttributes():null; |
|
89
|
|
|
} |
|
90
|
|
|
if($translation) |
|
91
|
|
|
$this->setTranslationConfiguration($translation); |
|
92
|
|
|
} |
|
93
|
|
|
$this->getApplication()->setGlobalization($this); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string default culture |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getTranslateDefaultCulture() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->_translateDefaultCulture; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param bool default culture, e.g. <tt>en_US</tt> for American English |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setTranslateDefaultCulture($value) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->_translateDefaultCulture = TPropertyValue::ensureBoolean($value); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string default culture |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getDefaultCulture() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->_defaultCulture; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string default culture, e.g. <tt>en_US</tt> for American English |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setDefaultCulture($culture) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->_defaultCulture = str_replace('-','_',$culture); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string default charset set |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getDefaultCharset() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->_defaultCharset; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string default localization charset, e.g. <tt>UTF-8</tt> |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setDefaultCharset($charset) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->_defaultCharset = $charset; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string current application culture |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getCulture() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->_culture; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string culture, e.g. <tt>en_US</tt> for American English |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setCulture($culture) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->_culture = str_replace('-','_',$culture); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string localization charset |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getCharset() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->_charset; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string localization charset, e.g. <tt>UTF-8</tt> |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setCharset($charset) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->_charset = $charset; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return TMap translation source configuration. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getTranslationConfiguration() |
|
180
|
|
|
{ |
|
181
|
|
|
return (!$this->_translateDefaultCulture && ($this->getDefaultCulture() == $this->getCulture())) |
|
182
|
|
|
? null |
|
183
|
|
|
: $this->_translation; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Sets the translation configuration. Example configuration: |
|
188
|
|
|
* <code> |
|
189
|
|
|
* $config['type'] = 'XLIFF'; //XLIFF, gettext, PHP, Database or MySQL (deprecated) |
|
190
|
|
|
* $config['source'] = 'Path.to.directory'; // for types XLIFF, PHP and gettext |
|
191
|
|
|
* $config['source'] = 'connectionId'; // for type Database |
|
192
|
|
|
* $config['source'] = 'mysql://user:pw@host/db'; // for type MySQL (deprecated) |
|
193
|
|
|
* $config['catalogue'] = 'messages'; //default catalog |
|
194
|
|
|
* $config['autosave'] = 'true'; //save untranslated message |
|
195
|
|
|
* $config['cache'] = 'true'; //cache translated message |
|
196
|
|
|
* $config['marker'] = '@@'; // surround untranslated text with '@@' |
|
197
|
|
|
* </code> |
|
198
|
|
|
* Throws exception is source is not found. |
|
199
|
|
|
* @param TMap|array configuration options |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function setTranslationConfiguration($config) |
|
202
|
|
|
{ |
|
203
|
|
|
if($config['type'] == 'XLIFF' || $config['type'] == 'gettext' || $config['type'] == 'PHP') |
|
204
|
|
|
{ |
|
205
|
|
|
if($config['source']) |
|
206
|
|
|
{ |
|
207
|
|
|
$config['source'] = Prado::getPathOfNamespace($config['source']); |
|
208
|
|
|
if(!is_dir($config['source'])) |
|
209
|
|
|
{ |
|
210
|
|
|
if(@mkdir($config['source'])===false) |
|
211
|
|
|
throw new TConfigurationException('globalization_source_path_failed', |
|
212
|
|
|
$config['source']); |
|
213
|
|
|
chmod($config['source'], PRADO_CHMOD); //make it deletable |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
else |
|
217
|
|
|
{ |
|
218
|
|
|
throw new TConfigurationException("invalid source dir '{$config['source']}'"); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
if(isset($config['cache']) && TPropertyValue::ensureBoolean($config['cache'])) |
|
222
|
|
|
{ |
|
223
|
|
|
$config['cache'] = $this->getApplication()->getRunTimePath().'/i18n'; |
|
224
|
|
|
if(!is_dir($config['cache'])) |
|
225
|
|
|
{ |
|
226
|
|
|
if(@mkdir($config['cache'])===false) |
|
227
|
|
|
throw new TConfigurationException('globalization_cache_path_failed', |
|
228
|
|
|
$config['cache']); |
|
229
|
|
|
chmod($config['cache'], PRADO_CHMOD); //make it deletable |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
else |
|
233
|
|
|
{ |
|
234
|
|
|
unset($config['cache']); |
|
235
|
|
|
} |
|
236
|
|
|
$this->_translation = $config; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return string current translation catalogue. |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getTranslationCatalogue() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->_translation['catalogue']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string update the translation catalogue. |
|
249
|
|
|
*/ |
|
250
|
|
|
public function setTranslationCatalogue($value) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->_translation['catalogue'] = $value; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Gets all the variants of a specific culture. If the parameter |
|
257
|
|
|
* $culture is null, the current culture is used. |
|
258
|
|
|
* @param string $culture the Culture string |
|
|
|
|
|
|
259
|
|
|
* @return array variants of the culture. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getCultureVariants($culture=null) |
|
262
|
|
|
{ |
|
263
|
|
|
if($culture===null) $culture = $this->getCulture(); |
|
264
|
|
|
$variants = explode('_', $culture); |
|
265
|
|
|
$result = array(); |
|
266
|
|
|
for(; count($variants) > 0; array_pop($variants)) |
|
|
|
|
|
|
267
|
|
|
$result[] = implode('_', $variants); |
|
268
|
|
|
return $result; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Returns a list of possible localized files. Example |
|
273
|
|
|
* <code> |
|
274
|
|
|
* $files = $app->getLocalizedResource("path/to/Home.page","en_US"); |
|
275
|
|
|
* </code> |
|
276
|
|
|
* will return |
|
277
|
|
|
* <pre> |
|
278
|
|
|
* array |
|
279
|
|
|
* 0 => 'path/to/en_US/Home.page' |
|
280
|
|
|
* 1 => 'path/to/en/Home.page' |
|
281
|
|
|
* 2 => 'path/to/Home.en_US.page' |
|
282
|
|
|
* 3 => 'path/to/Home.en.page' |
|
283
|
|
|
* 4 => 'path/to/Home.page' |
|
284
|
|
|
* </pre> |
|
285
|
|
|
* Note that you still need to verify the existance of these files. |
|
286
|
|
|
* @param string filename |
|
287
|
|
|
* @param string culture string, null to use current culture |
|
288
|
|
|
* @return array list of possible localized resource files. |
|
289
|
|
|
*/ |
|
290
|
|
|
public function getLocalizedResource($file,$culture=null) |
|
291
|
|
|
{ |
|
292
|
|
|
$files = array(); |
|
293
|
|
|
$variants = $this->getCultureVariants($culture); |
|
294
|
|
|
$path = pathinfo($file); |
|
295
|
|
|
foreach($variants as $variant) |
|
296
|
|
|
$files[] = $path['dirname'].DIRECTORY_SEPARATOR.$variant.DIRECTORY_SEPARATOR.$path['basename']; |
|
297
|
|
|
$filename = substr($path['basename'],0,strrpos($path['basename'],'.')); |
|
298
|
|
|
foreach($variants as $variant) |
|
299
|
|
|
$files[] = $path['dirname'].DIRECTORY_SEPARATOR.$filename.'.'.$variant.'.'.$path['extension']; |
|
300
|
|
|
$files[] = $file; |
|
301
|
|
|
return $files; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.