No Description

XliffFileDumper.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * @var string
  21. */
  22. private $defaultLocale;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function dump(MessageCatalogue $messages, $options = array())
  27. {
  28. if (array_key_exists('default_locale', $options)) {
  29. $this->defaultLocale = $options['default_locale'];
  30. } else {
  31. $this->defaultLocale = \Locale::getDefault();
  32. }
  33. parent::dump($messages, $options);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function format(MessageCatalogue $messages, $domain)
  39. {
  40. $dom = new \DOMDocument('1.0', 'utf-8');
  41. $dom->formatOutput = true;
  42. $xliff = $dom->appendChild($dom->createElement('xliff'));
  43. $xliff->setAttribute('version', '1.2');
  44. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
  45. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  46. $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
  47. $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
  48. $xliffFile->setAttribute('datatype', 'plaintext');
  49. $xliffFile->setAttribute('original', 'file.ext');
  50. $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
  51. foreach ($messages->all($domain) as $source => $target) {
  52. $translation = $dom->createElement('trans-unit');
  53. $translation->setAttribute('id', md5($source));
  54. $translation->setAttribute('resname', $source);
  55. $s = $translation->appendChild($dom->createElement('source'));
  56. $s->appendChild($dom->createTextNode($source));
  57. // Does the target contain characters requiring a CDATA section?
  58. $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
  59. $t = $translation->appendChild($dom->createElement('target'));
  60. $t->appendChild($text);
  61. $metadata = $messages->getMetadata($source, $domain);
  62. if (null !== $metadata && array_key_exists('notes', $metadata) && is_array($metadata['notes'])) {
  63. foreach ($metadata['notes'] as $note) {
  64. if (!isset($note['content'])) {
  65. continue;
  66. }
  67. $n = $translation->appendChild($dom->createElement('note'));
  68. $n->appendChild($dom->createTextNode($note['content']));
  69. if (isset($note['priority'])) {
  70. $n->setAttribute('priority', $note['priority']);
  71. }
  72. if (isset($note['from'])) {
  73. $n->setAttribute('from', $note['from']);
  74. }
  75. }
  76. }
  77. $xliffBody->appendChild($translation);
  78. }
  79. return $dom->saveXML();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function getExtension()
  85. {
  86. return 'xlf';
  87. }
  88. }