XliffFileLoader.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Config\Resource\FileResource;
  13. /**
  14. * XliffFileLoader loads translations from XLIFF files.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @api
  19. */
  20. class XliffFileLoader implements LoaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @api
  26. */
  27. public function load($resource, $locale, $domain = 'messages')
  28. {
  29. if (!stream_is_local($resource)) {
  30. throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
  31. }
  32. $xml = $this->parseFile($resource);
  33. $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
  34. $catalogue = new MessageCatalogue($locale);
  35. foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
  36. $catalogue->set((string) $translation->source, (string) $translation->target, $domain);
  37. }
  38. $catalogue->addResource(new FileResource($resource));
  39. return $catalogue;
  40. }
  41. /**
  42. * Validates and parses the given file into a SimpleXMLElement
  43. *
  44. * @param string $file
  45. *
  46. * @return SimpleXMLElement
  47. */
  48. private function parseFile($file)
  49. {
  50. $internalErrors = libxml_use_internal_errors(true);
  51. libxml_clear_errors();
  52. $dom = new \DOMDocument();
  53. $dom->validateOnParse = true;
  54. if (!@$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
  55. throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
  56. }
  57. $location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
  58. $parts = explode('/', $location);
  59. if (0 === stripos($location, 'phar://')) {
  60. $tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
  61. if ($tmpfile) {
  62. copy($location, $tmpfile);
  63. $parts = explode('/', str_replace('\\', '/', $tmpfile));
  64. }
  65. }
  66. $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
  67. $location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
  68. $source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
  69. $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
  70. if (!@$dom->schemaValidateSource($source)) {
  71. throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
  72. }
  73. $dom->normalizeDocument();
  74. libxml_use_internal_errors($internalErrors);
  75. return simplexml_import_dom($dom);
  76. }
  77. /**
  78. * Returns the XML errors of the internal XML parser
  79. *
  80. * @return array An array of errors
  81. */
  82. private function getXmlErrors($internalErrors)
  83. {
  84. $errors = array();
  85. foreach (libxml_get_errors() as $error) {
  86. $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
  87. LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
  88. $error->code,
  89. trim($error->message),
  90. $error->file ? $error->file : 'n/a',
  91. $error->line,
  92. $error->column
  93. );
  94. }
  95. libxml_clear_errors();
  96. libxml_use_internal_errors($internalErrors);
  97. return $errors;
  98. }
  99. }