PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / gettext / gettext / src / Generators / Xliff.php

Xliff.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at vendor/gettext/gettext/src/Generators/Xliff.php

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