comparison vendor/symfony/translation/Loader/XliffFileLoader.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
125 $xml = simplexml_import_dom($dom); 125 $xml = simplexml_import_dom($dom);
126 $encoding = strtoupper($dom->encoding); 126 $encoding = strtoupper($dom->encoding);
127 127
128 $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0'); 128 $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
129 129
130 foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) { 130 foreach ($xml->xpath('//xliff:unit') as $unit) {
131 $source = $segment->source; 131 foreach ($unit->segment as $segment) {
132 132 $source = $segment->source;
133 // If the xlf file has another encoding specified, try to convert it because 133
134 // simple_xml will always return utf-8 encoded values 134 // If the xlf file has another encoding specified, try to convert it because
135 $target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding); 135 // simple_xml will always return utf-8 encoded values
136 136 $target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
137 $catalogue->set((string) $source, $target, $domain); 137
138 138 $catalogue->set((string) $source, $target, $domain);
139 $metadata = array(); 139
140 if (isset($segment->target) && $segment->target->attributes()) { 140 $metadata = array();
141 $metadata['target-attributes'] = array(); 141 if (isset($segment->target) && $segment->target->attributes()) {
142 foreach ($segment->target->attributes() as $key => $value) { 142 $metadata['target-attributes'] = array();
143 $metadata['target-attributes'][$key] = (string) $value; 143 foreach ($segment->target->attributes() as $key => $value) {
144 $metadata['target-attributes'][$key] = (string) $value;
145 }
144 } 146 }
145 } 147
146 148 if (isset($unit->notes)) {
147 $catalogue->setMetadata((string) $source, $metadata, $domain); 149 $metadata['notes'] = array();
150 foreach ($unit->notes->note as $noteNode) {
151 $note = array();
152 foreach ($noteNode->attributes() as $key => $value) {
153 $note[$key] = (string) $value;
154 }
155 $note['content'] = (string) $noteNode;
156 $metadata['notes'][] = $note;
157 }
158 }
159
160 $catalogue->setMetadata((string) $source, $metadata, $domain);
161 }
148 } 162 }
149 } 163 }
150 164
151 /** 165 /**
152 * Convert a UTF8 string to the specified encoding. 166 * Convert a UTF8 string to the specified encoding.
219 */ 233 */
220 private function fixXmlLocation($schemaSource, $xmlUri) 234 private function fixXmlLocation($schemaSource, $xmlUri)
221 { 235 {
222 $newPath = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd'; 236 $newPath = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
223 $parts = explode('/', $newPath); 237 $parts = explode('/', $newPath);
238 $locationstart = 'file:///';
224 if (0 === stripos($newPath, 'phar://')) { 239 if (0 === stripos($newPath, 'phar://')) {
225 $tmpfile = tempnam(sys_get_temp_dir(), 'sf2'); 240 $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
226 if ($tmpfile) { 241 if ($tmpfile) {
227 copy($newPath, $tmpfile); 242 copy($newPath, $tmpfile);
228 $parts = explode('/', str_replace('\\', '/', $tmpfile)); 243 $parts = explode('/', str_replace('\\', '/', $tmpfile));
244 } else {
245 array_shift($parts);
246 $locationstart = 'phar:///';
229 } 247 }
230 } 248 }
231 249
232 $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : ''; 250 $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
233 $newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts)); 251 $newPath = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
234 252
235 return str_replace($xmlUri, $newPath, $schemaSource); 253 return str_replace($xmlUri, $newPath, $schemaSource);
236 } 254 }
237 255
238 /** 256 /**
281 return $version->nodeValue; 299 return $version->nodeValue;
282 } 300 }
283 301
284 $namespace = $xliff->attributes->getNamedItem('xmlns'); 302 $namespace = $xliff->attributes->getNamedItem('xmlns');
285 if ($namespace) { 303 if ($namespace) {
286 if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) { 304 if (0 !== substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) {
287 throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace)); 305 throw new InvalidArgumentException(sprintf('Not a valid XLIFF namespace "%s"', $namespace));
288 } 306 }
289 307
290 return substr($namespace, 34); 308 return substr($namespace, 34);
291 } 309 }