PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / translation / Dumper / XliffFileDumper.php
independent-analytics / vendor / symfony / translation / Dumper Last commit date
CsvFileDumper.php 1 month ago DumperInterface.php 2 years ago FileDumper.php 1 month ago IcuResFileDumper.php 1 month ago IniFileDumper.php 1 month ago JsonFileDumper.php 1 month ago MoFileDumper.php 1 month ago PhpFileDumper.php 1 month ago PoFileDumper.php 1 month ago QtFileDumper.php 1 month ago XliffFileDumper.php 1 month ago YamlFileDumper.php 1 month ago
XliffFileDumper.php
168 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace IAWPSCOPED\Symfony\Component\Translation\Dumper;
12
13 use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidArgumentException;
14 use IAWPSCOPED\Symfony\Component\Translation\MessageCatalogue;
15 /**
16 * XliffFileDumper generates xliff files from a message catalogue.
17 *
18 * @author Michel Salib <michelsalib@hotmail.com>
19 * @internal
20 */
21 class XliffFileDumper extends FileDumper
22 {
23 /**
24 * {@inheritdoc}
25 */
26 public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []) : string
27 {
28 $xliffVersion = '1.2';
29 if (\array_key_exists('xliff_version', $options)) {
30 $xliffVersion = $options['xliff_version'];
31 }
32 if (\array_key_exists('default_locale', $options)) {
33 $defaultLocale = $options['default_locale'];
34 } else {
35 $defaultLocale = \Locale::getDefault();
36 }
37 if ('1.2' === $xliffVersion) {
38 return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
39 }
40 if ('2.0' === $xliffVersion) {
41 return $this->dumpXliff2($defaultLocale, $messages, $domain);
42 }
43 throw new InvalidArgumentException(\sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
44 }
45 /**
46 * {@inheritdoc}
47 */
48 protected function getExtension() : string
49 {
50 return 'xlf';
51 }
52 private function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, ?string $domain, array $options = [])
53 {
54 $toolInfo = ['tool-id' => 'symfony', 'tool-name' => 'Symfony'];
55 if (\array_key_exists('tool_info', $options)) {
56 $toolInfo = \array_merge($toolInfo, $options['tool_info']);
57 }
58 $dom = new \DOMDocument('1.0', 'utf-8');
59 $dom->formatOutput = \true;
60 $xliff = $dom->appendChild($dom->createElement('xliff'));
61 $xliff->setAttribute('version', '1.2');
62 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
63 $xliffFile = $xliff->appendChild($dom->createElement('file'));
64 $xliffFile->setAttribute('source-language', \str_replace('_', '-', $defaultLocale));
65 $xliffFile->setAttribute('target-language', \str_replace('_', '-', $messages->getLocale()));
66 $xliffFile->setAttribute('datatype', 'plaintext');
67 $xliffFile->setAttribute('original', 'file.ext');
68 $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
69 $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
70 foreach ($toolInfo as $id => $value) {
71 $xliffTool->setAttribute($id, $value);
72 }
73 $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
74 foreach ($messages->all($domain) as $source => $target) {
75 $translation = $dom->createElement('trans-unit');
76 $translation->setAttribute('id', \strtr(\substr(\base64_encode(\hash('sha256', $source, \true)), 0, 7), '/+', '._'));
77 $translation->setAttribute('resname', $source);
78 $s = $translation->appendChild($dom->createElement('source'));
79 $s->appendChild($dom->createTextNode($source));
80 // Does the target contain characters requiring a CDATA section?
81 $text = 1 === \preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
82 $targetElement = $dom->createElement('target');
83 $metadata = $messages->getMetadata($source, $domain);
84 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
85 foreach ($metadata['target-attributes'] as $name => $value) {
86 $targetElement->setAttribute($name, $value);
87 }
88 }
89 $t = $translation->appendChild($targetElement);
90 $t->appendChild($text);
91 if ($this->hasMetadataArrayInfo('notes', $metadata)) {
92 foreach ($metadata['notes'] as $note) {
93 if (!isset($note['content'])) {
94 continue;
95 }
96 $n = $translation->appendChild($dom->createElement('note'));
97 $n->appendChild($dom->createTextNode($note['content']));
98 if (isset($note['priority'])) {
99 $n->setAttribute('priority', $note['priority']);
100 }
101 if (isset($note['from'])) {
102 $n->setAttribute('from', $note['from']);
103 }
104 }
105 }
106 $xliffBody->appendChild($translation);
107 }
108 return $dom->saveXML();
109 }
110 private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?string $domain)
111 {
112 $dom = new \DOMDocument('1.0', 'utf-8');
113 $dom->formatOutput = \true;
114 $xliff = $dom->appendChild($dom->createElement('xliff'));
115 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
116 $xliff->setAttribute('version', '2.0');
117 $xliff->setAttribute('srcLang', \str_replace('_', '-', $defaultLocale));
118 $xliff->setAttribute('trgLang', \str_replace('_', '-', $messages->getLocale()));
119 $xliffFile = $xliff->appendChild($dom->createElement('file'));
120 if (\str_ends_with($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
121 $xliffFile->setAttribute('id', \substr($domain, 0, -\strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)) . '.' . $messages->getLocale());
122 } else {
123 $xliffFile->setAttribute('id', $domain . '.' . $messages->getLocale());
124 }
125 foreach ($messages->all($domain) as $source => $target) {
126 $translation = $dom->createElement('unit');
127 $translation->setAttribute('id', \strtr(\substr(\base64_encode(\hash('sha256', $source, \true)), 0, 7), '/+', '._'));
128 if (\strlen($source) <= 80) {
129 $translation->setAttribute('name', $source);
130 }
131 $metadata = $messages->getMetadata($source, $domain);
132 // Add notes section
133 if ($this->hasMetadataArrayInfo('notes', $metadata)) {
134 $notesElement = $dom->createElement('notes');
135 foreach ($metadata['notes'] as $note) {
136 $n = $dom->createElement('note');
137 $n->appendChild($dom->createTextNode($note['content'] ?? ''));
138 unset($note['content']);
139 foreach ($note as $name => $value) {
140 $n->setAttribute($name, $value);
141 }
142 $notesElement->appendChild($n);
143 }
144 $translation->appendChild($notesElement);
145 }
146 $segment = $translation->appendChild($dom->createElement('segment'));
147 $s = $segment->appendChild($dom->createElement('source'));
148 $s->appendChild($dom->createTextNode($source));
149 // Does the target contain characters requiring a CDATA section?
150 $text = 1 === \preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
151 $targetElement = $dom->createElement('target');
152 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
153 foreach ($metadata['target-attributes'] as $name => $value) {
154 $targetElement->setAttribute($name, $value);
155 }
156 }
157 $t = $segment->appendChild($targetElement);
158 $t->appendChild($text);
159 $xliffFile->appendChild($translation);
160 }
161 return $dom->saveXML();
162 }
163 private function hasMetadataArrayInfo(string $key, array $metadata = null) : bool
164 {
165 return \is_iterable($metadata[$key] ?? null);
166 }
167 }
168