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