|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the TYPO3 CMS project. |
| 5 | + * |
| 6 | + * It is free software; you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU General Public License, either version 2 |
| 8 | + * of the License, or any later version. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please read the |
| 11 | + * LICENSE.txt file that was distributed with this source code. |
| 12 | + * |
| 13 | + * The TYPO3 project - inspiring people to share! |
| 14 | + */ |
| 15 | + |
| 16 | +namespace Evoweb\EwLlxml2xliff\Localization\Parser; |
| 17 | + |
| 18 | +use TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException; |
| 19 | +use TYPO3\CMS\Core\Utility\ArrayUtility; |
| 20 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 21 | +use TYPO3\CMS\Core\Utility\PathUtility; |
| 22 | + |
| 23 | +/** |
| 24 | + * Parser for XML locallang file. |
| 25 | + */ |
| 26 | +class LocallangXmlParser extends \TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Associative array of "filename => parsed data" pairs. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + protected $parsedTargetFiles; |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns parsed representation of XML file. |
| 37 | + * |
| 38 | + * @param string $sourcePath Source file path |
| 39 | + * @param string $languageKey Language key |
| 40 | + * @return array |
| 41 | + */ |
| 42 | + public function getParsedData($sourcePath, $languageKey) |
| 43 | + { |
| 44 | + $this->sourcePath = $sourcePath; |
| 45 | + $this->languageKey = $languageKey; |
| 46 | + // Parse source |
| 47 | + $parsedSource = $this->parseXmlFile(); |
| 48 | + // Parse target |
| 49 | + $localizedTargetPath = $this->getLocalizedFileName($this->sourcePath, $this->languageKey); |
| 50 | + $targetPath = $this->languageKey !== 'default' && @is_file($localizedTargetPath) |
| 51 | + ? $localizedTargetPath : $this->sourcePath; |
| 52 | + try { |
| 53 | + $parsedTarget = $this->getParsedTargetData($targetPath); |
| 54 | + } catch (InvalidXmlFileException $e) { |
| 55 | + $parsedTarget = $this->getParsedTargetData($this->sourcePath); |
| 56 | + } |
| 57 | + $LOCAL_LANG = []; |
| 58 | + $LOCAL_LANG[$languageKey] = $parsedSource; |
| 59 | + ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG[$languageKey], $parsedTarget); |
| 60 | + return $LOCAL_LANG; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns array representation of XLIFF data, starting from a root node. |
| 65 | + * |
| 66 | + * @param \SimpleXMLElement $root XML root element |
| 67 | + * @param string $element Target or Source |
| 68 | + * @return array |
| 69 | + * @throws InvalidXmlFileException |
| 70 | + */ |
| 71 | + protected function doParsingFromRootForElement(\SimpleXMLElement $root, $element) |
| 72 | + { |
| 73 | + $bodyOfFileTag = $root->data->languageKey; |
| 74 | + if ($bodyOfFileTag === null) { |
| 75 | + throw new InvalidXmlFileException( |
| 76 | + 'Invalid locallang.xml language file "' . PathUtility::stripPathSitePrefix($this->sourcePath) . '"', |
| 77 | + 1487944884 |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if ($element === 'source' || $this->languageKey === 'default') { |
| 82 | + $parsedData = $this->getParsedDataForElement($bodyOfFileTag, $element); |
| 83 | + } else { |
| 84 | + $parsedData = []; |
| 85 | + } |
| 86 | + if ($element === 'target') { |
| 87 | + // Check if the source llxml file contains localized records |
| 88 | + $localizedBodyOfFileTag = $root->data->xpath('languageKey[@index=\'' . $this->languageKey . '\']'); |
| 89 | + if (isset($localizedBodyOfFileTag[0]) && $localizedBodyOfFileTag[0] instanceof \SimpleXMLElement) { |
| 90 | + $parsedDataTarget = $this->getParsedDataForElement($localizedBodyOfFileTag[0], $element); |
| 91 | + $mergedData = $parsedDataTarget + $parsedData; |
| 92 | + if ($this->languageKey === 'default') { |
| 93 | + $parsedData = array_intersect_key($mergedData, $parsedData, $parsedDataTarget); |
| 94 | + } else { |
| 95 | + $parsedData = array_intersect_key($mergedData, $parsedDataTarget); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return $parsedData; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Parse the given language key tag |
| 104 | + * |
| 105 | + * @param \SimpleXMLElement $bodyOfFileTag |
| 106 | + * @param string $element |
| 107 | + * @return array |
| 108 | + */ |
| 109 | + protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, $element) |
| 110 | + { |
| 111 | + $parsedData = []; |
| 112 | + $children = $bodyOfFileTag->children(); |
| 113 | + if ($children->count() === 0) { |
| 114 | + // Check for externally-referenced resource: |
| 115 | + // <languageKey index="fr">EXT:yourext/path/to/localized/locallang.xml</languageKey> |
| 116 | + $reference = sprintf('%s', $bodyOfFileTag); |
| 117 | + if (substr($reference, -4) === '.xml') { |
| 118 | + return $this->getParsedTargetData(GeneralUtility::getFileAbsFileName($reference)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + foreach ($children as $translationElement) { |
| 123 | + if ($translationElement->getName() === 'label') { |
| 124 | + $parsedData[(string)$translationElement['index']][0] = [ |
| 125 | + $element => (string)$translationElement |
| 126 | + ]; |
| 127 | + } |
| 128 | + } |
| 129 | + return $parsedData; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns array representation of XLIFF data, starting from a root node. |
| 134 | + * |
| 135 | + * @param \SimpleXMLElement $root A root node |
| 136 | + * @return array An array representing parsed XLIFF |
| 137 | + */ |
| 138 | + protected function doParsingFromRoot(\SimpleXMLElement $root) |
| 139 | + { |
| 140 | + return $this->doParsingFromRootForElement($root, 'source'); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns array representation of XLIFF data, starting from a root node. |
| 145 | + * |
| 146 | + * @param \SimpleXMLElement $root A root node |
| 147 | + * @return array An array representing parsed XLIFF |
| 148 | + */ |
| 149 | + protected function doParsingTargetFromRoot(\SimpleXMLElement $root) |
| 150 | + { |
| 151 | + return $this->doParsingFromRootForElement($root, 'target'); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns parsed representation of XML file. |
| 156 | + * |
| 157 | + * Parses XML if it wasn't done before. Caches parsed data. |
| 158 | + * |
| 159 | + * @param string $path An absolute path to XML file |
| 160 | + * @return array Parsed XML file |
| 161 | + */ |
| 162 | + public function getParsedTargetData($path) |
| 163 | + { |
| 164 | + if (!isset($this->parsedTargetFiles[$path])) { |
| 165 | + $this->parsedTargetFiles[$path] = $this->parseXmlTargetFile($path); |
| 166 | + } |
| 167 | + return $this->parsedTargetFiles[$path]; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Reads and parses XML file and returns internal representation of data. |
| 172 | + * |
| 173 | + * @param string $targetPath Path of the target file |
| 174 | + * @return array |
| 175 | + * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException |
| 176 | + */ |
| 177 | + protected function parseXmlTargetFile($targetPath) |
| 178 | + { |
| 179 | + $rootXmlNode = false; |
| 180 | + if (file_exists($targetPath)) { |
| 181 | + $xmlContent = file_get_contents($targetPath); |
| 182 | + // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept |
| 183 | + $previousValueOfEntityLoader = libxml_disable_entity_loader(true); |
| 184 | + $rootXmlNode = simplexml_load_string($xmlContent, \SimpleXMLElement::class, LIBXML_NOWARNING); |
| 185 | + libxml_disable_entity_loader($previousValueOfEntityLoader); |
| 186 | + } |
| 187 | + if ($rootXmlNode === false) { |
| 188 | + $xmlError = libxml_get_last_error(); |
| 189 | + throw new InvalidXmlFileException( |
| 190 | + 'The path provided does not point to existing and accessible well-formed XML file. Reason: ' |
| 191 | + . $xmlError->message . ' in ' . $targetPath . ', line ' . $xmlError->line, |
| 192 | + 1278155987 |
| 193 | + ); |
| 194 | + } |
| 195 | + return $this->doParsingTargetFromRoot($rootXmlNode); |
| 196 | + } |
| 197 | +} |
0 commit comments