comparison vendor/symfony/translation/Loader/QtFileLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\Util\XmlUtils;
15 use Symfony\Component\Translation\MessageCatalogue;
16 use Symfony\Component\Translation\Exception\InvalidResourceException;
17 use Symfony\Component\Translation\Exception\NotFoundResourceException;
18 use Symfony\Component\Config\Resource\FileResource;
19
20 /**
21 * QtFileLoader loads translations from QT Translations XML files.
22 *
23 * @author Benjamin Eberlei <kontakt@beberlei.de>
24 */
25 class QtFileLoader implements LoaderInterface
26 {
27 /**
28 * {@inheritdoc}
29 */
30 public function load($resource, $locale, $domain = 'messages')
31 {
32 if (!stream_is_local($resource)) {
33 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
34 }
35
36 if (!file_exists($resource)) {
37 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
38 }
39
40 try {
41 $dom = XmlUtils::loadFile($resource);
42 } catch (\InvalidArgumentException $e) {
43 throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e);
44 }
45
46 $internalErrors = libxml_use_internal_errors(true);
47 libxml_clear_errors();
48
49 $xpath = new \DOMXPath($dom);
50 $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]');
51
52 $catalogue = new MessageCatalogue($locale);
53 if ($nodes->length == 1) {
54 $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message');
55 foreach ($translations as $translation) {
56 $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue;
57
58 if (!empty($translationValue)) {
59 $catalogue->set(
60 (string) $translation->getElementsByTagName('source')->item(0)->nodeValue,
61 $translationValue,
62 $domain
63 );
64 }
65 $translation = $translation->nextSibling;
66 }
67
68 if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
69 $catalogue->addResource(new FileResource($resource));
70 }
71 }
72
73 libxml_use_internal_errors($internalErrors);
74
75 return $catalogue;
76 }
77 }