Drupal investigation

XliffFileDumper.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 format(MessageCatalogue $messages, $domain)
  45. {
  46. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.', E_USER_DEPRECATED);
  47. return $this->formatCatalogue($messages, $domain);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function getExtension()
  53. {
  54. return 'xlf';
  55. }
  56. private function dumpXliff1($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
  57. {
  58. $toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
  59. if (array_key_exists('tool_info', $options)) {
  60. $toolInfo = array_merge($toolInfo, $options['tool_info']);
  61. }
  62. $dom = new \DOMDocument('1.0', 'utf-8');
  63. $dom->formatOutput = true;
  64. $xliff = $dom->appendChild($dom->createElement('xliff'));
  65. $xliff->setAttribute('version', '1.2');
  66. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
  67. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  68. $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
  69. $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
  70. $xliffFile->setAttribute('datatype', 'plaintext');
  71. $xliffFile->setAttribute('original', 'file.ext');
  72. $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
  73. $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
  74. foreach ($toolInfo as $id => $value) {
  75. $xliffTool->setAttribute($id, $value);
  76. }
  77. $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
  78. foreach ($messages->all($domain) as $source => $target) {
  79. $translation = $dom->createElement('trans-unit');
  80. $translation->setAttribute('id', md5($source));
  81. $translation->setAttribute('resname', $source);
  82. $s = $translation->appendChild($dom->createElement('source'));
  83. $s->appendChild($dom->createTextNode($source));
  84. // Does the target contain characters requiring a CDATA section?
  85. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  86. $targetElement = $dom->createElement('target');
  87. $metadata = $messages->getMetadata($source, $domain);
  88. if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
  89. foreach ($metadata['target-attributes'] as $name => $value) {
  90. $targetElement->setAttribute($name, $value);
  91. }
  92. }
  93. $t = $translation->appendChild($targetElement);
  94. $t->appendChild($text);
  95. if ($this->hasMetadataArrayInfo('notes', $metadata)) {
  96. foreach ($metadata['notes'] as $note) {
  97. if (!isset($note['content'])) {
  98. continue;
  99. }
  100. $n = $translation->appendChild($dom->createElement('note'));
  101. $n->appendChild($dom->createTextNode($note['content']));
  102. if (isset($note['priority'])) {
  103. $n->setAttribute('priority', $note['priority']);
  104. }
  105. if (isset($note['from'])) {
  106. $n->setAttribute('from', $note['from']);
  107. }
  108. }
  109. }
  110. $xliffBody->appendChild($translation);
  111. }
  112. return $dom->saveXML();
  113. }
  114. private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
  115. {
  116. $dom = new \DOMDocument('1.0', 'utf-8');
  117. $dom->formatOutput = true;
  118. $xliff = $dom->appendChild($dom->createElement('xliff'));
  119. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
  120. $xliff->setAttribute('version', '2.0');
  121. $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
  122. $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
  123. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  124. $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
  125. foreach ($messages->all($domain) as $source => $target) {
  126. $translation = $dom->createElement('unit');
  127. $translation->setAttribute('id', md5($source));
  128. $segment = $translation->appendChild($dom->createElement('segment'));
  129. $s = $segment->appendChild($dom->createElement('source'));
  130. $s->appendChild($dom->createTextNode($source));
  131. // Does the target contain characters requiring a CDATA section?
  132. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  133. $targetElement = $dom->createElement('target');
  134. $metadata = $messages->getMetadata($source, $domain);
  135. if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
  136. foreach ($metadata['target-attributes'] as $name => $value) {
  137. $targetElement->setAttribute($name, $value);
  138. }
  139. }
  140. $t = $segment->appendChild($targetElement);
  141. $t->appendChild($text);
  142. $xliffFile->appendChild($translation);
  143. }
  144. return $dom->saveXML();
  145. }
  146. /**
  147. * @param string $key
  148. * @param array|null $metadata
  149. *
  150. * @return bool
  151. */
  152. private function hasMetadataArrayInfo($key, $metadata = null)
  153. {
  154. return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || is_array($metadata[$key]));
  155. }
  156. }