Ei kuvausta

PoFileDumper.php 3.9KB

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