ArrayLoader.php
2 months ago
CsvFileLoader.php
2 months ago
FileLoader.php
2 months ago
IcuDatFileLoader.php
2 months ago
IcuResFileLoader.php
2 months ago
IniFileLoader.php
2 months ago
JsonFileLoader.php
2 months ago
LoaderInterface.php
2 months ago
MoFileLoader.php
2 months ago
PhpFileLoader.php
2 months ago
PoFileLoader.php
2 months ago
QtFileLoader.php
2 months ago
XliffFileLoader.php
2 months ago
YamlFileLoader.php
2 months ago
QtFileLoader.php
67 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 ProfilePressVendor\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Config\Resource\FileResource; |
| 14 | use ProfilePressVendor\Symfony\Component\Config\Util\XmlUtils; |
| 15 | use ProfilePressVendor\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 16 | use ProfilePressVendor\Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 17 | use ProfilePressVendor\Symfony\Component\Translation\Exception\RuntimeException; |
| 18 | use ProfilePressVendor\Symfony\Component\Translation\MessageCatalogue; |
| 19 | /** |
| 20 | * QtFileLoader loads translations from QT Translations XML files. |
| 21 | * |
| 22 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
| 23 | */ |
| 24 | class QtFileLoader implements LoaderInterface |
| 25 | { |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public function load($resource, string $locale, string $domain = 'messages') |
| 30 | { |
| 31 | if (!class_exists(XmlUtils::class)) { |
| 32 | throw new RuntimeException('Loading translations from the QT format requires the Symfony Config component.'); |
| 33 | } |
| 34 | if (!stream_is_local($resource)) { |
| 35 | throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); |
| 36 | } |
| 37 | if (!file_exists($resource)) { |
| 38 | throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); |
| 39 | } |
| 40 | try { |
| 41 | $dom = XmlUtils::loadFile($resource); |
| 42 | } catch (\InvalidArgumentException $e) { |
| 43 | throw new InvalidResourceException(sprintf('Unable to load "%s".', $resource), $e->getCode(), $e); |
| 44 | } |
| 45 | $internalErrors = libxml_use_internal_errors(\true); |
| 46 | libxml_clear_errors(); |
| 47 | $xpath = new \DOMXPath($dom); |
| 48 | $nodes = $xpath->evaluate('//TS/context/name[text()="' . $domain . '"]'); |
| 49 | $catalogue = new MessageCatalogue($locale); |
| 50 | if (1 == $nodes->length) { |
| 51 | $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); |
| 52 | foreach ($translations as $translation) { |
| 53 | $translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue; |
| 54 | if (!empty($translationValue)) { |
| 55 | $catalogue->set((string) $translation->getElementsByTagName('source')->item(0)->nodeValue, $translationValue, $domain); |
| 56 | } |
| 57 | $translation = $translation->nextSibling; |
| 58 | } |
| 59 | if (class_exists(FileResource::class)) { |
| 60 | $catalogue->addResource(new FileResource($resource)); |
| 61 | } |
| 62 | } |
| 63 | libxml_use_internal_errors($internalErrors); |
| 64 | return $catalogue; |
| 65 | } |
| 66 | } |
| 67 |