| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class PoFileLoader extends FileLoader |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected function loadResource(string $resource): array |
| 62 |
{ |
| 63 |
$stream = fopen($resource, 'r'); |
| 64 |
|
| 65 |
$defaults = [ |
| 66 |
'ids' => [], |
| 67 |
'translated' => null, |
| 68 |
]; |
| 69 |
|
| 70 |
$messages = []; |
| 71 |
$item = $defaults; |
| 72 |
$flags = []; |
| 73 |
|
| 74 |
while ($line = fgets($stream)) { |
| 75 |
$line = trim($line); |
| 76 |
|
| 77 |
if ('' === $line) { |
| 78 |
|
| 79 |
if (!\in_array('fuzzy', $flags, true)) { |
| 80 |
$this->addMessage($messages, $item); |
| 81 |
} |
| 82 |
$item = $defaults; |
| 83 |
$flags = []; |
| 84 |
} elseif (str_starts_with($line, '#,')) { |
| 85 |
$flags = array_map('trim', explode(',', substr($line, 2))); |
| 86 |
} elseif (str_starts_with($line, 'msgid "')) { |
| 87 |
|
| 88 |
|
| 89 |
$this->addMessage($messages, $item); |
| 90 |
$item = $defaults; |
| 91 |
$item['ids']['singular'] = substr($line, 7, -1); |
| 92 |
} elseif (str_starts_with($line, 'msgstr "')) { |
| 93 |
$item['translated'] = substr($line, 8, -1); |
| 94 |
} elseif ('"' === $line[0]) { |
| 95 |
$continues = isset($item['translated']) ? 'translated' : 'ids'; |
| 96 |
|
| 97 |
if (\is_array($item[$continues])) { |
| 98 |
end($item[$continues]); |
| 99 |
$item[$continues][key($item[$continues])] .= substr($line, 1, -1); |
| 100 |
} else { |
| 101 |
$item[$continues] .= substr($line, 1, -1); |
| 102 |
} |
| 103 |
} elseif (str_starts_with($line, 'msgid_plural "')) { |
| 104 |
$item['ids']['plural'] = substr($line, 14, -1); |
| 105 |
} elseif (str_starts_with($line, 'msgstr[')) { |
| 106 |
$size = strpos($line, ']'); |
| 107 |
$item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (!\in_array('fuzzy', $flags, true)) { |
| 112 |
$this->addMessage($messages, $item); |
| 113 |
} |
| 114 |
fclose($stream); |
| 115 |
|
| 116 |
return $messages; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function addMessage(array &$messages, array $item): void |
| 126 |
{ |
| 127 |
if (!empty($item['ids']['singular'])) { |
| 128 |
$id = stripcslashes($item['ids']['singular']); |
| 129 |
if (isset($item['ids']['plural'])) { |
| 130 |
$id .= '|'.stripcslashes($item['ids']['plural']); |
| 131 |
} |
| 132 |
|
| 133 |
$translated = (array) $item['translated']; |
| 134 |
|
| 135 |
ksort($translated); |
| 136 |
|
| 137 |
end($translated); |
| 138 |
$count = key($translated); |
| 139 |
|
| 140 |
$empties = array_fill(0, $count + 1, '-'); |
| 141 |
$translated += $empties; |
| 142 |
ksort($translated); |
| 143 |
|
| 144 |
$messages[$id] = stripcslashes(implode('|', $translated)); |
| 145 |
} |
| 146 |
} |
| 147 |
} |