XliffFileDumper.php 6.8 KB

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