-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGettextDictionary.php
689 lines (625 loc) · 24.8 KB
/
GettextDictionary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
/**
* Manipulation with gettext .po and .mo files
* @author Pavel Železný <[email protected]>
*/
class GettextDictionary {
/** @var array */
private $defaultHeaders = array();
/** @var array */
public $files = array();
/** @var array */
private $headers = array();
/** @var array */
private $translations = array();
/** @var bool */
private $colaborativeMode = FALSE;
/**
* Constructor
* @author Pavel Železný <[email protected]>
* @param string $path Optional gettext dictionary file path
* @param string $identifier Optional dictionary name
* @return void
*/
public function __construct($path = NULL, $identifier = NULL) {
if ($path !== NULL) {
$this->loadDictionary($path, $identifier);
} elseif (($path === NULL) && ($identifier !== NULL)) {
throw new InvalidArgumentException('New empty dictionary can not have name.');
}
$this->setDefaultHeaders(array(
'Project-Id-Version' => '',
'POT-Creation-Date' => date("Y-m-d H:iO"),
'PO-Revision-Date' => date("Y-m-d H:iO"),
'Language-Team' => '',
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
'Plural-Forms' => 'nplurals=2; plural=(n==1)? 0 : 1;',
'Last-Translator' => 'JAO NetteTranslator <[email protected]>',
'X-Poedit-Language' => '',
'X-Poedit-Country' => '',
'X-Poedit-SourceCharset' => 'utf-8',
'X-Poedit-KeywordsList' => '',
'X-Poedit-Basepath' => '.',
'X-Poedit-SearchPath-0' => '.',
'X-Poedit-SearchPath-1' => '..',
));
}
/**
* Load gettext dictionary file
* @author Pavel Železný <[email protected]>
* @param string $path Gettext dictionary file path
* @param string $identifier Optional dictionary name
* @return \Gettext provides a fluent interface
* @throws \InvalidArgumentException \InvalidArgumentException
* @todo Change logic of force loading more than one file
*/
public function loadDictionary($path, $identifier = NULL) {
if ($this->getTranslationsCount() == 0) {
if ((file_exists($path)) && (is_readable($path))) {
try {
switch (pathinfo($path, PATHINFO_EXTENSION)) {
case 'mo':
if (filesize($path) > 10) {
$this->setDictionaryFileId($path, $identifier);
return $this->parseMoFile(basename($path));
} else {
throw new \InvalidArgumentException('Dictionary file is not .mo compatible');
}
break;
case 'po':
$this->setDictionaryFileId($path, $identifier);
return $this->parsePoFile(basename($path));
break;
default:
throw new \InvalidArgumentException('Unsupported file type');
}
$this->setDictionaryFileId($path, $identifier);
} catch (\InvalidArgumentException $exception) {
throw new \InvalidArgumentException('Dictionary file structure is not correct.', 0, $exception);
}
} else {
throw new \InvalidArgumentException('Dictionary file is not exist or is not readable');
}
} else {
throw new \InvalidArgumentException('Dictionary is not empty');
}
}
/**
* Save gettext dictionary file
* @author Pavel Železný <[email protected]>
* @param string $path Gettext dictionary file path
* @param string $identifier Optional dictionary name
* @return \Gettext provides a fluent interface
* @throws \InvalidArgumentException
*/
public function saveDictionary($path, $identifier = NULL) {
// Force datetime change of newly generated dictionary
$this->setHeader('PO-Revision-Date', date("Y-m-d H:iO"), $identifier);
if (((file_exists($path) === TRUE) && (is_writable($path))) || ((file_exists($path) === FALSE) && (is_writable(dirname($path))))) {
switch (pathinfo($path, PATHINFO_EXTENSION)) {
case 'mo':
return $this->generateMoFile($path, $identifier);
break;
case 'po':
return $this->generatePoFile($path, $identifier);
break;
default:
throw new \InvalidArgumentException('Unsupported file type');
}
} else {
throw new \InvalidArgumentException('Destination is not writeable');
}
}
/**
* Parse content of gettext .po file
* @author Pavel Železný <[email protected]>
* @param string $path Gettext .po file path
* @return \Gettext provides a fluent interface
* @see http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/PO-Files.html#PO-Files
* @throws \InvalidArgumentException
*/
private function parsePoFile($path) {
$path = realpath($path);
$fp = @fopen($path, 'r');
while (feof($fp) != TRUE) {
$buffer = fgets($fp);
if (preg_match('/^#([.:, ]|(\| msgctxt)|(\| msgid)) (.*)$/', $buffer, $matches)) {
switch ($matches[1]) {
case ' ':
$comments['comment'] = $matches[4];
break;
case '.':
$comments['extracted-comment'] = $matches[4];
break;
case ':':
$comments['reference'] = $matches[4];
break;
case ',':
$comments['flag'] = $matches[4];
break;
case '| msgctxt':
$comments['previous-context'] = $matches[4];
break;
case '| msgid':
$comments['previous-untranslated-string'] = $matches[4];
break;
}
} elseif (preg_match('/^msgctxt "(.*)"$/', $buffer, $matches)) {
$context = $matches[1];
} elseif (preg_match('/^msgid "(.*)"$/', $buffer, $matches)) {
$original = $matches[1];
if ($matches[1] != '') {
$translations = & $this->addOriginal($original, isset($context) ? $context : '', $path);
if (isset($comments)) {
$translations->setComments($comments);
}
}
} elseif (preg_match('/^msgid_plural "(.*)"$/', $buffer, $matches)) {
$translations->setPlural($matches[1]);
} elseif ((preg_match('/^msgstr "(.*)"$/', $buffer, $matches)) && ($original != '')) {
$translations->setTranslation(str_replace('\n', "\n", $matches[1]), 0);
} elseif ((preg_match('/^msgstr\[([0-9]+)\] "(.*)"$/', $buffer, $matches)) && ($original[0] != '')) {
$lastIndex = (int) $matches[1];
$translations->setTranslation(str_replace('\n', "\n", $matches[2]), $lastIndex);
} elseif (preg_match('/^"(.*)"$/', $buffer, $matches)) {
if ((isset($original)) && ($original != '')) {
$translations->setTranslation($translations->getTranslation(isset($lastIndex) ? $lastIndex : 0) . str_replace('\n', "\n", $matches[1]), isset($lastIndex) ? $lastIndex : 0);
} else {
$delimiter = strpos($matches[1], ': ');
$this->setHeader(substr($matches[1], 0, $delimiter), trim(substr(str_replace('\n', "\n", $matches[1]), $delimiter + 2)), $path);
}
} elseif (trim($buffer) == '') {
unset($comments, $original, $lastIndex, $comment, $context, $translations);
}
}
fclose($fp);
return $this;
}
/**
* Parse binary content of gettext .mo file
* @author Pavel Železný <[email protected]>
* @param string $path Gettext .mo file path
* @return \Gettext provides a fluent interface
* @see http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/MO-Files.html#MO-Files
* @throws \InvalidArgumentException
*/
private function parseMoFile($path) {
$path = realpath($path);
$fp = @fopen($path, 'rb');
/* binary block read function */
$endian = FALSE;
$read = function($bytes, $seekBytes = NULL, $unpack = TRUE) use ($fp, $endian) {
if ($seekBytes !== NULL) {
fseek($fp, $seekBytes);
}
$data = fread($fp, $bytes);
return $unpack === TRUE ? ($endian === FALSE ? unpack('V' . $bytes / 4, $data) : unpack('N' . $bytes / 4, $data)) : $data;
};
$endian = (mb_strtolower(substr(dechex(current($read(1))), -8), 'UTF-8') == "950412de") ? FALSE : TRUE; /* Is file endian */
$total = current($read(4, 8)); /* Count of translated strings */
$originalIndex = $read(8 * $total, current($read(4, 12))); /* Array contain binary position of each original string */
$translationIndex = $read(8 * $total, current($read(4, 16))); /* Array contain binary position of each translated string */
for ($i = 0; $i < $total; ++$i) {
$temp = ($originalIndex[$i * 2 + 1] != 0) ? array_reverse(explode(iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x04)), $read($originalIndex[$i * 2 + 1], $originalIndex[$i * 2 + 2], FALSE))) : array('0' => '');
$context = isset($temp[1]) ? $temp[1] : '';
$original = explode(iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00)), $temp[0]);
$translation = ($translationIndex[$i * 2 + 1] != 0) ? explode(iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00)), $translation = $read($translationIndex[$i * 2 + 1], $translationIndex[$i * 2 + 2], FALSE)) : NULL;
if ($original[0] != "") {
$this->addOriginal($original, $context, $path, $translation);
} else {
$this->setHeaders($this->parseMoMetadata(current($translation)), $path);
}
}
fclose($fp);
return $this;
}
/**
* Gettext .mo file metadata parser
* @author Pavel Železný <[email protected]>
* @param string $input
* @return array
*/
private function parseMoMetadata($input) {
$input = preg_split('/[\n,]+/', trim($input));
$output = array();
foreach ($input as $metadata) {
$tmp = preg_split("(: )", $metadata);
$output[trim($tmp[0])] = count($tmp) > 2 ? ltrim(strstr($metadata, ': '), ': ') : $tmp[1];
}
return $output;
}
/**
* Save dictionary data into gettext .po file
* @author Pavel Železný <[email protected]>
* @param string $path Gettext .po file path
* @param string $identifier Optional dictionary name
* @return void
* @throws \InvalidArgumentException
*/
private function generatePoFile($path, $identifier = NULL) {
$fp = fopen($path, 'w');
fwrite($fp, $this->encodeGettxtPoBlock('', '', implode($this->generateHeaders($identifier))));
foreach ($this->getTranslations($identifier !== NULL ? $identifier : $this->getDictionaryFileId($identifier)) as $translation) {
foreach ($translation as $context => $object) {
fwrite($fp, $this->encodeGettxtPoBlock($object->getOriginal(), $context, $object->getTranslations(), $object->getComments()));
}
}
fclose($fp);
}
/**
* Encode one translation to .po gettext file
* @author Pavel Železný <[email protected]>
* @param array $original Original untranslated string
* @param string $context Context of translation
* @param array $translations Translation strings
* @param array $comments Comments
* @return string
*/
private function encodeGettxtPoBlock($original, $context = '', $translations = NULL, $comments = array()) {
$original = (array) $original;
$translations = (array) $translations;
$translationsCount = count($translations);
$block = '';
foreach ($comments as $type => $comment) {
switch ($type) {
case 'comment':
$block .= '# ' . $comment . "\n";
break;
case 'extracted-comment':
$block .= '#. ' . $comment . "\n";
break;
case 'reference':
$block .= '#: ' . $comment . "\n";
break;
case 'flag':
$block .= '#, ' . $comment . "\n";
break;
case 'previous-context':
$block .= '#| msgctxt ' . $comment . "\n";
break;
case 'previous-untranslated-string':
$block .= '#| msgid ' . $comment . "\n";
break;
}
}
$block .= $context != '' ? 'msgctxt "' . $context . '"' . "\n" : '';
$block .= 'msgid "' . current($original) . '"' . "\n";
if (count($original) > 1) {
$block .= 'msgid_plural "' . end($original) . '"' . "\n";
}
foreach ($translations as $key => $translation) {
if (strpos(trim($translation), "\n") > 0) {
$translation = '"' . "\n" . '"' . str_replace("\n", '\n"' . "\n" . '"', trim($translation)) . '\n';
}
$block .= 'msgstr' . ($translationsCount > 1 ? '[' . $key . ']' : '') . ' "' . $translation . '"' . "\n";
}
$block .= "\n";
return $block;
}
/**
* Save dictionary data into gettext .mo file
* @author Don't know who is first autor but my source is https://github.com/marten-cz/NetteTranslator
* @param string $path Gettext .mo file path
* @param string $identifier Optional dictionary name
* @return void
* @throws \InvalidArgumentException
*/
private function generateMoFile($path, $identifier = NULL) {
$metadata = implode($this->generateHeaders($identifier));
$identifier = $this->getDictionaryFileId($identifier);
$items = $this->getTranslationsCount($identifier) + 1;
$strings = $metadata . iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00));
$idsOffsets = array(0, 28 + $items * 16);
$stringsOffsets = array(array(0, strlen($metadata)));
$ids = '';
foreach ($this->getTranslations($identifier !== NULL ? $identifier : $this->getDictionaryFileId($identifier)) as $translation) {
foreach ($translation as $context => $object) {
$original = $object->getOriginal();
$id = $context != '' ? $context . iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x04)) . $original[0] : $original[0];
if (count($original) > 1) {
$id .= iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00)) . end($original);
}
$string = implode(iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00)), $object->getTranslations());
$idsOffsets[] = strlen($id);
$idsOffsets[] = strlen($ids) + 28 + $items * 16;
$stringsOffsets[] = array(strlen($strings), strlen($string));
$ids .= $id . iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00));
$strings .= $string . iconv('UTF-32BE', 'UTF-8' . '//IGNORE', pack('N', 0x00));
}
}
$valuesOffsets = array();
foreach ($stringsOffsets as $offset) {
list ($all, $one) = $offset;
$valuesOffsets[] = $one;
$valuesOffsets[] = $all + strlen($ids) + 28 + $items * 16;
}
$offsets = array_merge($idsOffsets, $valuesOffsets);
$mo = pack('Iiiiiii', 0x950412de, 0, $items, 28, 28 + $items * 8, 0, 28 + $items * 16);
foreach ($offsets as $offset)
$mo .= pack('i', $offset);
file_put_contents($path, $mo . $ids . $strings);
}
/**
* Return headers to store in file
* @author Pavel Železný <[email protected]>
* @param string $identifier Optional dictionary name
* @return array
* @throws \InvalidArgumentException
*/
private function generateHeaders($identifier = NULL) {
$headers = array();
foreach ($this->getHeaders($this->getDictionaryFileId($identifier)) as $key => $val) {
$headers[] = $key . ': ' . $val . "\n";
}
return $headers;
}
/**
* Return dictionary headers
* @author Pavel Železný <[email protected]>
* @param string $identifier Optional dictionary name
* @return array
* @throws \InvalidArgumentException
*/
public function getHeaders($identifier = NULL) {
if (($identifier !== NULL) && (($this->getDictionaryFileId($identifier) === FALSE) || (isset($this->headers[$this->getDictionaryFileId($identifier)]) === FALSE))) {
throw new InvalidArgumentException('Required dictionary identifier is not exist.');
}
$output = array();
foreach (array_keys($this->headers) as $fileId) {
$output[$fileId] = array_merge($this->defaultHeaders, $this->headers[$fileId]);
}
return $identifier === NULL ? $output : $output[$this->getDictionaryFileId($identifier)];
}
/**
* Return dictionary translation objects
* @author Pavel Železný <[email protected]>
* @param string $identifier Optional dictionary name
* @return array
* @throws \InvalidArgumentException
*/
public function getTranslations($identifier = NULL) {
if (($identifier !== NULL) && ($this->getDictionaryFileId($identifier) === FALSE)) {
throw new InvalidArgumentException('Required dictionary identifier is not exist.');
}
if ($identifier === NULL) {
return $this->translations;
} else {
$output = array();
foreach ($this->translations as $original => $contexts) {
foreach ($contexts as $context => $fileIds) {
foreach ($fileIds as $fileId => $translation) {
if ($fileId === $this->getDictionaryFileId($identifier)) {
$output[$original][$context] = $translation;
}
}
}
}
return $output;
}
}
/**
* How many translation objects are in dictionary
* Each context variant is counted as one
* @author Pavel Železný <[email protected]>
* @param string $identifier Optional dictionary name
* @return int
* @throws \InvalidArgumentException
*/
private function getTranslationsCount($identifier = NULL) {
if (($identifier !== NULL) && ($this->getDictionaryFileId($identifier) === FALSE)) {
throw new InvalidArgumentException('Required dictionary identifier is not exist.');
}
$count = 0;
foreach ($this->getTranslations() as $translation) {
foreach ($translation as $context) {
foreach (array_keys($context) as $fileId) {
if (($identifier === NULL) || ($fileId == $this->getDictionaryFileId($identifier))) {
$count = $count + 1;
}
}
}
}
return $count;
}
/**
* Set default dictionary headers
* @author Pavel Železný <[email protected]>
* @param array $headers
* @return \GettextTranslation provides a fluent interface
*/
public function setDefaultHeaders($headers) {
foreach ($headers as $index => $value) {
$this->setdefaultHeader($index, $value);
}
return $this;
}
/**
* Set default dictionary header
* @author Pavel Železný <[email protected]>
* @param string $index
* @param string $value
* @return \GettextTranslation provides a fluent interface
*/
public function setDefaultHeader($index, $value) {
$this->defaultHeaders[$index] = $value;
return $this;
}
/**
* Set dictionary headers
* @author Pavel Železný <[email protected]>
* @param array $headers
* @param string $identifier Optional dictionary name
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setHeaders($headers, $identifier = NULL) {
foreach ($headers as $index => $value) {
$this->setHeader($index, $value, $identifier);
}
return $this;
}
/**
* Set dictionary header
* @author Pavel Železný <[email protected]>
* @param string $index
* @param string $value
* @param string $identifier Optional dictionary name
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setHeader($index, $value, $identifier = NULL) {
if (($identifier !== NULL) && ($this->getDictionaryFileId($identifier) === FALSE)) {
throw new InvalidArgumentException('Required dictionary headers are not exist.');
}
$this->headers[$this->getDictionaryFileId($identifier)][$index] = $value;
return $this;
}
/**
* Add translation object to dictionary
* @author Pavel Železný <[email protected]>
* @param string|array $original
* @param string $context
* @param string $identifier Optional dictionary name
* @param string|array $translation
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function addOriginal($original, $context = '', $identifier = NULL, $translation = NULL) {
$identifier = $this->getDictionaryFileId($identifier);
if (((is_array($translation) === TRUE) && (count($translation) > 1)) && ((count($original) < 2) || (is_array($original) === FALSE))) {
throw new \InvalidArgumentException('Translation with plurals need to have plural definition.');
} elseif (trim(is_array($original) ? $original[0] : $original) == '') {
throw new \InvalidArgumentException('Untranslated string cannot be empty.');
} elseif (is_string($context) === FALSE) {
throw new \InvalidArgumentException('Context have to be string.');
} elseif (isset($this->translations[is_array($original) ? $original[0] : $original][$context][$this->generateDictionaryFileIdentifier($identifier)])) {
throw new \InvalidArgumentException('Same defined original is exist.');
} elseif ($identifier === FALSE) {
throw new \InvalidArgumentException('Required dictionary source is not exist.');
}
return $this->translations[is_array($original) ? $original[0] : $original][$context][$this->generateDictionaryFileIdentifier($identifier)] = new GettextTranslation($original, $context, $translation);
}
/**
* Get translation object from dictionary
* @author Pavel Železný <[email protected]>
* @param string|array $original
* @param string $context
* @param string $identifier Optional dictionary name
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function getOriginal($original, $context = '', $identifier = NULL) {
$identifier = $this->getDictionaryFileId($identifier);
if (!isset($this->translations[is_array($original) ? $original[0] : $original][$context][$identifier])) {
throw new \InvalidArgumentException('Required original is not exist.');
} elseif ($identifier === FALSE) {
throw new \InvalidArgumentException('Required dictionary source is not exist.');
}
return $this->translations[is_array($original) ? $original[0] : $original][$context][$identifier];
}
/**
* Alias to getOriginal Method
* @author Pavel Železný <[email protected]>
* @param string|array $original
* @param string $context
* @param string $identifier Optional dictionary name
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function getTranslation($original, $context = '', $identifier = NULL) {
return $this->getOriginal($original, $context, $identifier);
}
/**
* Remove unwanted translation
* @author Pavel Železný <[email protected]>
* @param string $original
* @param string $context if NULL, remove whole translation otherwise remove defined context
* @param string $identifier Optional dictionary name
* @return void
*/
public function removeTranslation($original, $context = NULL, $identifier = NULL) {
$identifier = $this->getDictionaryFileId($identifier);
if (($context != NULL) && (isset($this->translations[$original][$context][$identifier]))) {
unset($this->translations[$original][$context][$identifier]);
} elseif (($context === NULL) && (isset($this->translations[$original]))) {
foreach ($this->translations[$original] as $context => $fileIdentifier) {
if ($fileIdentifier === $identifier) {
unset($this->translations[$original][$context][$fileIdentifier]);
}
}
}
}
/**
* Return dictionary file internal id if exist, FALSE otherwise
* @author Pavel Železný <[email protected]>
* @param string $fileDefinition Can be used fullpath, filename or defined identifier
* @return int | FALSE
* @throws \InvalidArgumentException
*/
public function getDictionaryFileId($fileDefinition) {
if ($fileDefinition === '') {
return '';
} elseif ($fileDefinition === NULL) {
return (count($this->files) == 1) ? 0 : '';
} else {
$output = FALSE;
foreach ($this->files as $internalId => $file) {
if (($file['identifier'] === $fileDefinition) || ($internalId === $fileDefinition)) {
return $internalId;
} elseif ((($file['mobileObject'] === TRUE) && (($file['filename'] . '.mo' == $fileDefinition) || ($file['path'] . DIRECTORY_SEPARATOR . $file['filename'] . '.mo' == $fileDefinition))) ||
(($file['portableObject'] === TRUE) && (($file['filename'] . '.po' == $fileDefinition) || ($file['path'] . DIRECTORY_SEPARATOR . $file['filename'] . '.po' == $fileDefinition)))) {
if ($output === FALSE) {
$output = $internalId;
} else {
throw new \InvalidArgumentException('More than one file with same definition was found.');
}
}
}
return $output;
}
}
/**
* Set info about loaded file into register
* @author Pavel Železný <[email protected]>
* @param string $path file path of dictionary
* @param string $identifier optionaly specified identifier
* @return void
* @throws \InvalidArgumentException
*/
private function setDictionaryFileId($path, $identifier = NULL) {
$path = realpath($path);
$type = pathinfo($path, PATHINFO_EXTENSION);
$internalId = $this->getDictionaryFileId($identifier !== NULL ? $identifier : $path);
if ($internalId === FALSE) {
$this->files[] = array(
'identifier' => $identifier !== NULL ? $identifier : $this->generateDictionaryFileIdentifier($path),
'path' => dirname($path),
'filename' => basename($path, '.' . $type),
'portableObject' => ($type == 'po') ? TRUE : FALSE,
'mobileObject' => ($type == 'mo') ? TRUE : FALSE,
);
} elseif ($type == 'mo') {
$this->files[$internalId]['mobileObject'] = TRUE;
} elseif ($type == 'po') {
$this->files[$internalId]['portableObject'] = TRUE;
}
}
/**
* Generate dictionary file identifier automaticaly
* @author Pavel Železný <[email protected]>
* @return string
* @throws \InvalidArgumentException
*/
private function generateDictionaryFileIdentifier($path) {
$type = pathinfo($path, PATHINFO_EXTENSION);
$identifier = strtolower(basename($path, '.' . $type));
$possibleId = $this->getDictionaryFileId($identifier);
if (($possibleId === FALSE) || (($path === '') && ($possibleId === '')) || (($type == 'mo') && ($this->files[$possibleId]['mobileObject'] === FALSE) && ($this->colaborativeMode === TRUE)) || (($type == 'po') && ($this->files[$possibleId]['mobileObject'] === FALSE) && ($this->colaborativeMode === TRUE))) {
return $identifier;
} else {
throw new \InvalidArgumentException('Unable to generate unique file identifier.');
}
}
}