PoFileLoader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Loader;
  11. /**
  12. * @copyright Copyright (c) 2010, Union of RAD https://github.com/UnionOfRAD/lithium
  13. * @copyright Copyright (c) 2012, Clemens Tolboom
  14. */
  15. class PoFileLoader extends FileLoader
  16. {
  17. /**
  18. * Parses portable object (PO) format.
  19. *
  20. * From https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
  21. * we should be able to parse files having:
  22. *
  23. * white-space
  24. * # translator-comments
  25. * #. extracted-comments
  26. * #: reference...
  27. * #, flag...
  28. * #| msgid previous-untranslated-string
  29. * msgid untranslated-string
  30. * msgstr translated-string
  31. *
  32. * extra or different lines are:
  33. *
  34. * #| msgctxt previous-context
  35. * #| msgid previous-untranslated-string
  36. * msgctxt context
  37. *
  38. * #| msgid previous-untranslated-string-singular
  39. * #| msgid_plural previous-untranslated-string-plural
  40. * msgid untranslated-string-singular
  41. * msgid_plural untranslated-string-plural
  42. * msgstr[0] translated-string-case-0
  43. * ...
  44. * msgstr[N] translated-string-case-n
  45. *
  46. * The definition states:
  47. * - white-space and comments are optional.
  48. * - msgid "" that an empty singleline defines a header.
  49. *
  50. * This parser sacrifices some features of the reference implementation the
  51. * differences to that implementation are as follows.
  52. * - No support for comments spanning multiple lines.
  53. * - Translator and extracted comments are treated as being the same type.
  54. * - Message IDs are allowed to have other encodings as just US-ASCII.
  55. *
  56. * Items with an empty id are ignored.
  57. */
  58. protected function loadResource(string $resource): array
  59. {
  60. $stream = fopen($resource, 'r');
  61. $defaults = [
  62. 'ids' => [],
  63. 'translated' => null,
  64. ];
  65. $messages = [];
  66. $item = $defaults;
  67. $flags = [];
  68. while ($line = fgets($stream)) {
  69. $line = trim($line);
  70. if ('' === $line) {
  71. // Whitespace indicated current item is done
  72. if (!\in_array('fuzzy', $flags)) {
  73. $this->addMessage($messages, $item);
  74. }
  75. $item = $defaults;
  76. $flags = [];
  77. } elseif (str_starts_with($line, '#,')) {
  78. $flags = array_map('trim', explode(',', substr($line, 2)));
  79. } elseif (str_starts_with($line, 'msgid "')) {
  80. // We start a new msg so save previous
  81. // TODO: this fails when comments or contexts are added
  82. $this->addMessage($messages, $item);
  83. $item = $defaults;
  84. $item['ids']['singular'] = substr($line, 7, -1);
  85. } elseif (str_starts_with($line, 'msgstr "')) {
  86. $item['translated'] = substr($line, 8, -1);
  87. } elseif ('"' === $line[0]) {
  88. $continues = isset($item['translated']) ? 'translated' : 'ids';
  89. if (\is_array($item[$continues])) {
  90. end($item[$continues]);
  91. $item[$continues][key($item[$continues])] .= substr($line, 1, -1);
  92. } else {
  93. $item[$continues] .= substr($line, 1, -1);
  94. }
  95. } elseif (str_starts_with($line, 'msgid_plural "')) {
  96. $item['ids']['plural'] = substr($line, 14, -1);
  97. } elseif (str_starts_with($line, 'msgstr[')) {
  98. $size = strpos($line, ']');
  99. $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1);
  100. }
  101. }
  102. // save last item
  103. if (!\in_array('fuzzy', $flags)) {
  104. $this->addMessage($messages, $item);
  105. }
  106. fclose($stream);
  107. return $messages;
  108. }
  109. /**
  110. * Save a translation item to the messages.
  111. *
  112. * A .po file could contain by error missing plural indexes. We need to
  113. * fix these before saving them.
  114. */
  115. private function addMessage(array &$messages, array $item): void
  116. {
  117. if (!empty($item['ids']['singular'])) {
  118. $id = stripcslashes($item['ids']['singular']);
  119. if (isset($item['ids']['plural'])) {
  120. $id .= '|'.stripcslashes($item['ids']['plural']);
  121. }
  122. $translated = (array) $item['translated'];
  123. // PO are by definition indexed so sort by index.
  124. ksort($translated);
  125. // Make sure every index is filled.
  126. end($translated);
  127. $count = key($translated);
  128. // Fill missing spots with '-'.
  129. $empties = array_fill(0, $count + 1, '-');
  130. $translated += $empties;
  131. ksort($translated);
  132. $messages[$id] = stripcslashes(implode('|', $translated));
  133. }
  134. }
  135. }