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