PoFileDumper.php 1.2 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. * PoFileDumper generates a gettext formatted string representation of a message catalogue.
  14. *
  15. * @author Stealth35
  16. */
  17. class PoFileDumper extends FileDumper
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function format(MessageCatalogue $messages, $domain = 'messages')
  23. {
  24. $output = '';
  25. $newLine = false;
  26. foreach ($messages->all($domain) as $source => $target) {
  27. if ($newLine) {
  28. $output .= "\n";
  29. } else {
  30. $newLine = true;
  31. }
  32. $output .= sprintf('msgid "%s"'."\n", $this->escape($source));
  33. $output .= sprintf('msgstr "%s"', $this->escape($target));
  34. }
  35. return $output;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. protected function getExtension()
  41. {
  42. return 'po';
  43. }
  44. private function escape($str)
  45. {
  46. return addcslashes($str, "\0..\37\42\134");
  47. }
  48. }