QtFileDumper.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. * QtFileDumper generates ts files from a message catalogue.
  14. *
  15. * @author Benjamin Eberlei <kontakt@beberlei.de>
  16. */
  17. class QtFileDumper extends FileDumper
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
  23. {
  24. $dom = new \DOMDocument('1.0', 'utf-8');
  25. $dom->formatOutput = true;
  26. $ts = $dom->appendChild($dom->createElement('TS'));
  27. $context = $ts->appendChild($dom->createElement('context'));
  28. $context->appendChild($dom->createElement('name', $domain));
  29. foreach ($messages->all($domain) as $source => $target) {
  30. $message = $context->appendChild($dom->createElement('message'));
  31. $metadata = $messages->getMetadata($source, $domain);
  32. if (isset($metadata['sources'])) {
  33. foreach ((array) $metadata['sources'] as $location) {
  34. $loc = explode(':', $location, 2);
  35. $location = $message->appendChild($dom->createElement('location'));
  36. $location->setAttribute('filename', $loc[0]);
  37. if (isset($loc[1])) {
  38. $location->setAttribute('line', $loc[1]);
  39. }
  40. }
  41. }
  42. $message->appendChild($dom->createElement('source', $source));
  43. $message->appendChild($dom->createElement('translation', $target));
  44. }
  45. return $dom->saveXML();
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function getExtension()
  51. {
  52. return 'ts';
  53. }
  54. }