|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gettext\Extractors; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
use Gettext\Translation; |
|
7
|
|
|
use SimpleXMLElement; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class to get gettext strings from xliff format. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Xliff extends Extractor implements ExtractorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function fromString($string, Translations $translations, array $options = []) |
|
18
|
|
|
{ |
|
19
|
|
|
$xml = new SimpleXMLElement($string, null, false); |
|
20
|
|
|
|
|
21
|
|
|
foreach ($xml->file as $file) { |
|
22
|
|
|
if (isset($file->notes)) { |
|
23
|
|
|
foreach ($file->notes->note as $note) { |
|
24
|
|
|
$translations->setHeader($note['id'], (string) $note); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
foreach ($file->unit as $unit) { |
|
29
|
|
|
foreach ($unit->segment as $segment) { |
|
30
|
|
|
$targets = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($segment->target as $target) { |
|
33
|
|
|
$targets[] = (string) $target; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$translation = new Translation(null, (string) $segment->source); |
|
38
|
|
|
$translation->setTranslation(array_shift($targets)); |
|
39
|
|
|
$translation->setPluralTranslations($targets); |
|
40
|
|
|
|
|
41
|
|
|
if (isset($unit->notes)) { |
|
42
|
|
|
foreach ($unit->notes->note as $note) { |
|
43
|
|
|
switch ($note['category']) { |
|
44
|
|
|
case 'context': |
|
45
|
|
|
$translation = $translation->getClone((string) $note); |
|
46
|
|
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'extracted-comment': |
|
49
|
|
|
$translation->addExtractedComment((string) $note); |
|
50
|
|
|
break; |
|
51
|
|
|
|
|
52
|
|
|
case 'flag': |
|
53
|
|
|
$translation->addFlag((string) $note); |
|
54
|
|
|
break; |
|
55
|
|
|
|
|
56
|
|
|
case 'reference': |
|
57
|
|
|
$ref = explode(':', (string) $note, 2); |
|
58
|
|
|
$translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null); |
|
59
|
|
|
break; |
|
60
|
|
|
|
|
61
|
|
|
default: |
|
62
|
|
|
$translation->addComment((string) $note); |
|
63
|
|
|
break; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$translations[] = $translation; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $translations; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|