]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/translation/Symfony/Component/Translation/Loader/QtFileLoader.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / translation / Symfony / Component / 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\Translation\MessageCatalogue;
15 use Symfony\Component\Translation\Exception\InvalidResourceException;
16 use Symfony\Component\Translation\Exception\NotFoundResourceException;
17 use Symfony\Component\Config\Resource\FileResource;
18
19 /**
20 * QtFileLoader loads translations from QT Translations XML files.
21 *
22 * @author Benjamin Eberlei <kontakt@beberlei.de>
23 *
24 * @api
25 */
26 class QtFileLoader implements LoaderInterface
27 {
28 /**
29 * {@inheritdoc}
30 *
31 * @api
32 */
33 public function load($resource, $locale, $domain = 'messages')
34 {
35 if (!stream_is_local($resource)) {
36 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
37 }
38
39 if (!file_exists($resource)) {
40 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
41 }
42
43 $dom = new \DOMDocument();
44 $current = libxml_use_internal_errors(true);
45 if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
46 throw new InvalidResourceException(implode("\n", $this->getXmlErrors()));
47 }
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 $catalogue->set(
57 (string) $translation->getElementsByTagName('source')->item(0)->nodeValue,
58 (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue,
59 $domain
60 );
61 $translation = $translation->nextSibling;
62 }
63 $catalogue->addResource(new FileResource($resource));
64 }
65
66 libxml_use_internal_errors($current);
67
68 return $catalogue;
69 }
70
71 /**
72 * Returns the XML errors of the internal XML parser
73 *
74 * @return array An array of errors
75 */
76 private function getXmlErrors()
77 {
78 $errors = array();
79 foreach (libxml_get_errors() as $error) {
80 $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
81 LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
82 $error->code,
83 trim($error->message),
84 $error->file ? $error->file : 'n/a',
85 $error->line,
86 $error->column
87 );
88 }
89
90 libxml_clear_errors();
91 libxml_use_internal_errors(false);
92
93 return $errors;
94 }
95 }