XliffFileDumper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\Dumper;
  11. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. /**
  14. * XliffFileDumper generates xliff files from a message catalogue.
  15. *
  16. * @author Michel Salib <michelsalib@hotmail.com>
  17. */
  18. class XliffFileDumper extends FileDumper
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
  24. {
  25. $xliffVersion = '1.2';
  26. if (\array_key_exists('xliff_version', $options)) {
  27. $xliffVersion = $options['xliff_version'];
  28. }
  29. if (\array_key_exists('default_locale', $options)) {
  30. $defaultLocale = $options['default_locale'];
  31. } else {
  32. $defaultLocale = \Locale::getDefault();
  33. }
  34. if ('1.2' === $xliffVersion) {
  35. return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
  36. }
  37. if ('2.0' === $xliffVersion) {
  38. return $this->dumpXliff2($defaultLocale, $messages, $domain);
  39. }
  40. throw new InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function getExtension()
  46. {
  47. return 'xlf';
  48. }
  49. private function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, ?string $domain, array $options = [])
  50. {
  51. $toolInfo = ['tool-id' => 'symfony', 'tool-name' => 'Symfony'];
  52. if (\array_key_exists('tool_info', $options)) {
  53. $toolInfo = array_merge($toolInfo, $options['tool_info']);
  54. }
  55. $dom = new \DOMDocument('1.0', 'utf-8');
  56. $dom->formatOutput = true;
  57. $xliff = $dom->appendChild($dom->createElement('xliff'));
  58. $xliff->setAttribute('version', '1.2');
  59. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
  60. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  61. $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
  62. $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
  63. $xliffFile->setAttribute('datatype', 'plaintext');
  64. $xliffFile->setAttribute('original', 'file.ext');
  65. $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
  66. $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
  67. foreach ($toolInfo as $id => $value) {
  68. $xliffTool->setAttribute($id, $value);
  69. }
  70. $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
  71. foreach ($messages->all($domain) as $source => $target) {
  72. $translation = $dom->createElement('trans-unit');
  73. $translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
  74. $translation->setAttribute('resname', $source);
  75. $s = $translation->appendChild($dom->createElement('source'));
  76. $s->appendChild($dom->createTextNode($source));
  77. // Does the target contain characters requiring a CDATA section?
  78. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  79. $targetElement = $dom->createElement('target');
  80. $metadata = $messages->getMetadata($source, $domain);
  81. if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
  82. foreach ($metadata['target-attributes'] as $name => $value) {
  83. $targetElement->setAttribute($name, $value);
  84. }
  85. }
  86. $t = $translation->appendChild($targetElement);
  87. $t->appendChild($text);
  88. if ($this->hasMetadataArrayInfo('notes', $metadata)) {
  89. foreach ($metadata['notes'] as $note) {
  90. if (!isset($note['content'])) {
  91. continue;
  92. }
  93. $n = $translation->appendChild($dom->createElement('note'));
  94. $n->appendChild($dom->createTextNode($note['content']));
  95. if (isset($note['priority'])) {
  96. $n->setAttribute('priority', $note['priority']);
  97. }
  98. if (isset($note['from'])) {
  99. $n->setAttribute('from', $note['from']);
  100. }
  101. }
  102. }
  103. $xliffBody->appendChild($translation);
  104. }
  105. return $dom->saveXML();
  106. }
  107. private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?string $domain)
  108. {
  109. $dom = new \DOMDocument('1.0', 'utf-8');
  110. $dom->formatOutput = true;
  111. $xliff = $dom->appendChild($dom->createElement('xliff'));
  112. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
  113. $xliff->setAttribute('version', '2.0');
  114. $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
  115. $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
  116. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  117. if (MessageCatalogue::INTL_DOMAIN_SUFFIX === substr($domain, -($suffixLength = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)))) {
  118. $xliffFile->setAttribute('id', substr($domain, 0, -$suffixLength).'.'.$messages->getLocale());
  119. } else {
  120. $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
  121. }
  122. foreach ($messages->all($domain) as $source => $target) {
  123. $translation = $dom->createElement('unit');
  124. $translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
  125. $name = $source;
  126. if (\strlen($source) > 80) {
  127. $name = substr(md5($source), -7);
  128. }
  129. $translation->setAttribute('name', $name);
  130. $metadata = $messages->getMetadata($source, $domain);
  131. // Add notes section
  132. if ($this->hasMetadataArrayInfo('notes', $metadata)) {
  133. $notesElement = $dom->createElement('notes');
  134. foreach ($metadata['notes'] as $note) {
  135. $n = $dom->createElement('note');
  136. $n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : ''));
  137. unset($note['content']);
  138. foreach ($note as $name => $value) {
  139. $n->setAttribute($name, $value);
  140. }
  141. $notesElement->appendChild($n);
  142. }
  143. $translation->appendChild($notesElement);
  144. }
  145. $segment = $translation->appendChild($dom->createElement('segment'));
  146. $s = $segment->appendChild($dom->createElement('source'));
  147. $s->appendChild($dom->createTextNode($source));
  148. // Does the target contain characters requiring a CDATA section?
  149. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  150. $targetElement = $dom->createElement('target');
  151. if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
  152. foreach ($metadata['target-attributes'] as $name => $value) {
  153. $targetElement->setAttribute($name, $value);
  154. }
  155. }
  156. $t = $segment->appendChild($targetElement);
  157. $t->appendChild($text);
  158. $xliffFile->appendChild($translation);
  159. }
  160. return $dom->saveXML();
  161. }
  162. private function hasMetadataArrayInfo(string $key, array $metadata = null): bool
  163. {
  164. return null !== $metadata && \array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || \is_array($metadata[$key]));
  165. }
  166. }