src/Viserio/Component/Parser/Parser/XliffParser.php
private function extractXliffVersion1(DOMDocument $dom): array
    {
        $xml = XmlUtils::importDom($dom);

        $encoding = \mb_strtoupper($dom->encoding);
        $datas    = [
            'version'         => '1.2',
            'source-language' => '',
            'target-language' => '',
        ];

        foreach ($xml->file->attributes() as $key => $value) {
            if ($key === 'source-language' || $key === 'target-language') {
                $datas[$key] = (string) $value;
            }
        }

        $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');

        foreach ((array) $xml->xpath('//xliff:trans-unit') as $trans) {
            $attributes = $trans->attributes();
            $id         = (string) ($attributes['resname'] ?? $trans->source ?? '');

            if ($id === '') {
                continue;
            }

            $datas[$id] = [
                'source' => (string) $trans->source,
                // If the xlf file has another encoding specified, try to convert it because
                // simple_xml will always return utf-8 encoded values
                'target' => isset($trans->target) ? self::utf8ToCharset((string) $trans->target, $encoding) : null,
            ];

            if (isset($attributes['id'])) {
                $datas[$id]['id'] = (string) $attributes['id'];
            }

            // If the translation has a note
            if (isset($trans->note)) {
                $datas[$id]['notes'] = self::parseNotes($trans->note, $encoding);
            }

            if (isset($trans->target) && ($attributes = $trans->target->attributes())) {
                $datas[$id]['target-attributes'] = [];

                foreach ($attributes as $key => $value) {
                    $datas[$id]['target-attributes'][$key] = (string) $value;
                }
            }
        }

        return $datas;
    }
private function extractXliffVersion1(DOMDocument $dom): array
    {
        $xml = XmlUtils::importDom($dom);

        $encoding = \mb_strtoupper($dom->encoding);
        $datas    = [
            'version'         => '1.2',
            'source-language' => '',
            'target-language' => '',
        ];

        foreach ($xml->file->attributes() as $key => $value) {
            if ($key === 'source-language' || $key === 'target-language') {
                $datas[$key] = (string) $value;
            }
        }

        $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');

        foreach ((array) $xml->xpath('//xliff:trans-unit') as $trans) {
            $attributes = $trans->attributes();
            $id         = (string) ($attributes['resname'] ?? $trans->source ?? '');

            if ($id === '') {
                continue;
            }

            $datas[$id] = [
                'source' => (string) $trans->source,
                // If the xlf file has another encoding specified, try to convert it because
                // simple_xml will always return utf-8 encoded values
                'target' => isset($trans->target) ? self::utf8ToCharset((string) $trans->target, $encoding) : null,
            ];

            if (isset($attributes['id'])) {
                $datas[$id]['id'] = (string) $attributes['id'];
            }

            // If the translation has a note
            if (isset($trans->note)) {
                $datas[$id]['notes'] = self::parseNotes($trans->note, $encoding);
            }

            if (isset($trans->target) && ($attributes = $trans->target->attributes())) {
                $datas[$id]['target-attributes'] = [];

                foreach ($attributes as $key => $value) {
                    $datas[$id]['target-attributes'][$key] = (string) $value;
                }
            }
        }

        return $datas;
    }
Extract messages and metadata from DOMDocument into a MessageCatalogue.

@param \DOMDocument $dom

@throws \Viserio\Component\Contract\Parser\Exception\ParseException

@return array