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
|
|
|
if ($value !== '') { |
30
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($notes->hasChildNodes()) { |
35
|
|
|
$file->appendChild($notes); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach ($translations as $translation) { |
39
|
|
|
$unit = $dom->createElement('unit'); |
40
|
|
|
$unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal())); |
41
|
|
|
|
42
|
|
|
//Save comments as notes |
43
|
|
|
$notes = $dom->createElement('notes'); |
44
|
|
|
|
45
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext()))->setAttribute('category', 'context'); |
46
|
|
|
|
47
|
|
|
foreach ($translation->getComments() as $comment) { |
48
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $comment))->setAttribute('category', 'comment'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($translation->getExtractedComments() as $comment) { |
52
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $comment))->setAttribute('category', 'extracted-comment'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($translation->getFlags() as $flag) { |
56
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $flag))->setAttribute('category', 'flag'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($translation->getReferences() as $reference) { |
60
|
|
|
$notes->appendChild(self::createTextNode($dom, 'note', $reference[0].':'.$reference[1]))->setAttribute('category', 'reference'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$unit->appendChild($notes); |
64
|
|
|
|
65
|
|
|
$segment = $unit->appendChild($dom->createElement('segment')); |
66
|
|
|
$segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal())); |
67
|
|
|
$segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation())); |
68
|
|
|
|
69
|
|
|
foreach ($translation->getPluralTranslations() as $plural) { |
70
|
|
|
$segment->appendChild(self::createTextNode($dom, 'target', $plural)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$file->appendChild($unit); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $dom->saveXML(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private static function createTextNode(DOMDocument $dom, $name, $string) |
80
|
|
|
{ |
81
|
|
|
$node = $dom->createElement($name); |
82
|
|
|
$text = (preg_match('/[&<>]/', $string) === 1) ? $dom->createCDATASection($string) : $dom->createTextNode($string); |
83
|
|
|
$node->appendChild($text); |
84
|
|
|
|
85
|
|
|
return $node; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|