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
|
<?php
namespace Gettext\Generators;
use Gettext\Translations; use DOMDocument;
class Xliff extends Generator implements GeneratorInterface { /** * {@inheritdoc} */ public static function toString(Translations $translations, array $options = []) { $dom = new DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); $xliff->setAttribute('version', '2.0'); $xliff->setAttribute('srcLang', $translations->getLanguage()); $xliff->setAttribute('trgLang', $translations->getLanguage()); $file = $xliff->appendChild($dom->createElement('file')); $file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage());
//Save headers as notes $notes = $dom->createElement('notes');
foreach ($translations->getHeaders() as $name => $value) { $notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name); }
if ($notes->hasChildNodes()) { $file->appendChild($notes); }
foreach ($translations as $translation) { $unit = $dom->createElement('unit'); $unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal()));
//Save comments as notes $notes = $dom->createElement('notes');
$notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext())) ->setAttribute('category', 'context');
foreach ($translation->getComments() as $comment) { $notes->appendChild(self::createTextNode($dom, 'note', $comment)) ->setAttribute('category', 'comment'); }
foreach ($translation->getExtractedComments() as $comment) { $notes->appendChild(self::createTextNode($dom, 'note', $comment)) ->setAttribute('category', 'extracted-comment'); }
foreach ($translation->getFlags() as $flag) { $notes->appendChild(self::createTextNode($dom, 'note', $flag)) ->setAttribute('category', 'flag'); }
foreach ($translation->getReferences() as $reference) { $notes->appendChild(self::createTextNode($dom, 'note', $reference[0].':'.$reference[1])) ->setAttribute('category', 'reference'); }
$unit->appendChild($notes);
$segment = $unit->appendChild($dom->createElement('segment')); $segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal())); $segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation()));
foreach ($translation->getPluralTranslations() as $plural) { if ($plural !== '') { $segment->appendChild(self::createTextNode($dom, 'target', $plural)); } }
$file->appendChild($unit); }
return $dom->saveXML(); }
private static function createTextNode(DOMDocument $dom, $name, $string) { $node = $dom->createElement($name); $text = (preg_match('/[&<>]/', $string) === 1) ? $dom->createCDATASection($string) : $dom->createTextNode($string); $node->appendChild($text);
return $node; } }
|