Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Translation\Loader;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Config\Resource\FileResource;
|
Chris@0
|
15 use Symfony\Component\Config\Util\XmlUtils;
|
Chris@17
|
16 use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
Chris@0
|
17 use Symfony\Component\Translation\Exception\InvalidResourceException;
|
Chris@0
|
18 use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
Chris@17
|
19 use Symfony\Component\Translation\MessageCatalogue;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * XliffFileLoader loads translations from XLIFF files.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
25 */
|
Chris@0
|
26 class XliffFileLoader implements LoaderInterface
|
Chris@0
|
27 {
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 public function load($resource, $locale, $domain = 'messages')
|
Chris@0
|
32 {
|
Chris@0
|
33 if (!stream_is_local($resource)) {
|
Chris@0
|
34 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 if (!file_exists($resource)) {
|
Chris@0
|
38 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 $catalogue = new MessageCatalogue($locale);
|
Chris@0
|
42 $this->extract($resource, $catalogue, $domain);
|
Chris@0
|
43
|
Chris@0
|
44 if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
|
Chris@0
|
45 $catalogue->addResource(new FileResource($resource));
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 return $catalogue;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 private function extract($resource, MessageCatalogue $catalogue, $domain)
|
Chris@0
|
52 {
|
Chris@0
|
53 try {
|
Chris@0
|
54 $dom = XmlUtils::loadFile($resource);
|
Chris@0
|
55 } catch (\InvalidArgumentException $e) {
|
Chris@0
|
56 throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $resource, $e->getMessage()), $e->getCode(), $e);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 $xliffVersion = $this->getVersionNumber($dom);
|
Chris@0
|
60 $this->validateSchema($xliffVersion, $dom, $this->getSchema($xliffVersion));
|
Chris@0
|
61
|
Chris@0
|
62 if ('1.2' === $xliffVersion) {
|
Chris@0
|
63 $this->extractXliff1($dom, $catalogue, $domain);
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 if ('2.0' === $xliffVersion) {
|
Chris@0
|
67 $this->extractXliff2($dom, $catalogue, $domain);
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Extract messages and metadata from DOMDocument into a MessageCatalogue.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param \DOMDocument $dom Source to extract messages and metadata
|
Chris@0
|
75 * @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata
|
Chris@0
|
76 * @param string $domain The domain
|
Chris@0
|
77 */
|
Chris@0
|
78 private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
|
Chris@0
|
79 {
|
Chris@0
|
80 $xml = simplexml_import_dom($dom);
|
Chris@0
|
81 $encoding = strtoupper($dom->encoding);
|
Chris@0
|
82
|
Chris@0
|
83 $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
|
Chris@0
|
84 foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
|
Chris@0
|
85 $attributes = $translation->attributes();
|
Chris@0
|
86
|
Chris@0
|
87 if (!(isset($attributes['resname']) || isset($translation->source))) {
|
Chris@0
|
88 continue;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
|
Chris@0
|
92 // If the xlf file has another encoding specified, try to convert it because
|
Chris@0
|
93 // simple_xml will always return utf-8 encoded values
|
Chris@17
|
94 $target = $this->utf8ToCharset((string) (isset($translation->target) ? $translation->target : $translation->source), $encoding);
|
Chris@0
|
95
|
Chris@0
|
96 $catalogue->set((string) $source, $target, $domain);
|
Chris@0
|
97
|
Chris@17
|
98 $metadata = [];
|
Chris@0
|
99 if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
|
Chris@0
|
100 $metadata['notes'] = $notes;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 if (isset($translation->target) && $translation->target->attributes()) {
|
Chris@17
|
104 $metadata['target-attributes'] = [];
|
Chris@0
|
105 foreach ($translation->target->attributes() as $key => $value) {
|
Chris@0
|
106 $metadata['target-attributes'][$key] = (string) $value;
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 if (isset($attributes['id'])) {
|
Chris@0
|
111 $metadata['id'] = (string) $attributes['id'];
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 $catalogue->setMetadata((string) $source, $metadata, $domain);
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * @param \DOMDocument $dom
|
Chris@0
|
120 * @param MessageCatalogue $catalogue
|
Chris@0
|
121 * @param string $domain
|
Chris@0
|
122 */
|
Chris@0
|
123 private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
|
Chris@0
|
124 {
|
Chris@0
|
125 $xml = simplexml_import_dom($dom);
|
Chris@0
|
126 $encoding = strtoupper($dom->encoding);
|
Chris@0
|
127
|
Chris@0
|
128 $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
|
Chris@0
|
129
|
Chris@14
|
130 foreach ($xml->xpath('//xliff:unit') as $unit) {
|
Chris@14
|
131 foreach ($unit->segment as $segment) {
|
Chris@14
|
132 $source = $segment->source;
|
Chris@0
|
133
|
Chris@14
|
134 // If the xlf file has another encoding specified, try to convert it because
|
Chris@14
|
135 // simple_xml will always return utf-8 encoded values
|
Chris@14
|
136 $target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
|
Chris@0
|
137
|
Chris@14
|
138 $catalogue->set((string) $source, $target, $domain);
|
Chris@0
|
139
|
Chris@17
|
140 $metadata = [];
|
Chris@14
|
141 if (isset($segment->target) && $segment->target->attributes()) {
|
Chris@17
|
142 $metadata['target-attributes'] = [];
|
Chris@14
|
143 foreach ($segment->target->attributes() as $key => $value) {
|
Chris@14
|
144 $metadata['target-attributes'][$key] = (string) $value;
|
Chris@14
|
145 }
|
Chris@0
|
146 }
|
Chris@14
|
147
|
Chris@14
|
148 if (isset($unit->notes)) {
|
Chris@17
|
149 $metadata['notes'] = [];
|
Chris@14
|
150 foreach ($unit->notes->note as $noteNode) {
|
Chris@17
|
151 $note = [];
|
Chris@14
|
152 foreach ($noteNode->attributes() as $key => $value) {
|
Chris@14
|
153 $note[$key] = (string) $value;
|
Chris@14
|
154 }
|
Chris@14
|
155 $note['content'] = (string) $noteNode;
|
Chris@14
|
156 $metadata['notes'][] = $note;
|
Chris@14
|
157 }
|
Chris@14
|
158 }
|
Chris@14
|
159
|
Chris@14
|
160 $catalogue->setMetadata((string) $source, $metadata, $domain);
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * Convert a UTF8 string to the specified encoding.
|
Chris@0
|
167 *
|
Chris@0
|
168 * @param string $content String to decode
|
Chris@0
|
169 * @param string $encoding Target encoding
|
Chris@0
|
170 *
|
Chris@0
|
171 * @return string
|
Chris@0
|
172 */
|
Chris@0
|
173 private function utf8ToCharset($content, $encoding = null)
|
Chris@0
|
174 {
|
Chris@0
|
175 if ('UTF-8' !== $encoding && !empty($encoding)) {
|
Chris@0
|
176 return mb_convert_encoding($content, $encoding, 'UTF-8');
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 return $content;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * Validates and parses the given file into a DOMDocument.
|
Chris@0
|
184 *
|
Chris@0
|
185 * @param string $file
|
Chris@0
|
186 * @param \DOMDocument $dom
|
Chris@0
|
187 * @param string $schema source of the schema
|
Chris@0
|
188 *
|
Chris@0
|
189 * @throws InvalidResourceException
|
Chris@0
|
190 */
|
Chris@0
|
191 private function validateSchema($file, \DOMDocument $dom, $schema)
|
Chris@0
|
192 {
|
Chris@0
|
193 $internalErrors = libxml_use_internal_errors(true);
|
Chris@0
|
194
|
Chris@0
|
195 $disableEntities = libxml_disable_entity_loader(false);
|
Chris@0
|
196
|
Chris@0
|
197 if (!@$dom->schemaValidateSource($schema)) {
|
Chris@0
|
198 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
199
|
Chris@0
|
200 throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $file, implode("\n", $this->getXmlErrors($internalErrors))));
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
204
|
Chris@0
|
205 $dom->normalizeDocument();
|
Chris@0
|
206
|
Chris@0
|
207 libxml_clear_errors();
|
Chris@0
|
208 libxml_use_internal_errors($internalErrors);
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 private function getSchema($xliffVersion)
|
Chris@0
|
212 {
|
Chris@0
|
213 if ('1.2' === $xliffVersion) {
|
Chris@0
|
214 $schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
|
Chris@0
|
215 $xmlUri = 'http://www.w3.org/2001/xml.xsd';
|
Chris@0
|
216 } elseif ('2.0' === $xliffVersion) {
|
Chris@0
|
217 $schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-2.0.xsd');
|
Chris@0
|
218 $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
|
Chris@0
|
219 } else {
|
Chris@0
|
220 throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 return $this->fixXmlLocation($schemaSource, $xmlUri);
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Internally changes the URI of a dependent xsd to be loaded locally.
|
Chris@0
|
228 *
|
Chris@0
|
229 * @param string $schemaSource Current content of schema file
|
Chris@0
|
230 * @param string $xmlUri External URI of XML to convert to local
|
Chris@0
|
231 *
|
Chris@0
|
232 * @return string
|
Chris@0
|
233 */
|
Chris@0
|
234 private function fixXmlLocation($schemaSource, $xmlUri)
|
Chris@0
|
235 {
|
Chris@0
|
236 $newPath = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
|
Chris@0
|
237 $parts = explode('/', $newPath);
|
Chris@14
|
238 $locationstart = 'file:///';
|
Chris@0
|
239 if (0 === stripos($newPath, 'phar://')) {
|
Chris@14
|
240 $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
|
Chris@0
|
241 if ($tmpfile) {
|
Chris@0
|
242 copy($newPath, $tmpfile);
|
Chris@0
|
243 $parts = explode('/', str_replace('\\', '/', $tmpfile));
|
Chris@14
|
244 } else {
|
Chris@14
|
245 array_shift($parts);
|
Chris@14
|
246 $locationstart = 'phar:///';
|
Chris@0
|
247 }
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@17
|
250 $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
Chris@14
|
251 $newPath = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
|
Chris@0
|
252
|
Chris@0
|
253 return str_replace($xmlUri, $newPath, $schemaSource);
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 /**
|
Chris@0
|
257 * Returns the XML errors of the internal XML parser.
|
Chris@0
|
258 *
|
Chris@0
|
259 * @param bool $internalErrors
|
Chris@0
|
260 *
|
Chris@0
|
261 * @return array An array of errors
|
Chris@0
|
262 */
|
Chris@0
|
263 private function getXmlErrors($internalErrors)
|
Chris@0
|
264 {
|
Chris@17
|
265 $errors = [];
|
Chris@0
|
266 foreach (libxml_get_errors() as $error) {
|
Chris@0
|
267 $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
|
Chris@0
|
268 LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
|
Chris@0
|
269 $error->code,
|
Chris@0
|
270 trim($error->message),
|
Chris@0
|
271 $error->file ?: 'n/a',
|
Chris@0
|
272 $error->line,
|
Chris@0
|
273 $error->column
|
Chris@0
|
274 );
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 libxml_clear_errors();
|
Chris@0
|
278 libxml_use_internal_errors($internalErrors);
|
Chris@0
|
279
|
Chris@0
|
280 return $errors;
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Gets xliff file version based on the root "version" attribute.
|
Chris@0
|
285 * Defaults to 1.2 for backwards compatibility.
|
Chris@0
|
286 *
|
Chris@0
|
287 * @param \DOMDocument $dom
|
Chris@0
|
288 *
|
Chris@0
|
289 * @throws InvalidArgumentException
|
Chris@0
|
290 *
|
Chris@0
|
291 * @return string
|
Chris@0
|
292 */
|
Chris@0
|
293 private function getVersionNumber(\DOMDocument $dom)
|
Chris@0
|
294 {
|
Chris@0
|
295 /** @var \DOMNode $xliff */
|
Chris@0
|
296 foreach ($dom->getElementsByTagName('xliff') as $xliff) {
|
Chris@0
|
297 $version = $xliff->attributes->getNamedItem('version');
|
Chris@0
|
298 if ($version) {
|
Chris@0
|
299 return $version->nodeValue;
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 $namespace = $xliff->attributes->getNamedItem('xmlns');
|
Chris@0
|
303 if ($namespace) {
|
Chris@14
|
304 if (0 !== substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) {
|
Chris@0
|
305 throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 return substr($namespace, 34);
|
Chris@0
|
309 }
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 // Falls back to v1.2
|
Chris@0
|
313 return '1.2';
|
Chris@0
|
314 }
|
Chris@0
|
315
|
Chris@0
|
316 /**
|
Chris@0
|
317 * @param \SimpleXMLElement|null $noteElement
|
Chris@0
|
318 * @param string|null $encoding
|
Chris@0
|
319 *
|
Chris@0
|
320 * @return array
|
Chris@0
|
321 */
|
Chris@0
|
322 private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, $encoding = null)
|
Chris@0
|
323 {
|
Chris@17
|
324 $notes = [];
|
Chris@0
|
325
|
Chris@0
|
326 if (null === $noteElement) {
|
Chris@0
|
327 return $notes;
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 /** @var \SimpleXMLElement $xmlNote */
|
Chris@0
|
331 foreach ($noteElement as $xmlNote) {
|
Chris@0
|
332 $noteAttributes = $xmlNote->attributes();
|
Chris@17
|
333 $note = ['content' => $this->utf8ToCharset((string) $xmlNote, $encoding)];
|
Chris@0
|
334 if (isset($noteAttributes['priority'])) {
|
Chris@0
|
335 $note['priority'] = (int) $noteAttributes['priority'];
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 if (isset($noteAttributes['from'])) {
|
Chris@0
|
339 $note['from'] = (string) $noteAttributes['from'];
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 $notes[] = $note;
|
Chris@0
|
343 }
|
Chris@0
|
344
|
Chris@0
|
345 return $notes;
|
Chris@0
|
346 }
|
Chris@0
|
347 }
|