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\Config\Util\XmlUtils;
15 use Symfony\Component\Translation\MessageCatalogue;
16 use Symfony\Component\Translation\Exception\InvalidResourceException;
17 use Symfony\Component\Translation\Exception\NotFoundResourceException;
18 use Symfony\Component\Config\Resource\FileResource;
21 * QtFileLoader loads translations from QT Translations XML files.
23 * @author Benjamin Eberlei <kontakt@beberlei.de>
25 class QtFileLoader implements LoaderInterface
30 public function load($resource, $locale, $domain = 'messages')
32 if (!stream_is_local($resource)) {
33 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
36 if (!file_exists($resource)) {
37 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
41 $dom = XmlUtils::loadFile($resource);
42 } catch (\InvalidArgumentException $e) {
43 throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e);
46 $internalErrors = libxml_use_internal_errors(true);
47 libxml_clear_errors();
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) {
56 $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue;
58 if (!empty($translationValue)) {
60 (string) $translation->getElementsByTagName('source')->item(0)->nodeValue,
65 $translation = $translation->nextSibling;
68 if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
69 $catalogue->addResource(new FileResource($resource));
73 libxml_use_internal_errors($internalErrors);