No Description

QtFileLoader.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Config\Util\XmlUtils;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. use Symfony\Component\Translation\Exception\InvalidResourceException;
  14. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  15. use Symfony\Component\Config\Resource\FileResource;
  16. /**
  17. * QtFileLoader loads translations from QT Translations XML files.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. *
  21. * @api
  22. */
  23. class QtFileLoader implements LoaderInterface
  24. {
  25. /**
  26. * {@inheritdoc}
  27. *
  28. * @api
  29. */
  30. public function load($resource, $locale, $domain = 'messages')
  31. {
  32. if (!stream_is_local($resource)) {
  33. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  34. }
  35. if (!file_exists($resource)) {
  36. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  37. }
  38. try {
  39. $dom = XmlUtils::loadFile($resource);
  40. } catch (\InvalidArgumentException $e) {
  41. throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e);
  42. }
  43. $internalErrors = libxml_use_internal_errors(true);
  44. libxml_clear_errors();
  45. $xpath = new \DOMXPath($dom);
  46. $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]');
  47. $catalogue = new MessageCatalogue($locale);
  48. if ($nodes->length == 1) {
  49. $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message');
  50. foreach ($translations as $translation) {
  51. $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue;
  52. if (!empty($translationValue)) {
  53. $catalogue->set(
  54. (string) $translation->getElementsByTagName('source')->item(0)->nodeValue,
  55. $translationValue,
  56. $domain
  57. );
  58. }
  59. $translation = $translation->nextSibling;
  60. }
  61. $catalogue->addResource(new FileResource($resource));
  62. }
  63. libxml_use_internal_errors($internalErrors);
  64. return $catalogue;
  65. }
  66. }