PoFileDumper.php 4.3 KB

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