ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Translation.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext;
4
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();
20
29 public static function generateId($context, $original)
30 {
31 return "{$context}\004{$original}";
32 }
33
41 public function __construct($context, $original, $plural = '')
42 {
43 $this->context = (string) $context;
44 $this->original = (string) $original;
45
46 $this->setPlural($plural);
47 }
48
55 public function getClone($context = null, $original = null)
56 {
57 $new = clone $this;
58
59 if ($context !== null) {
60 $new->context = (string) $context;
61 }
62
63 if ($original !== null) {
64 $new->original = (string) $original;
65 }
66
67 return $new;
68 }
69
75 public function getId()
76 {
77 return static::generateId($this->context, $this->original);
78 }
79
88 public function is($context, $original = '')
89 {
90 return (($this->context === $context) && ($this->original === $original)) ? true : false;
91 }
92
98 public function getOriginal()
99 {
100 return $this->original;
101 }
102
108 public function hasOriginal()
109 {
110 return ($this->original !== '') ? true : false;
111 }
112
118 public function setTranslation($translation)
119 {
120 $this->translation = (string) $translation;
121 }
122
128 public function getTranslation()
129 {
130 return $this->translation;
131 }
132
138 public function hasTranslation()
139 {
140 return ($this->translation !== '') ? true : false;
141 }
142
148 public function setPlural($plural)
149 {
150 $this->plural = (string) $plural;
151
152 $this->normalizeTranslationCount();
153 }
154
160 public function getPlural()
161 {
162 return $this->plural;
163 }
164
170 public function hasPlural()
171 {
172 return ($this->plural !== '') ? true : false;
173 }
174
181 public function setPluralTranslation($plural, $key = 0)
182 {
183 $this->pluralTranslation[$key] = $plural;
184 $this->normalizeTranslationCount();
185 }
186
194 public function getPluralTranslation($key = null)
195 {
196 if ($key === null) {
197 return $this->pluralTranslation;
198 }
199
200 return isset($this->pluralTranslation[$key]) ? (string) $this->pluralTranslation[$key] : '';
201 }
202
208 public function hasPluralTranslation()
209 {
210 return implode('', $this->pluralTranslation) !== '';
211 }
212
216 public function deletePluralTranslation()
217 {
218 $this->pluralTranslation = array();
219
220 $this->normalizeTranslationCount();
221 }
222
228 public function setTranslationCount($count)
229 {
230 $this->translationCount = is_null($count) ? null : intval($count);
231
232 $this->normalizeTranslationCount();
233 }
234
241 public function getTranslationCount()
242 {
243 return $this->hasPlural() ? $this->translationCount : null;
244 }
245
249 protected function normalizeTranslationCount()
250 {
251 if ($this->translationCount === null) {
252 return;
253 }
254
255 if ($this->hasPlural()) {
256 $allowed = $this->translationCount - 1;
257 $current = count($this->pluralTranslation);
258
259 if ($allowed > $current) {
260 $this->pluralTranslation = $this->pluralTranslation + array_fill(0, $allowed, '');
261 } elseif ($current > $allowed) {
262 $this->pluralTranslation = array_slice($this->pluralTranslation, 0, $allowed);
263 }
264 } else {
265 $this->pluralTranslation = array();
266 }
267 }
268
274 public function getContext()
275 {
276 return $this->context;
277 }
278
284 public function hasContext()
285 {
286 return (isset($this->context) && ($this->context !== '')) ? true : false;
287 }
288
295 public function addReference($filename, $line = null)
296 {
297 $key = "{$filename}:{$line}";
298 $this->references[$key] = array($filename, $line);
299 }
300
306 public function hasReferences()
307 {
308 return !empty($this->references);
309 }
310
316 public function getReferences()
317 {
318 return array_values($this->references);
319 }
320
324 public function deleteReferences()
325 {
326 $this->references = array();
327 }
328
334 public function addComment($comment)
335 {
336 $this->comments[] = $comment;
337 }
338
344 public function hasComments()
345 {
346 return isset($this->comments[0]);
347 }
348
354 public function getComments()
355 {
356 return $this->comments;
357 }
358
362 public function deleteComments()
363 {
364 $this->comments = array();
365 }
366
373 {
374 $this->extractedComments[] = $comment;
375 }
376
382 public function hasExtractedComments()
383 {
384 return isset($this->extractedComments[0]);
385 }
386
392 public function getExtractedComments()
393 {
394 return $this->extractedComments;
395 }
396
400 public function deleteExtractedComments()
401 {
402 $this->extractedComments = array();
403 }
404
410 public function addFlag($flag)
411 {
412 $this->flags[] = $flag;
413 }
414
420 public function hasFlags()
421 {
422 return isset($this->flags[0]);
423 }
424
430 public function getFlags()
431 {
432 return $this->flags;
433 }
434
438 public function deleteFlags()
439 {
440 $this->flags = array();
441 }
442
449 public function mergeWith(Translation $translation, $method = null)
450 {
451 if ($method === null) {
452 $method = Translations::$mergeDefault;
453 }
454
455 if (!$this->hasTranslation() || ($translation->hasTranslation() && ($method & Translations::MERGE_OVERRIDE))) {
456 $this->setTranslation($translation->getTranslation());
457 }
458
459 if (($method & Translations::MERGE_PLURAL) && !$this->hasPlural()) {
460 $this->setPlural($translation->getPlural());
461 }
462
463 if ($this->hasPlural() && !$this->hasPluralTranslation() && $translation->hasPluralTranslation()) {
464 $this->pluralTranslation = $translation->getPluralTranslation();
465 }
466
467 if ($method & Translations::MERGE_REFERENCES) {
468 foreach ($translation->getReferences() as $reference) {
469 $this->addReference($reference[0], $reference[1]);
470 }
471 }
472
473 if ($method & Translations::MERGE_COMMENTS) {
474 $this->comments = array_values(array_unique(array_merge($translation->getComments(), $this->comments)));
475 $this->extractedComments = array_values(array_unique(array_merge($translation->getExtractedComments(), $this->extractedComments)));
476 $this->flags = array_values(array_unique(array_merge($translation->getFlags(), $this->flags)));
477 }
478 }
479}
$filename
Definition: buildRTE.php:89
$comment
Definition: buildRTE.php:83
An exception for terminatinating execution or to throw for unit testing.
Class to manage a translation string.
Definition: Translation.php:9
addExtractedComment($comment)
Adds a new extracted comment for this translation.
getPluralTranslation($key=null)
Gets one or all plural translations.
normalizeTranslationCount()
Normalizes the translation count.
deleteComments()
Removes all comments.
hasContext()
Checks if the context is empty or not.
getComments()
Returns all comments for this translation.
hasExtractedComments()
Checks if the translation has any extracted comment.
getFlags()
Returns all extracted flags for this translation.
hasReferences()
Checks if the translation has any reference.
deletePluralTranslation()
Removes all plural translations.
hasFlags()
Checks if the translation has any flag.
hasOriginal()
Checks if the original string is empty or not.
deleteFlags()
Removes all flags.
hasComments()
Checks if the translation has any comment.
getTranslationCount()
Returns the number of singular + plural translations Returns null if this Translation is not a plural...
setTranslationCount($count)
Set the number of singular + plural translations allowed.
getContext()
Gets the context of this translation.
addReference($filename, $line=null)
Adds a new reference for this translation.
getTranslation()
Gets the translation string.
addFlag($flag)
Adds a new flat for this translation.
deleteReferences()
Removes all references.
static generateId($context, $original)
Generates the id of a translation (context + glue + original).
Definition: Translation.php:29
is($context, $original='')
Checks whether the translation matches with the arguments.
Definition: Translation.php:88
getPlural()
Gets the plural translation string.
setPlural($plural)
Sets the plural translation string.
hasPlural()
Checks if the plural translation string is empty or not.
getReferences()
Return all references for this translation.
setPluralTranslation($plural, $key=0)
Set a new plural translation.
__construct($context, $original, $plural='')
Construct.
Definition: Translation.php:41
getOriginal()
Gets the original string.
Definition: Translation.php:98
mergeWith(Translation $translation, $method=null)
Merges this translation with other translation.
addComment($comment)
Adds a new comment for this translation.
deleteExtractedComments()
Removes all extracted comments.
getClone($context=null, $original=null)
Clones this translation.
Definition: Translation.php:55
hasTranslation()
Checks if the translation string is empty or not.
hasPluralTranslation()
Checks if there are any plural translation.
getExtractedComments()
Returns all extracted comments for this translation.
setTranslation($translation)
Sets the translation string.
getId()
Returns the id of this translation.
Definition: Translation.php:75
$key
Definition: croninfo.php:18
$context
Definition: webdav.php:25