|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gettext\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
use DOMDocument; |
|
7
|
|
|
|
|
8
|
|
|
class Xliff extends Generator implements GeneratorInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* {@inheritdoc} |
|
12
|
|
|
*/ |
|
13
|
|
|
public static function toString(Translations $translations, array $options = []) |
|
14
|
|
|
{ |
|
15
|
|
|
$dom = new DOMDocument('1.0', 'utf-8'); |
|
16
|
|
|
$dom->formatOutput = true; |
|
17
|
|
|
$xliff = $dom->appendChild($dom->createElement('xliff')); |
|
18
|
|
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
|
19
|
|
|
$xliff->setAttribute('version', '2.0'); |
|
20
|
|
|
$xliff->setAttribute('srcLang', $translations->getLanguage()); |
|
21
|
|
|
$xliff->setAttribute('trgLang', $translations->getLanguage()); |
|
22
|
|
|
$file = $xliff->appendChild($dom->createElement('file')); |
|
23
|
|
|
$file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage()); |
|
24
|
|
|
|
|
25
|
|
|
//Save headers as notes |
|
26
|
|
|
$notes = $dom->createElement('notes'); |
|
27
|
|
|
|
|
28
|
|
|
foreach ($translations->getHeaders() as $name => $value) { |
|
29
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($notes->hasChildNodes()) { |
|
33
|
|
|
$file->appendChild($notes); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
foreach ($translations as $translation) { |
|
37
|
|
|
//Find an XLIFF unit ID, if one is available; otherwise generate |
|
38
|
|
|
$unitId = md5($translation->getContext().$translation->getOriginal()); |
|
39
|
|
|
foreach ($translation->getComments() as $comment) { |
|
40
|
|
|
if (!preg_match('/XLIFF_UNIT_ID: (.*)/', $comment, $matches)) { |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
$unitId = $matches[1]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$unit = $dom->createElement('unit'); |
|
47
|
|
|
$unit->setAttribute('id', $unitId); |
|
48
|
|
|
|
|
49
|
|
|
//Save comments as notes |
|
50
|
|
|
$notes = $dom->createElement('notes'); |
|
51
|
|
|
|
|
52
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext())) |
|
53
|
|
|
->setAttribute('category', 'context'); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($translation->getComments() as $comment) { |
|
56
|
|
|
//Skip XLIFF unit ID comments. |
|
57
|
|
|
if (preg_match('/XLIFF_UNIT_ID: (.*)/', $comment)) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $comment)) |
|
62
|
|
|
->setAttribute('category', 'comment'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($translation->getExtractedComments() as $comment) { |
|
66
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $comment)) |
|
67
|
|
|
->setAttribute('category', 'extracted-comment'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($translation->getFlags() as $flag) { |
|
71
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $flag)) |
|
72
|
|
|
->setAttribute('category', 'flag'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
foreach ($translation->getReferences() as $reference) { |
|
76
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $reference[0].':'.$reference[1])) |
|
77
|
|
|
->setAttribute('category', 'reference'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$unit->appendChild($notes); |
|
81
|
|
|
|
|
82
|
|
|
$segment = $unit->appendChild($dom->createElement('segment')); |
|
83
|
|
|
$segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal())); |
|
84
|
|
|
$segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation())); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($translation->getPluralTranslations() as $plural) { |
|
87
|
|
|
if ($plural !== '') { |
|
88
|
|
|
$segment->appendChild(self::createTextNode($dom, 'target', $plural)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$file->appendChild($unit); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $dom->saveXML(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private static function createTextNode(DOMDocument $dom, $name, $string) |
|
99
|
|
|
{ |
|
100
|
|
|
$node = $dom->createElement($name); |
|
101
|
|
|
$text = (preg_match('/[&<>]/', $string) === 1) |
|
102
|
|
|
? $dom->createCDATASection($string) |
|
103
|
|
|
: $dom->createTextNode($string); |
|
104
|
|
|
$node->appendChild($text); |
|
105
|
|
|
|
|
106
|
|
|
return $node; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|