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