PoFileDumper.php 1.5 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. * 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 formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
  23. {
  24. $output = 'msgid ""'."\n";
  25. $output .= 'msgstr ""'."\n";
  26. $output .= '"Content-Type: text/plain; charset=UTF-8\n"'."\n";
  27. $output .= '"Content-Transfer-Encoding: 8bit\n"'."\n";
  28. $output .= '"Language: '.$messages->getLocale().'\n"'."\n";
  29. $output .= "\n";
  30. $newLine = false;
  31. foreach ($messages->all($domain) as $source => $target) {
  32. if ($newLine) {
  33. $output .= "\n";
  34. } else {
  35. $newLine = true;
  36. }
  37. $output .= sprintf('msgid "%s"'."\n", $this->escape($source));
  38. $output .= sprintf('msgstr "%s"', $this->escape($target));
  39. }
  40. return $output;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function getExtension()
  46. {
  47. return 'po';
  48. }
  49. private function escape($str)
  50. {
  51. return addcslashes($str, "\0..\37\42\134");
  52. }
  53. }