Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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