4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader
;
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
;
20 * QtFileLoader loads translations from QT Translations XML files.
22 * @author Benjamin Eberlei <kontakt@beberlei.de>
26 class QtFileLoader
implements LoaderInterface
33 public function load($resource, $locale, $domain = 'messages')
35 if (!stream_is_local($resource)) {
36 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
39 if (!file_exists($resource)) {
40 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
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()));
49 $xpath = new \
DOMXPath($dom);
50 $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]');
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) {
57 (string) $translation->getElementsByTagName('source')->item(0)->nodeValue
,
58 (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue
,
61 $translation = $translation->nextSibling
;
63 $catalogue->addResource(new FileResource($resource));
66 libxml_use_internal_errors($current);
72 * Returns the XML errors of the internal XML parser
74 * @return array An array of errors
76 private function getXmlErrors()
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',
83 trim($error->message
),
84 $error->file
? $error->file
: 'n/a',
90 libxml_clear_errors();
91 libxml_use_internal_errors(false);