Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / translation / Loader / QtFileLoader.php
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
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 (1 == $nodes->length) {
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 }