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@0
|
14 use Symfony\Component\Config\Util\XmlUtils;
|
Chris@0
|
15 use Symfony\Component\Translation\MessageCatalogue;
|
Chris@0
|
16 use Symfony\Component\Translation\Exception\InvalidResourceException;
|
Chris@0
|
17 use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
Chris@0
|
18 use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
Chris@0
|
19 use Symfony\Component\Config\Resource\FileResource;
|
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@0
|
94 $target = $this->utf8ToCharset((string) (isset($translation->target) ? $translation->target : $source), $encoding);
|
Chris@0
|
95
|
Chris@0
|
96 $catalogue->set((string) $source, $target, $domain);
|
Chris@0
|
97
|
Chris@0
|
98 $metadata = array();
|
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@0
|
104 $metadata['target-attributes'] = array();
|
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@0
|
130 foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
|
Chris@0
|
131 $source = $segment->source;
|
Chris@0
|
132
|
Chris@0
|
133 // If the xlf file has another encoding specified, try to convert it because
|
Chris@0
|
134 // simple_xml will always return utf-8 encoded values
|
Chris@0
|
135 $target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
|
Chris@0
|
136
|
Chris@0
|
137 $catalogue->set((string) $source, $target, $domain);
|
Chris@0
|
138
|
Chris@0
|
139 $metadata = array();
|
Chris@0
|
140 if (isset($segment->target) && $segment->target->attributes()) {
|
Chris@0
|
141 $metadata['target-attributes'] = array();
|
Chris@0
|
142 foreach ($segment->target->attributes() as $key => $value) {
|
Chris@0
|
143 $metadata['target-attributes'][$key] = (string) $value;
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 $catalogue->setMetadata((string) $source, $metadata, $domain);
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Convert a UTF8 string to the specified encoding.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @param string $content String to decode
|
Chris@0
|
155 * @param string $encoding Target encoding
|
Chris@0
|
156 *
|
Chris@0
|
157 * @return string
|
Chris@0
|
158 */
|
Chris@0
|
159 private function utf8ToCharset($content, $encoding = null)
|
Chris@0
|
160 {
|
Chris@0
|
161 if ('UTF-8' !== $encoding && !empty($encoding)) {
|
Chris@0
|
162 return mb_convert_encoding($content, $encoding, 'UTF-8');
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 return $content;
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * Validates and parses the given file into a DOMDocument.
|
Chris@0
|
170 *
|
Chris@0
|
171 * @param string $file
|
Chris@0
|
172 * @param \DOMDocument $dom
|
Chris@0
|
173 * @param string $schema source of the schema
|
Chris@0
|
174 *
|
Chris@0
|
175 * @throws InvalidResourceException
|
Chris@0
|
176 */
|
Chris@0
|
177 private function validateSchema($file, \DOMDocument $dom, $schema)
|
Chris@0
|
178 {
|
Chris@0
|
179 $internalErrors = libxml_use_internal_errors(true);
|
Chris@0
|
180
|
Chris@0
|
181 $disableEntities = libxml_disable_entity_loader(false);
|
Chris@0
|
182
|
Chris@0
|
183 if (!@$dom->schemaValidateSource($schema)) {
|
Chris@0
|
184 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
185
|
Chris@0
|
186 throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $file, implode("\n", $this->getXmlErrors($internalErrors))));
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
190
|
Chris@0
|
191 $dom->normalizeDocument();
|
Chris@0
|
192
|
Chris@0
|
193 libxml_clear_errors();
|
Chris@0
|
194 libxml_use_internal_errors($internalErrors);
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 private function getSchema($xliffVersion)
|
Chris@0
|
198 {
|
Chris@0
|
199 if ('1.2' === $xliffVersion) {
|
Chris@0
|
200 $schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
|
Chris@0
|
201 $xmlUri = 'http://www.w3.org/2001/xml.xsd';
|
Chris@0
|
202 } elseif ('2.0' === $xliffVersion) {
|
Chris@0
|
203 $schemaSource = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-2.0.xsd');
|
Chris@0
|
204 $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd';
|
Chris@0
|
205 } else {
|
Chris@0
|
206 throw new InvalidArgumentException(sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion));
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 return $this->fixXmlLocation($schemaSource, $xmlUri);
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Internally changes the URI of a dependent xsd to be loaded locally.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @param string $schemaSource Current content of schema file
|
Chris@0
|
216 * @param string $xmlUri External URI of XML to convert to local
|
Chris@0
|
217 *
|
Chris@0
|
218 * @return string
|
Chris@0
|
219 */
|
Chris@0
|
220 private function fixXmlLocation($schemaSource, $xmlUri)
|
Chris@0
|
221 {
|
Chris@0
|
222 $newPath = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
|
Chris@0
|
223 $parts = explode('/', $newPath);
|
Chris@0
|
224 if (0 === stripos($newPath, 'phar://')) {
|
Chris@0
|
225 $tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
|
Chris@0
|
226 if ($tmpfile) {
|
Chris@0
|
227 copy($newPath, $tmpfile);
|
Chris@0
|
228 $parts = explode('/', str_replace('\\', '/', $tmpfile));
|
Chris@0
|
229 }
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
Chris@0
|
233 $newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
|
Chris@0
|
234
|
Chris@0
|
235 return str_replace($xmlUri, $newPath, $schemaSource);
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 /**
|
Chris@0
|
239 * Returns the XML errors of the internal XML parser.
|
Chris@0
|
240 *
|
Chris@0
|
241 * @param bool $internalErrors
|
Chris@0
|
242 *
|
Chris@0
|
243 * @return array An array of errors
|
Chris@0
|
244 */
|
Chris@0
|
245 private function getXmlErrors($internalErrors)
|
Chris@0
|
246 {
|
Chris@0
|
247 $errors = array();
|
Chris@0
|
248 foreach (libxml_get_errors() as $error) {
|
Chris@0
|
249 $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
|
Chris@0
|
250 LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
|
Chris@0
|
251 $error->code,
|
Chris@0
|
252 trim($error->message),
|
Chris@0
|
253 $error->file ?: 'n/a',
|
Chris@0
|
254 $error->line,
|
Chris@0
|
255 $error->column
|
Chris@0
|
256 );
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 libxml_clear_errors();
|
Chris@0
|
260 libxml_use_internal_errors($internalErrors);
|
Chris@0
|
261
|
Chris@0
|
262 return $errors;
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * Gets xliff file version based on the root "version" attribute.
|
Chris@0
|
267 * Defaults to 1.2 for backwards compatibility.
|
Chris@0
|
268 *
|
Chris@0
|
269 * @param \DOMDocument $dom
|
Chris@0
|
270 *
|
Chris@0
|
271 * @throws InvalidArgumentException
|
Chris@0
|
272 *
|
Chris@0
|
273 * @return string
|
Chris@0
|
274 */
|
Chris@0
|
275 private function getVersionNumber(\DOMDocument $dom)
|
Chris@0
|
276 {
|
Chris@0
|
277 /** @var \DOMNode $xliff */
|
Chris@0
|
278 foreach ($dom->getElementsByTagName('xliff') as $xliff) {
|
Chris@0
|
279 $version = $xliff->attributes->getNamedItem('version');
|
Chris@0
|
280 if ($version) {
|
Chris@0
|
281 return $version->nodeValue;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 $namespace = $xliff->attributes->getNamedItem('xmlns');
|
Chris@0
|
285 if ($namespace) {
|
Chris@0
|
286 if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
|
Chris@0
|
287 throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 return substr($namespace, 34);
|
Chris@0
|
291 }
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 // Falls back to v1.2
|
Chris@0
|
295 return '1.2';
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 /**
|
Chris@0
|
299 * @param \SimpleXMLElement|null $noteElement
|
Chris@0
|
300 * @param string|null $encoding
|
Chris@0
|
301 *
|
Chris@0
|
302 * @return array
|
Chris@0
|
303 */
|
Chris@0
|
304 private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, $encoding = null)
|
Chris@0
|
305 {
|
Chris@0
|
306 $notes = array();
|
Chris@0
|
307
|
Chris@0
|
308 if (null === $noteElement) {
|
Chris@0
|
309 return $notes;
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 /** @var \SimpleXMLElement $xmlNote */
|
Chris@0
|
313 foreach ($noteElement as $xmlNote) {
|
Chris@0
|
314 $noteAttributes = $xmlNote->attributes();
|
Chris@0
|
315 $note = array('content' => $this->utf8ToCharset((string) $xmlNote, $encoding));
|
Chris@0
|
316 if (isset($noteAttributes['priority'])) {
|
Chris@0
|
317 $note['priority'] = (int) $noteAttributes['priority'];
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 if (isset($noteAttributes['from'])) {
|
Chris@0
|
321 $note['from'] = (string) $noteAttributes['from'];
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 $notes[] = $note;
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 return $notes;
|
Chris@0
|
328 }
|
Chris@0
|
329 }
|