PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.28.0
Independent Analytics – WordPress Analytics Plugin v1.28.0
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 / Loader / XliffFileLoader.php
independent-analytics / vendor / symfony / translation / Loader Last commit date
ArrayLoader.php 3 years ago CsvFileLoader.php 3 years ago FileLoader.php 3 years ago IcuDatFileLoader.php 3 years ago IcuResFileLoader.php 3 years ago IniFileLoader.php 3 years ago JsonFileLoader.php 3 years ago LoaderInterface.php 3 years ago MoFileLoader.php 3 years ago PhpFileLoader.php 3 years ago PoFileLoader.php 3 years ago QtFileLoader.php 3 years ago XliffFileLoader.php 3 years ago YamlFileLoader.php 3 years ago
XliffFileLoader.php
186 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 IAWP_SCOPED\Symfony\Component\Translation\Loader;
12
13 use IAWP_SCOPED\Symfony\Component\Config\Resource\FileResource;
14 use IAWP_SCOPED\Symfony\Component\Config\Util\Exception\InvalidXmlException;
15 use IAWP_SCOPED\Symfony\Component\Config\Util\Exception\XmlParsingException;
16 use IAWP_SCOPED\Symfony\Component\Config\Util\XmlUtils;
17 use IAWP_SCOPED\Symfony\Component\Translation\Exception\InvalidResourceException;
18 use IAWP_SCOPED\Symfony\Component\Translation\Exception\NotFoundResourceException;
19 use IAWP_SCOPED\Symfony\Component\Translation\Exception\RuntimeException;
20 use IAWP_SCOPED\Symfony\Component\Translation\MessageCatalogue;
21 use IAWP_SCOPED\Symfony\Component\Translation\Util\XliffUtils;
22 /**
23 * XliffFileLoader loads translations from XLIFF files.
24 *
25 * @author Fabien Potencier <fabien@symfony.com>
26 */
27 class XliffFileLoader implements LoaderInterface
28 {
29 /**
30 * {@inheritdoc}
31 */
32 public function load($resource, string $locale, string $domain = 'messages')
33 {
34 if (!\class_exists(XmlUtils::class)) {
35 throw new RuntimeException('Loading translations from the Xliff format requires the Symfony Config component.');
36 }
37 if (!$this->isXmlString($resource)) {
38 if (!\stream_is_local($resource)) {
39 throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource));
40 }
41 if (!\file_exists($resource)) {
42 throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource));
43 }
44 if (!\is_file($resource)) {
45 throw new InvalidResourceException(\sprintf('This is neither a file nor an XLIFF string "%s".', $resource));
46 }
47 }
48 try {
49 if ($this->isXmlString($resource)) {
50 $dom = XmlUtils::parse($resource);
51 } else {
52 $dom = XmlUtils::loadFile($resource);
53 }
54 } catch (\InvalidArgumentException|XmlParsingException|InvalidXmlException $e) {
55 throw new InvalidResourceException(\sprintf('Unable to load "%s": ', $resource) . $e->getMessage(), $e->getCode(), $e);
56 }
57 if ($errors = XliffUtils::validateSchema($dom)) {
58 throw new InvalidResourceException(\sprintf('Invalid resource provided: "%s"; Errors: ', $resource) . XliffUtils::getErrorsAsString($errors));
59 }
60 $catalogue = new MessageCatalogue($locale);
61 $this->extract($dom, $catalogue, $domain);
62 if (\is_file($resource) && \class_exists(FileResource::class)) {
63 $catalogue->addResource(new FileResource($resource));
64 }
65 return $catalogue;
66 }
67 private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
68 {
69 $xliffVersion = XliffUtils::getVersionNumber($dom);
70 if ('1.2' === $xliffVersion) {
71 $this->extractXliff1($dom, $catalogue, $domain);
72 }
73 if ('2.0' === $xliffVersion) {
74 $this->extractXliff2($dom, $catalogue, $domain);
75 }
76 }
77 /**
78 * Extract messages and metadata from DOMDocument into a MessageCatalogue.
79 */
80 private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
81 {
82 $xml = \simplexml_import_dom($dom);
83 $encoding = $dom->encoding ? \strtoupper($dom->encoding) : null;
84 $namespace = 'urn:oasis:names:tc:xliff:document:1.2';
85 $xml->registerXPathNamespace('xliff', $namespace);
86 foreach ($xml->xpath('//xliff:file') as $file) {
87 $fileAttributes = $file->attributes();
88 $file->registerXPathNamespace('xliff', $namespace);
89 foreach ($file->xpath('.//xliff:trans-unit') as $translation) {
90 $attributes = $translation->attributes();
91 if (!(isset($attributes['resname']) || isset($translation->source))) {
92 continue;
93 }
94 $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
95 // If the xlf file has another encoding specified, try to convert it because
96 // simple_xml will always return utf-8 encoded values
97 $target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding);
98 $catalogue->set((string) $source, $target, $domain);
99 $metadata = ['source' => (string) $translation->source, 'file' => ['original' => (string) $fileAttributes['original']]];
100 if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
101 $metadata['notes'] = $notes;
102 }
103 if (isset($translation->target) && $translation->target->attributes()) {
104 $metadata['target-attributes'] = [];
105 foreach ($translation->target->attributes() as $key => $value) {
106 $metadata['target-attributes'][$key] = (string) $value;
107 }
108 }
109 if (isset($attributes['id'])) {
110 $metadata['id'] = (string) $attributes['id'];
111 }
112 $catalogue->setMetadata((string) $source, $metadata, $domain);
113 }
114 }
115 }
116 private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
117 {
118 $xml = \simplexml_import_dom($dom);
119 $encoding = $dom->encoding ? \strtoupper($dom->encoding) : null;
120 $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
121 foreach ($xml->xpath('//xliff:unit') as $unit) {
122 foreach ($unit->segment as $segment) {
123 $attributes = $unit->attributes();
124 $source = $attributes['name'] ?? $segment->source;
125 // If the xlf file has another encoding specified, try to convert it because
126 // simple_xml will always return utf-8 encoded values
127 $target = $this->utf8ToCharset((string) ($segment->target ?? $segment->source), $encoding);
128 $catalogue->set((string) $source, $target, $domain);
129 $metadata = [];
130 if (isset($segment->target) && $segment->target->attributes()) {
131 $metadata['target-attributes'] = [];
132 foreach ($segment->target->attributes() as $key => $value) {
133 $metadata['target-attributes'][$key] = (string) $value;
134 }
135 }
136 if (isset($unit->notes)) {
137 $metadata['notes'] = [];
138 foreach ($unit->notes->note as $noteNode) {
139 $note = [];
140 foreach ($noteNode->attributes() as $key => $value) {
141 $note[$key] = (string) $value;
142 }
143 $note['content'] = (string) $noteNode;
144 $metadata['notes'][] = $note;
145 }
146 }
147 $catalogue->setMetadata((string) $source, $metadata, $domain);
148 }
149 }
150 }
151 /**
152 * Convert a UTF8 string to the specified encoding.
153 */
154 private function utf8ToCharset(string $content, string $encoding = null) : string
155 {
156 if ('UTF-8' !== $encoding && !empty($encoding)) {
157 return \mb_convert_encoding($content, $encoding, 'UTF-8');
158 }
159 return $content;
160 }
161 private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, string $encoding = null) : array
162 {
163 $notes = [];
164 if (null === $noteElement) {
165 return $notes;
166 }
167 /** @var \SimpleXMLElement $xmlNote */
168 foreach ($noteElement as $xmlNote) {
169 $noteAttributes = $xmlNote->attributes();
170 $note = ['content' => $this->utf8ToCharset((string) $xmlNote, $encoding)];
171 if (isset($noteAttributes['priority'])) {
172 $note['priority'] = (int) $noteAttributes['priority'];
173 }
174 if (isset($noteAttributes['from'])) {
175 $note['from'] = (string) $noteAttributes['from'];
176 }
177 $notes[] = $note;
178 }
179 return $notes;
180 }
181 private function isXmlString(string $resource) : bool
182 {
183 return 0 === \strpos($resource, '<?xml');
184 }
185 }
186