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