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