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