Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/translation/Loader/PoFileLoader.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
62 */ | 62 */ |
63 protected function loadResource($resource) | 63 protected function loadResource($resource) |
64 { | 64 { |
65 $stream = fopen($resource, 'r'); | 65 $stream = fopen($resource, 'r'); |
66 | 66 |
67 $defaults = array( | 67 $defaults = [ |
68 'ids' => array(), | 68 'ids' => [], |
69 'translated' => null, | 69 'translated' => null, |
70 ); | 70 ]; |
71 | 71 |
72 $messages = array(); | 72 $messages = []; |
73 $item = $defaults; | 73 $item = $defaults; |
74 $flags = array(); | 74 $flags = []; |
75 | 75 |
76 while ($line = fgets($stream)) { | 76 while ($line = fgets($stream)) { |
77 $line = trim($line); | 77 $line = trim($line); |
78 | 78 |
79 if ('' === $line) { | 79 if ('' === $line) { |
80 // Whitespace indicated current item is done | 80 // Whitespace indicated current item is done |
81 if (!in_array('fuzzy', $flags)) { | 81 if (!\in_array('fuzzy', $flags)) { |
82 $this->addMessage($messages, $item); | 82 $this->addMessage($messages, $item); |
83 } | 83 } |
84 $item = $defaults; | 84 $item = $defaults; |
85 $flags = array(); | 85 $flags = []; |
86 } elseif ('#,' === substr($line, 0, 2)) { | 86 } elseif ('#,' === substr($line, 0, 2)) { |
87 $flags = array_map('trim', explode(',', substr($line, 2))); | 87 $flags = array_map('trim', explode(',', substr($line, 2))); |
88 } elseif ('msgid "' === substr($line, 0, 7)) { | 88 } elseif ('msgid "' === substr($line, 0, 7)) { |
89 // We start a new msg so save previous | 89 // We start a new msg so save previous |
90 // TODO: this fails when comments or contexts are added | 90 // TODO: this fails when comments or contexts are added |
94 } elseif ('msgstr "' === substr($line, 0, 8)) { | 94 } elseif ('msgstr "' === substr($line, 0, 8)) { |
95 $item['translated'] = substr($line, 8, -1); | 95 $item['translated'] = substr($line, 8, -1); |
96 } elseif ('"' === $line[0]) { | 96 } elseif ('"' === $line[0]) { |
97 $continues = isset($item['translated']) ? 'translated' : 'ids'; | 97 $continues = isset($item['translated']) ? 'translated' : 'ids'; |
98 | 98 |
99 if (is_array($item[$continues])) { | 99 if (\is_array($item[$continues])) { |
100 end($item[$continues]); | 100 end($item[$continues]); |
101 $item[$continues][key($item[$continues])] .= substr($line, 1, -1); | 101 $item[$continues][key($item[$continues])] .= substr($line, 1, -1); |
102 } else { | 102 } else { |
103 $item[$continues] .= substr($line, 1, -1); | 103 $item[$continues] .= substr($line, 1, -1); |
104 } | 104 } |
108 $size = strpos($line, ']'); | 108 $size = strpos($line, ']'); |
109 $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1); | 109 $item['translated'][(int) substr($line, 7, 1)] = substr($line, $size + 3, -1); |
110 } | 110 } |
111 } | 111 } |
112 // save last item | 112 // save last item |
113 if (!in_array('fuzzy', $flags)) { | 113 if (!\in_array('fuzzy', $flags)) { |
114 $this->addMessage($messages, $item); | 114 $this->addMessage($messages, $item); |
115 } | 115 } |
116 fclose($stream); | 116 fclose($stream); |
117 | 117 |
118 return $messages; | 118 return $messages; |
124 * A .po file could contain by error missing plural indexes. We need to | 124 * A .po file could contain by error missing plural indexes. We need to |
125 * fix these before saving them. | 125 * fix these before saving them. |
126 */ | 126 */ |
127 private function addMessage(array &$messages, array $item) | 127 private function addMessage(array &$messages, array $item) |
128 { | 128 { |
129 if (is_array($item['translated'])) { | 129 if (\is_array($item['translated'])) { |
130 $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]); | 130 $messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]); |
131 if (isset($item['ids']['plural'])) { | 131 if (isset($item['ids']['plural'])) { |
132 $plurals = $item['translated']; | 132 $plurals = $item['translated']; |
133 // PO are by definition indexed so sort by index. | 133 // PO are by definition indexed so sort by index. |
134 ksort($plurals); | 134 ksort($plurals); |