No Description

XliffFileLoader.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * XliffFileLoader loads translations from XLIFF files.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @api
  22. */
  23. class XliffFileLoader 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. list($xml, $encoding) = $this->parseFile($resource);
  39. $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
  40. $catalogue = new MessageCatalogue($locale);
  41. foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
  42. $attributes = $translation->attributes();
  43. if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
  44. continue;
  45. }
  46. $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
  47. // If the xlf file has another encoding specified, try to convert it because
  48. // simple_xml will always return utf-8 encoded values
  49. $target = $this->utf8ToCharset((string) $translation->target, $encoding);
  50. $catalogue->set((string) $source, $target, $domain);
  51. if (isset($translation->note)) {
  52. $notes = array();
  53. foreach ($translation->note as $xmlNote) {
  54. $noteAttributes = $xmlNote->attributes();
  55. $note = array('content' => $this->utf8ToCharset((string) $xmlNote, $encoding));
  56. if (isset($noteAttributes['priority'])) {
  57. $note['priority'] = (int) $noteAttributes['priority'];
  58. }
  59. if (isset($noteAttributes['from'])) {
  60. $note['from'] = (string) $noteAttributes['from'];
  61. }
  62. $notes[] = $note;
  63. }
  64. $catalogue->setMetadata((string) $source, array('notes' => $notes), $domain);
  65. }
  66. }
  67. $catalogue->addResource(new FileResource($resource));
  68. return $catalogue;
  69. }
  70. /**
  71. * Convert a UTF8 string to the specified encoding.
  72. *
  73. * @param string $content String to decode
  74. * @param string $encoding Target encoding
  75. *
  76. * @return string
  77. */
  78. private function utf8ToCharset($content, $encoding = null)
  79. {
  80. if ('UTF-8' !== $encoding && !empty($encoding)) {
  81. if (function_exists('mb_convert_encoding')) {
  82. return mb_convert_encoding($content, $encoding, 'UTF-8');
  83. }
  84. if (function_exists('iconv')) {
  85. return iconv('UTF-8', $encoding, $content);
  86. }
  87. throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
  88. }
  89. return $content;
  90. }
  91. /**
  92. * Validates and parses the given file into a SimpleXMLElement.
  93. *
  94. * @param string $file
  95. *
  96. * @throws \RuntimeException
  97. *
  98. * @return \SimpleXMLElement
  99. *
  100. * @throws InvalidResourceException
  101. */
  102. private function parseFile($file)
  103. {
  104. try {
  105. $dom = XmlUtils::loadFile($file);
  106. } catch (\InvalidArgumentException $e) {
  107. throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $file, $e->getMessage()), $e->getCode(), $e);
  108. }
  109. $internalErrors = libxml_use_internal_errors(true);
  110. $location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
  111. $parts = explode('/', $location);
  112. if (0 === stripos($location, 'phar://')) {
  113. $tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
  114. if ($tmpfile) {
  115. copy($location, $tmpfile);
  116. $parts = explode('/', str_replace('\\', '/', $tmpfile));
  117. }
  118. }
  119. $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
  120. $location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
  121. $source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
  122. $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
  123. if (!@$dom->schemaValidateSource($source)) {
  124. throw new InvalidResourceException(implode("\n", $this->getXmlErrors($internalErrors)));
  125. }
  126. $dom->normalizeDocument();
  127. libxml_clear_errors();
  128. libxml_use_internal_errors($internalErrors);
  129. return array(simplexml_import_dom($dom), strtoupper($dom->encoding));
  130. }
  131. /**
  132. * Returns the XML errors of the internal XML parser.
  133. *
  134. * @param bool $internalErrors
  135. *
  136. * @return array An array of errors
  137. */
  138. private function getXmlErrors($internalErrors)
  139. {
  140. $errors = array();
  141. foreach (libxml_get_errors() as $error) {
  142. $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
  143. LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
  144. $error->code,
  145. trim($error->message),
  146. $error->file ? $error->file : 'n/a',
  147. $error->line,
  148. $error->column
  149. );
  150. }
  151. libxml_clear_errors();
  152. libxml_use_internal_errors($internalErrors);
  153. return $errors;
  154. }
  155. }