ArrayLoader.php
1 month ago
CsvFileLoader.php
1 month ago
FileLoader.php
1 month ago
IcuDatFileLoader.php
1 month ago
IcuResFileLoader.php
1 month ago
IniFileLoader.php
1 month ago
JsonFileLoader.php
1 month ago
LoaderInterface.php
1 month ago
MoFileLoader.php
1 month ago
PhpFileLoader.php
1 month ago
PoFileLoader.php
1 month ago
QtFileLoader.php
1 month ago
XliffFileLoader.php
1 month ago
YamlFileLoader.php
1 month ago
XliffFileLoader.php
187 lines
| 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 | namespace IAWPSCOPED\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Config\Resource\FileResource; |
| 14 | use IAWPSCOPED\Symfony\Component\Config\Util\Exception\InvalidXmlException; |
| 15 | use IAWPSCOPED\Symfony\Component\Config\Util\Exception\XmlParsingException; |
| 16 | use IAWPSCOPED\Symfony\Component\Config\Util\XmlUtils; |
| 17 | use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 18 | use IAWPSCOPED\Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 19 | use IAWPSCOPED\Symfony\Component\Translation\Exception\RuntimeException; |
| 20 | use IAWPSCOPED\Symfony\Component\Translation\MessageCatalogue; |
| 21 | use IAWPSCOPED\Symfony\Component\Translation\Util\XliffUtils; |
| 22 | /** |
| 23 | * XliffFileLoader loads translations from XLIFF files. |
| 24 | * |
| 25 | * @author Fabien Potencier <fabien@symfony.com> |
| 26 | * @internal |
| 27 | */ |
| 28 | class XliffFileLoader implements LoaderInterface |
| 29 | { |
| 30 | /** |
| 31 | * {@inheritdoc} |
| 32 | */ |
| 33 | public function load(mixed $resource, string $locale, string $domain = 'messages') : MessageCatalogue |
| 34 | { |
| 35 | if (!\class_exists(XmlUtils::class)) { |
| 36 | throw new RuntimeException('Loading translations from the Xliff format requires the Symfony Config component.'); |
| 37 | } |
| 38 | if (!$this->isXmlString($resource)) { |
| 39 | if (!\stream_is_local($resource)) { |
| 40 | throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource)); |
| 41 | } |
| 42 | if (!\file_exists($resource)) { |
| 43 | throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource)); |
| 44 | } |
| 45 | if (!\is_file($resource)) { |
| 46 | throw new InvalidResourceException(\sprintf('This is neither a file nor an XLIFF string "%s".', $resource)); |
| 47 | } |
| 48 | } |
| 49 | try { |
| 50 | if ($this->isXmlString($resource)) { |
| 51 | $dom = XmlUtils::parse($resource); |
| 52 | } else { |
| 53 | $dom = XmlUtils::loadFile($resource); |
| 54 | } |
| 55 | } catch (\InvalidArgumentException|XmlParsingException|InvalidXmlException $e) { |
| 56 | throw new InvalidResourceException(\sprintf('Unable to load "%s": ', $resource) . $e->getMessage(), $e->getCode(), $e); |
| 57 | } |
| 58 | if ($errors = XliffUtils::validateSchema($dom)) { |
| 59 | throw new InvalidResourceException(\sprintf('Invalid resource provided: "%s"; Errors: ', $resource) . XliffUtils::getErrorsAsString($errors)); |
| 60 | } |
| 61 | $catalogue = new MessageCatalogue($locale); |
| 62 | $this->extract($dom, $catalogue, $domain); |
| 63 | if (\is_file($resource) && \class_exists(FileResource::class)) { |
| 64 | $catalogue->addResource(new FileResource($resource)); |
| 65 | } |
| 66 | return $catalogue; |
| 67 | } |
| 68 | private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain) |
| 69 | { |
| 70 | $xliffVersion = XliffUtils::getVersionNumber($dom); |
| 71 | if ('1.2' === $xliffVersion) { |
| 72 | $this->extractXliff1($dom, $catalogue, $domain); |
| 73 | } |
| 74 | if ('2.0' === $xliffVersion) { |
| 75 | $this->extractXliff2($dom, $catalogue, $domain); |
| 76 | } |
| 77 | } |
| 78 | /** |
| 79 | * Extract messages and metadata from DOMDocument into a MessageCatalogue. |
| 80 | */ |
| 81 | private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain) |
| 82 | { |
| 83 | $xml = \simplexml_import_dom($dom); |
| 84 | $encoding = $dom->encoding ? \strtoupper($dom->encoding) : null; |
| 85 | $namespace = 'urn:oasis:names:tc:xliff:document:1.2'; |
| 86 | $xml->registerXPathNamespace('xliff', $namespace); |
| 87 | foreach ($xml->xpath('//xliff:file') as $file) { |
| 88 | $fileAttributes = $file->attributes(); |
| 89 | $file->registerXPathNamespace('xliff', $namespace); |
| 90 | foreach ($file->xpath('.//xliff:trans-unit') as $translation) { |
| 91 | $attributes = $translation->attributes(); |
| 92 | if (!(isset($attributes['resname']) || isset($translation->source))) { |
| 93 | continue; |
| 94 | } |
| 95 | $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; |
| 96 | // If the xlf file has another encoding specified, try to convert it because |
| 97 | // simple_xml will always return utf-8 encoded values |
| 98 | $target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding); |
| 99 | $catalogue->set((string) $source, $target, $domain); |
| 100 | $metadata = ['source' => (string) $translation->source, 'file' => ['original' => (string) $fileAttributes['original']]]; |
| 101 | if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) { |
| 102 | $metadata['notes'] = $notes; |
| 103 | } |
| 104 | if (isset($translation->target) && $translation->target->attributes()) { |
| 105 | $metadata['target-attributes'] = []; |
| 106 | foreach ($translation->target->attributes() as $key => $value) { |
| 107 | $metadata['target-attributes'][$key] = (string) $value; |
| 108 | } |
| 109 | } |
| 110 | if (isset($attributes['id'])) { |
| 111 | $metadata['id'] = (string) $attributes['id']; |
| 112 | } |
| 113 | $catalogue->setMetadata((string) $source, $metadata, $domain); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain) |
| 118 | { |
| 119 | $xml = \simplexml_import_dom($dom); |
| 120 | $encoding = $dom->encoding ? \strtoupper($dom->encoding) : null; |
| 121 | $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0'); |
| 122 | foreach ($xml->xpath('//xliff:unit') as $unit) { |
| 123 | foreach ($unit->segment as $segment) { |
| 124 | $attributes = $unit->attributes(); |
| 125 | $source = $attributes['name'] ?? $segment->source; |
| 126 | // If the xlf file has another encoding specified, try to convert it because |
| 127 | // simple_xml will always return utf-8 encoded values |
| 128 | $target = $this->utf8ToCharset((string) ($segment->target ?? $segment->source), $encoding); |
| 129 | $catalogue->set((string) $source, $target, $domain); |
| 130 | $metadata = []; |
| 131 | if (isset($segment->target) && $segment->target->attributes()) { |
| 132 | $metadata['target-attributes'] = []; |
| 133 | foreach ($segment->target->attributes() as $key => $value) { |
| 134 | $metadata['target-attributes'][$key] = (string) $value; |
| 135 | } |
| 136 | } |
| 137 | if (isset($unit->notes)) { |
| 138 | $metadata['notes'] = []; |
| 139 | foreach ($unit->notes->note as $noteNode) { |
| 140 | $note = []; |
| 141 | foreach ($noteNode->attributes() as $key => $value) { |
| 142 | $note[$key] = (string) $value; |
| 143 | } |
| 144 | $note['content'] = (string) $noteNode; |
| 145 | $metadata['notes'][] = $note; |
| 146 | } |
| 147 | } |
| 148 | $catalogue->setMetadata((string) $source, $metadata, $domain); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | /** |
| 153 | * Convert a UTF8 string to the specified encoding. |
| 154 | */ |
| 155 | private function utf8ToCharset(string $content, string $encoding = null) : string |
| 156 | { |
| 157 | if ('UTF-8' !== $encoding && !empty($encoding)) { |
| 158 | return \mb_convert_encoding($content, $encoding, 'UTF-8'); |
| 159 | } |
| 160 | return $content; |
| 161 | } |
| 162 | private function parseNotesMetadata(\SimpleXMLElement $noteElement = null, string $encoding = null) : array |
| 163 | { |
| 164 | $notes = []; |
| 165 | if (null === $noteElement) { |
| 166 | return $notes; |
| 167 | } |
| 168 | /** @var \SimpleXMLElement $xmlNote */ |
| 169 | foreach ($noteElement as $xmlNote) { |
| 170 | $noteAttributes = $xmlNote->attributes(); |
| 171 | $note = ['content' => $this->utf8ToCharset((string) $xmlNote, $encoding)]; |
| 172 | if (isset($noteAttributes['priority'])) { |
| 173 | $note['priority'] = (int) $noteAttributes['priority']; |
| 174 | } |
| 175 | if (isset($noteAttributes['from'])) { |
| 176 | $note['from'] = (string) $noteAttributes['from']; |
| 177 | } |
| 178 | $notes[] = $note; |
| 179 | } |
| 180 | return $notes; |
| 181 | } |
| 182 | private function isXmlString(string $resource) : bool |
| 183 | { |
| 184 | return 0 === \strpos($resource, '<?xml'); |
| 185 | } |
| 186 | } |
| 187 |