PoFileDumper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
  20. {
  21. $output = 'msgid ""'."\n";
  22. $output .= 'msgstr ""'."\n";
  23. $output .= '"Content-Type: text/plain; charset=UTF-8\n"'."\n";
  24. $output .= '"Content-Transfer-Encoding: 8bit\n"'."\n";
  25. $output .= '"Language: '.$messages->getLocale().'\n"'."\n";
  26. $output .= "\n";
  27. $newLine = false;
  28. foreach ($messages->all($domain) as $source => $target) {
  29. if ($newLine) {
  30. $output .= "\n";
  31. } else {
  32. $newLine = true;
  33. }
  34. $metadata = $messages->getMetadata($source, $domain);
  35. if (isset($metadata['comments'])) {
  36. $output .= $this->formatComments($metadata['comments']);
  37. }
  38. if (isset($metadata['flags'])) {
  39. $output .= $this->formatComments(implode(',', (array) $metadata['flags']), ',');
  40. }
  41. if (isset($metadata['sources'])) {
  42. $output .= $this->formatComments(implode(' ', (array) $metadata['sources']), ':');
  43. }
  44. $sourceRules = $this->getStandardRules($source);
  45. $targetRules = $this->getStandardRules($target);
  46. if (2 == \count($sourceRules) && [] !== $targetRules) {
  47. $output .= sprintf('msgid "%s"'."\n", $this->escape($sourceRules[0]));
  48. $output .= sprintf('msgid_plural "%s"'."\n", $this->escape($sourceRules[1]));
  49. foreach ($targetRules as $i => $targetRule) {
  50. $output .= sprintf('msgstr[%d] "%s"'."\n", $i, $this->escape($targetRule));
  51. }
  52. } else {
  53. $output .= sprintf('msgid "%s"'."\n", $this->escape($source));
  54. $output .= sprintf('msgstr "%s"'."\n", $this->escape($target));
  55. }
  56. }
  57. return $output;
  58. }
  59. private function getStandardRules(string $id): array
  60. {
  61. // Partly copied from TranslatorTrait::trans.
  62. $parts = [];
  63. if (preg_match('/^\|++$/', $id)) {
  64. $parts = explode('|', $id);
  65. } elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
  66. $parts = $matches[0];
  67. }
  68. $intervalRegexp = <<<'EOF'
  69. /^(?P<interval>
  70. ({\s*
  71. (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
  72. \s*})
  73. |
  74. (?P<left_delimiter>[\[\]])
  75. \s*
  76. (?P<left>-Inf|\-?\d+(\.\d+)?)
  77. \s*,\s*
  78. (?P<right>\+?Inf|\-?\d+(\.\d+)?)
  79. \s*
  80. (?P<right_delimiter>[\[\]])
  81. )\s*(?P<message>.*?)$/xs
  82. EOF;
  83. $standardRules = [];
  84. foreach ($parts as $part) {
  85. $part = trim(str_replace('||', '|', $part));
  86. if (preg_match($intervalRegexp, $part)) {
  87. // Explicit rule is not a standard rule.
  88. return [];
  89. } else {
  90. $standardRules[] = $part;
  91. }
  92. }
  93. return $standardRules;
  94. }
  95. protected function getExtension(): string
  96. {
  97. return 'po';
  98. }
  99. private function escape(string $str): string
  100. {
  101. return addcslashes($str, "\0..\37\42\134");
  102. }
  103. private function formatComments(string|array $comments, string $prefix = ''): ?string
  104. {
  105. $output = null;
  106. foreach ((array) $comments as $comment) {
  107. $output .= sprintf('#%s %s'."\n", $prefix, $comment);
  108. }
  109. return $output;
  110. }
  111. }