1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to manage a translation string. |
7
|
|
|
*/ |
8
|
|
|
class Translation |
9
|
|
|
{ |
10
|
|
|
protected $context; |
11
|
|
|
protected $original; |
12
|
|
|
protected $translation = ''; |
13
|
|
|
protected $plural; |
14
|
|
|
protected $pluralTranslation = array(); |
15
|
|
|
protected $references = array(); |
16
|
|
|
protected $comments = array(); |
17
|
|
|
protected $extractedComments = array(); |
18
|
|
|
protected $flags = array(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates the id of a translation (context + glue + original). |
22
|
|
|
* |
23
|
|
|
* @param string $context |
24
|
|
|
* @param string $original |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public static function generateId($context, $original) |
29
|
|
|
{ |
30
|
|
|
return "{$context}\004{$original}"; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct. |
35
|
|
|
* |
36
|
|
|
* @param string $context The context of the translation |
37
|
|
|
* @param string $original The original string |
38
|
|
|
* @param string $plural The original plural string |
39
|
|
|
*/ |
40
|
|
|
public function __construct($context, $original, $plural = '') |
41
|
|
|
{ |
42
|
|
|
$this->context = (string) $context; |
43
|
|
|
$this->original = (string) $original; |
44
|
|
|
|
45
|
|
|
$this->setPlural($plural); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Clones this translation. |
50
|
|
|
* |
51
|
|
|
* @param null|string $context Optional new context |
52
|
|
|
* @param null|string $original Optional new original |
53
|
|
|
*/ |
54
|
|
|
public function getClone($context = null, $original = null) |
55
|
|
|
{ |
56
|
|
|
$new = clone $this; |
57
|
|
|
|
58
|
|
|
if ($context !== null) { |
59
|
|
|
$new->context = (string) $context; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($original !== null) { |
63
|
|
|
$new->original = (string) $original; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $new; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the id of this translation. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getId() |
75
|
|
|
{ |
76
|
|
|
return static::generateId($this->context, $this->original); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks whether the translation matches with the arguments. |
81
|
|
|
* |
82
|
|
|
* @param string $context |
83
|
|
|
* @param string $original |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function is($context, $original = '') |
88
|
|
|
{ |
89
|
|
|
return (($this->context === $context) && ($this->original === $original)) ? true : false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Gets the original string. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getOriginal() |
98
|
|
|
{ |
99
|
|
|
return $this->original; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks if the original string is empty or not. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasOriginal() |
108
|
|
|
{ |
109
|
|
|
return ($this->original !== '') ? true : false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets the translation string. |
114
|
|
|
* |
115
|
|
|
* @param string $translation |
116
|
|
|
* |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
public function setTranslation($translation) |
120
|
|
|
{ |
121
|
|
|
$this->translation = (string) $translation; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets the translation string. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getTranslation() |
132
|
|
|
{ |
133
|
|
|
return $this->translation; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Checks if the translation string is empty or not. |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasTranslation() |
142
|
|
|
{ |
143
|
|
|
return ($this->translation !== '') ? true : false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets the plural translation string. |
148
|
|
|
* |
149
|
|
|
* @param string $plural |
150
|
|
|
* |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
|
|
public function setPlural($plural) |
154
|
|
|
{ |
155
|
|
|
$this->plural = (string) $plural; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Gets the plural translation string. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getPlural() |
166
|
|
|
{ |
167
|
|
|
return $this->plural; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Checks if the plural translation string is empty or not. |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function hasPlural() |
176
|
|
|
{ |
177
|
|
|
return ($this->plural !== '') ? true : false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set a new plural translation. |
182
|
|
|
* |
183
|
|
|
* @param array $plural |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
|
|
public function setPluralTranslations(array $plural) |
188
|
|
|
{ |
189
|
|
|
$this->pluralTranslation = $plural; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets all plural translations. |
196
|
|
|
* |
197
|
|
|
* @param int $limit |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getPluralTranslations($limit = null) |
202
|
|
|
{ |
203
|
|
|
if ($limit === null) { |
204
|
|
|
return $this->pluralTranslation; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$current = count($this->pluralTranslation); |
208
|
|
|
|
209
|
|
|
if ($limit > $current) { |
210
|
|
|
return $this->pluralTranslation + array_fill(0, $limit, ''); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ($limit < $current) { |
214
|
|
|
return array_slice($this->pluralTranslation, 0, $limit); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->pluralTranslation; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Checks if there are any plural translation. |
222
|
|
|
* |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function hasPluralTranslations() |
226
|
|
|
{ |
227
|
|
|
return implode('', $this->pluralTranslation) !== ''; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Removes all plural translations. |
232
|
|
|
*/ |
233
|
|
|
public function deletePluralTranslation() |
234
|
|
|
{ |
235
|
|
|
$this->pluralTranslation = array(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Gets the context of this translation. |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function getContext() |
244
|
|
|
{ |
245
|
|
|
return $this->context; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Checks if the context is empty or not. |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function hasContext() |
254
|
|
|
{ |
255
|
|
|
return (isset($this->context) && ($this->context !== '')) ? true : false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Adds a new reference for this translation. |
260
|
|
|
* |
261
|
|
|
* @param string $filename The file path where the translation has been found |
262
|
|
|
* @param null|int $line The line number where the translation has been found |
263
|
|
|
*/ |
264
|
|
|
public function addReference($filename, $line = null) |
265
|
|
|
{ |
266
|
|
|
$key = "{$filename}:{$line}"; |
267
|
|
|
$this->references[$key] = array($filename, $line); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Checks if the translation has any reference. |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function hasReferences() |
276
|
|
|
{ |
277
|
|
|
return !empty($this->references); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Return all references for this translation. |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function getReferences() |
286
|
|
|
{ |
287
|
|
|
return array_values($this->references); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Removes all references. |
292
|
|
|
*/ |
293
|
|
|
public function deleteReferences() |
294
|
|
|
{ |
295
|
|
|
$this->references = array(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Adds a new comment for this translation. |
300
|
|
|
* |
301
|
|
|
* @param string $comment |
302
|
|
|
*/ |
303
|
|
|
public function addComment($comment) |
304
|
|
|
{ |
305
|
|
|
if (!in_array($comment, $this->comments, true)) { |
306
|
|
|
$this->comments[] = $comment; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Checks if the translation has any comment. |
312
|
|
|
* |
313
|
|
|
* @return bool |
314
|
|
|
*/ |
315
|
|
|
public function hasComments() |
316
|
|
|
{ |
317
|
|
|
return isset($this->comments[0]); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Returns all comments for this translation. |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
|
|
public function getComments() |
326
|
|
|
{ |
327
|
|
|
return $this->comments; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Removes all comments. |
332
|
|
|
*/ |
333
|
|
|
public function deleteComments() |
334
|
|
|
{ |
335
|
|
|
$this->comments = array(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Adds a new extracted comment for this translation. |
340
|
|
|
* |
341
|
|
|
* @param string $comment |
342
|
|
|
*/ |
343
|
|
|
public function addExtractedComment($comment) |
344
|
|
|
{ |
345
|
|
|
if (!in_array($comment, $this->extractedComments, true)) { |
346
|
|
|
$this->extractedComments[] = $comment; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Checks if the translation has any extracted comment. |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
|
|
public function hasExtractedComments() |
356
|
|
|
{ |
357
|
|
|
return isset($this->extractedComments[0]); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns all extracted comments for this translation. |
362
|
|
|
* |
363
|
|
|
* @return array |
364
|
|
|
*/ |
365
|
|
|
public function getExtractedComments() |
366
|
|
|
{ |
367
|
|
|
return $this->extractedComments; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Removes all extracted comments. |
372
|
|
|
*/ |
373
|
|
|
public function deleteExtractedComments() |
374
|
|
|
{ |
375
|
|
|
$this->extractedComments = array(); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Adds a new flat for this translation. |
380
|
|
|
* |
381
|
|
|
* @param string $flag |
382
|
|
|
*/ |
383
|
|
|
public function addFlag($flag) |
384
|
|
|
{ |
385
|
|
|
if (!in_array($flag, $this->flags, true)) { |
386
|
|
|
$this->flags[] = $flag; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Checks if the translation has any flag. |
392
|
|
|
* |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
|
|
public function hasFlags() |
396
|
|
|
{ |
397
|
|
|
return isset($this->flags[0]); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Returns all extracted flags for this translation. |
402
|
|
|
* |
403
|
|
|
* @return array |
404
|
|
|
*/ |
405
|
|
|
public function getFlags() |
406
|
|
|
{ |
407
|
|
|
return $this->flags; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Removes all flags. |
412
|
|
|
*/ |
413
|
|
|
public function deleteFlags() |
414
|
|
|
{ |
415
|
|
|
$this->flags = array(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Merges this translation with other translation. |
420
|
|
|
* |
421
|
|
|
* @param Translation $translation The translation to merge with |
422
|
|
|
* @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations |
423
|
|
|
*/ |
424
|
|
|
public function mergeWith(Translation $translation, $method = null) |
425
|
|
|
{ |
426
|
|
|
if ($method === null) { |
427
|
|
|
$method = Translations::$mergeDefault; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
if (!$this->hasTranslation() || ($translation->hasTranslation() && ($method & Translations::MERGE_OVERRIDE))) { |
431
|
|
|
$this->setTranslation($translation->getTranslation()); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
if (($method & Translations::MERGE_PLURAL) && !$this->hasPlural()) { |
435
|
|
|
$this->setPlural($translation->getPlural()); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if ($this->hasPlural() && !$this->hasPluralTranslations() && $translation->hasPluralTranslations()) { |
439
|
|
|
$this->pluralTranslation = $translation->getPluralTranslations(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if ($method & Translations::MERGE_REFERENCES) { |
443
|
|
|
foreach ($translation->getReferences() as $reference) { |
444
|
|
|
$this->addReference($reference[0], $reference[1]); |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
if ($method & Translations::MERGE_COMMENTS) { |
449
|
|
|
$this->comments = array_values(array_unique(array_merge($translation->getComments(), $this->comments))); |
450
|
|
|
$this->extractedComments = array_values(array_unique(array_merge($translation->getExtractedComments(), $this->extractedComments))); |
451
|
|
|
$this->flags = array_values(array_unique(array_merge($translation->getFlags(), $this->flags))); |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
|