XliffFileDumper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. protected function format(MessageCatalogue $messages, $domain)
  23. {
  24. $dom = new \DOMDocument('1.0', 'utf-8');
  25. $dom->formatOutput = true;
  26. $xliff = $dom->appendChild($dom->createElement('xliff'));
  27. $xliff->setAttribute('version', '1.2');
  28. $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
  29. $xliffFile = $xliff->appendChild($dom->createElement('file'));
  30. $xliffFile->setAttribute('source-language', $messages->getLocale());
  31. $xliffFile->setAttribute('datatype', 'plaintext');
  32. $xliffFile->setAttribute('original', 'file.ext');
  33. $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
  34. foreach ($messages->all($domain) as $source => $target) {
  35. $translation = $dom->createElement('trans-unit');
  36. $translation->setAttribute('id', md5($source));
  37. $translation->setAttribute('resname', $source);
  38. $s = $translation->appendChild($dom->createElement('source'));
  39. $s->appendChild($dom->createTextNode($source));
  40. $t = $translation->appendChild($dom->createElement('target'));
  41. $t->appendChild($dom->createTextNode($target));
  42. $xliffBody->appendChild($translation);
  43. }
  44. return $dom->saveXML();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function getExtension()
  50. {
  51. return 'xlf';
  52. }
  53. }