QtFileLoader.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Translation\MessageCatalogue;
  12. use Symfony\Component\Translation\Exception\InvalidResourceException;
  13. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. /**
  16. * QtFileLoader loads translations from QT Translations XML files.
  17. *
  18. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19. *
  20. * @api
  21. */
  22. class QtFileLoader implements LoaderInterface
  23. {
  24. /**
  25. * {@inheritdoc}
  26. *
  27. * @api
  28. */
  29. public function load($resource, $locale, $domain = 'messages')
  30. {
  31. if (!stream_is_local($resource)) {
  32. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  33. }
  34. if (!file_exists($resource)) {
  35. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  36. }
  37. $dom = new \DOMDocument();
  38. $current = libxml_use_internal_errors(true);
  39. if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
  40. throw new InvalidResourceException(implode("\n", $this->getXmlErrors()));
  41. }
  42. $xpath = new \DOMXPath($dom);
  43. $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]');
  44. $catalogue = new MessageCatalogue($locale);
  45. if ($nodes->length == 1) {
  46. $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message');
  47. foreach ($translations as $translation) {
  48. $catalogue->set(
  49. (string) $translation->getElementsByTagName('source')->item(0)->nodeValue,
  50. (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue,
  51. $domain
  52. );
  53. $translation = $translation->nextSibling;
  54. }
  55. $catalogue->addResource(new FileResource($resource));
  56. }
  57. libxml_use_internal_errors($current);
  58. return $catalogue;
  59. }
  60. /**
  61. * Returns the XML errors of the internal XML parser
  62. *
  63. * @return array An array of errors
  64. */
  65. private function getXmlErrors()
  66. {
  67. $errors = array();
  68. foreach (libxml_get_errors() as $error) {
  69. $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
  70. LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
  71. $error->code,
  72. trim($error->message),
  73. $error->file ? $error->file : 'n/a',
  74. $error->line,
  75. $error->column
  76. );
  77. }
  78. libxml_clear_errors();
  79. libxml_use_internal_errors(false);
  80. return $errors;
  81. }
  82. }