QtFileDumper.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
  20. {
  21. $dom = new \DOMDocument('1.0', 'utf-8');
  22. $dom->formatOutput = true;
  23. $ts = $dom->appendChild($dom->createElement('TS'));
  24. $context = $ts->appendChild($dom->createElement('context'));
  25. $context->appendChild($dom->createElement('name', $domain));
  26. foreach ($messages->all($domain) as $source => $target) {
  27. $message = $context->appendChild($dom->createElement('message'));
  28. $metadata = $messages->getMetadata($source, $domain);
  29. if (isset($metadata['sources'])) {
  30. foreach ((array) $metadata['sources'] as $location) {
  31. $loc = explode(':', $location, 2);
  32. $location = $message->appendChild($dom->createElement('location'));
  33. $location->setAttribute('filename', $loc[0]);
  34. if (isset($loc[1])) {
  35. $location->setAttribute('line', $loc[1]);
  36. }
  37. }
  38. }
  39. $message->appendChild($dom->createElement('source', $source));
  40. $message->appendChild($dom->createElement('translation', $target));
  41. }
  42. return $dom->saveXML();
  43. }
  44. protected function getExtension(): string
  45. {
  46. return 'ts';
  47. }
  48. }