菜谱项目

XliffFileDumper.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  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, $domain, array $options = array())
  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, $options);
  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($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
  50. {
  51. $toolInfo = array('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', md5($source));
  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($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
  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. $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
  118. foreach ($messages->all($domain) as $source => $target) {
  119. $translation = $dom->createElement('unit');
  120. $translation->setAttribute('id', md5($source));
  121. $segment = $translation->appendChild($dom->createElement('segment'));
  122. $s = $segment->appendChild($dom->createElement('source'));
  123. $s->appendChild($dom->createTextNode($source));
  124. // Does the target contain characters requiring a CDATA section?
  125. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  126. $targetElement = $dom->createElement('target');
  127. $metadata = $messages->getMetadata($source, $domain);
  128. if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
  129. foreach ($metadata['target-attributes'] as $name => $value) {
  130. $targetElement->setAttribute($name, $value);
  131. }
  132. }
  133. $t = $segment->appendChild($targetElement);
  134. $t->appendChild($text);
  135. $xliffFile->appendChild($translation);
  136. }
  137. return $dom->saveXML();
  138. }
  139. /**
  140. * @param string $key
  141. * @param array|null $metadata
  142. *
  143. * @return bool
  144. */
  145. private function hasMetadataArrayInfo($key, $metadata = null)
  146. {
  147. return null !== $metadata && array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || is_array($metadata[$key]));
  148. }
  149. }