| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Config\Resource\FileResource; |
| 15 |
use Symfony\Component\Config\Util\Exception\InvalidXmlException; |
| 16 |
use Symfony\Component\Config\Util\Exception\XmlParsingException; |
| 17 |
use Symfony\Component\Config\Util\XmlUtils; |
| 18 |
use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 19 |
use Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 20 |
use Symfony\Component\Translation\Exception\RuntimeException; |
| 21 |
use Symfony\Component\Translation\MessageCatalogue; |
| 22 |
use Symfony\Component\Translation\Util\XliffUtils; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class XliffFileLoader implements LoaderInterface |
| 30 |
{ |
| 31 |
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue |
| 32 |
{ |
| 33 |
if (!class_exists(XmlUtils::class)) { |
| 34 |
throw new RuntimeException('Loading translations from the Xliff format requires the Symfony Config component.'); |
| 35 |
} |
| 36 |
|
| 37 |
if (!$this->isXmlString($resource)) { |
| 38 |
if (!stream_is_local($resource)) { |
| 39 |
throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource)); |
| 40 |
} |
| 41 |
|
| 42 |
if (!file_exists($resource)) { |
| 43 |
throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource)); |
| 44 |
} |
| 45 |
|
| 46 |
if (!is_file($resource)) { |
| 47 |
throw new InvalidResourceException(\sprintf('This is neither a file nor an XLIFF string "%s".', $resource)); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
try { |
| 52 |
if ($this->isXmlString($resource)) { |
| 53 |
$dom = XmlUtils::parse($resource); |
| 54 |
} else { |
| 55 |
$dom = XmlUtils::loadFile($resource); |
| 56 |
} |
| 57 |
} catch (\InvalidArgumentException|XmlParsingException|InvalidXmlException $e) { |
| 58 |
throw new InvalidResourceException(\sprintf('Unable to load "%s": ', $resource).$e->getMessage(), $e->getCode(), $e); |
| 59 |
} |
| 60 |
|
| 61 |
if ($errors = XliffUtils::validateSchema($dom)) { |
| 62 |
throw new InvalidResourceException(\sprintf('Invalid resource provided: "%s"; Errors: ', $resource).XliffUtils::getErrorsAsString($errors)); |
| 63 |
} |
| 64 |
|
| 65 |
$catalogue = new MessageCatalogue($locale); |
| 66 |
$this->extract($dom, $catalogue, $domain); |
| 67 |
|
| 68 |
if (is_file($resource) && class_exists(FileResource::class)) { |
| 69 |
$catalogue->addResource(new FileResource($resource)); |
| 70 |
} |
| 71 |
|
| 72 |
return $catalogue; |
| 73 |
} |
| 74 |
|
| 75 |
private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void |
| 76 |
{ |
| 77 |
$xliffVersion = XliffUtils::getVersionNumber($dom); |
| 78 |
|
| 79 |
if ('1.2' === $xliffVersion) { |
| 80 |
$this->extractXliff1($dom, $catalogue, $domain); |
| 81 |
} |
| 82 |
|
| 83 |
if ('2.0' === $xliffVersion) { |
| 84 |
$this->extractXliff2($dom, $catalogue, $domain); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void |
| 92 |
{ |
| 93 |
$xml = simplexml_import_dom($dom); |
| 94 |
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null; |
| 95 |
|
| 96 |
$namespace = 'urn:oasis:names:tc:xliff:document:1.2'; |
| 97 |
$xml->registerXPathNamespace('xliff', $namespace); |
| 98 |
|
| 99 |
foreach ($xml->xpath('//xliff:file') as $file) { |
| 100 |
$fileAttributes = $file->attributes(); |
| 101 |
|
| 102 |
$file->registerXPathNamespace('xliff', $namespace); |
| 103 |
|
| 104 |
foreach ($file->xpath('.//xliff:prop') as $prop) { |
| 105 |
$catalogue->setCatalogueMetadata($prop->attributes()['prop-type'], (string) $prop, $domain); |
| 106 |
} |
| 107 |
|
| 108 |
foreach ($file->xpath('.//xliff:trans-unit') as $translation) { |
| 109 |
$attributes = $translation->attributes(); |
| 110 |
|
| 111 |
if (!(isset($attributes['resname']) || isset($translation->source))) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$source = (string) (isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source); |
| 116 |
|
| 117 |
if (isset($translation->target) |
| 118 |
&& 'needs-translation' === (string) $translation->target->attributes()['state'] |
| 119 |
&& \in_array((string) $translation->target, [$source, (string) $translation->source], true) |
| 120 |
) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
$target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding); |
| 127 |
|
| 128 |
$catalogue->set($source, $target, $domain); |
| 129 |
|
| 130 |
$metadata = [ |
| 131 |
'source' => (string) $translation->source, |
| 132 |
'file' => [ |
| 133 |
'original' => (string) $fileAttributes['original'], |
| 134 |
], |
| 135 |
]; |
| 136 |
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) { |
| 137 |
$metadata['notes'] = $notes; |
| 138 |
} |
| 139 |
|
| 140 |
if (isset($translation->target) && $translation->target->attributes()) { |
| 141 |
$metadata['target-attributes'] = []; |
| 142 |
foreach ($translation->target->attributes() as $key => $value) { |
| 143 |
$metadata['target-attributes'][$key] = (string) $value; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (isset($attributes['id'])) { |
| 148 |
$metadata['id'] = (string) $attributes['id']; |
| 149 |
} |
| 150 |
|
| 151 |
$catalogue->setMetadata($source, $metadata, $domain); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain): void |
| 157 |
{ |
| 158 |
$xml = simplexml_import_dom($dom); |
| 159 |
$encoding = $dom->encoding ? strtoupper($dom->encoding) : null; |
| 160 |
|
| 161 |
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0'); |
| 162 |
|
| 163 |
foreach ($xml->xpath('//xliff:unit') as $unit) { |
| 164 |
foreach ($unit->segment as $segment) { |
| 165 |
$attributes = $unit->attributes(); |
| 166 |
$source = $attributes['name'] ?? $segment->source; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
$target = $this->utf8ToCharset((string) ($segment->target ?? $segment->source), $encoding); |
| 171 |
|
| 172 |
$catalogue->set((string) $source, $target, $domain); |
| 173 |
|
| 174 |
$metadata = []; |
| 175 |
if ($segment->attributes()) { |
| 176 |
$metadata['segment-attributes'] = []; |
| 177 |
foreach ($segment->attributes() as $key => $value) { |
| 178 |
$metadata['segment-attributes'][$key] = (string) $value; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (isset($segment->target) && $segment->target->attributes()) { |
| 183 |
$metadata['target-attributes'] = []; |
| 184 |
foreach ($segment->target->attributes() as $key => $value) { |
| 185 |
$metadata['target-attributes'][$key] = (string) $value; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if (isset($unit->notes)) { |
| 190 |
$metadata['notes'] = []; |
| 191 |
foreach ($unit->notes->note as $noteNode) { |
| 192 |
$note = []; |
| 193 |
foreach ($noteNode->attributes() as $key => $value) { |
| 194 |
$note[$key] = (string) $value; |
| 195 |
} |
| 196 |
$note['content'] = (string) $noteNode; |
| 197 |
$metadata['notes'][] = $note; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
$catalogue->setMetadata((string) $source, $metadata, $domain); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private function utf8ToCharset(string $content, ?string $encoding = null): string |
| 210 |
{ |
| 211 |
if ('UTF-8' !== $encoding && $encoding) { |
| 212 |
return mb_convert_encoding($content, $encoding, 'UTF-8'); |
| 213 |
} |
| 214 |
|
| 215 |
return $content; |
| 216 |
} |
| 217 |
|
| 218 |
private function parseNotesMetadata(?\SimpleXMLElement $noteElement = null, ?string $encoding = null): array |
| 219 |
{ |
| 220 |
$notes = []; |
| 221 |
|
| 222 |
if (null === $noteElement) { |
| 223 |
return $notes; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
foreach ($noteElement as $xmlNote) { |
| 228 |
$noteAttributes = $xmlNote->attributes(); |
| 229 |
$note = ['content' => $this->utf8ToCharset((string) $xmlNote, $encoding)]; |
| 230 |
if (isset($noteAttributes['priority'])) { |
| 231 |
$note['priority'] = (int) $noteAttributes['priority']; |
| 232 |
} |
| 233 |
|
| 234 |
if (isset($noteAttributes['from'])) { |
| 235 |
$note['from'] = (string) $noteAttributes['from']; |
| 236 |
} |
| 237 |
|
| 238 |
$notes[] = $note; |
| 239 |
} |
| 240 |
|
| 241 |
return $notes; |
| 242 |
} |
| 243 |
|
| 244 |
private function isXmlString(string $resource): bool |
| 245 |
{ |
| 246 |
return str_starts_with($resource, '<?xml'); |
| 247 |
} |
| 248 |
} |