| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Config\Resource\FileResource; |
| 15 |
use Symfony\Component\Config\Util\XmlUtils; |
| 16 |
use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 17 |
use Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 18 |
use Symfony\Component\Translation\Exception\RuntimeException; |
| 19 |
use Symfony\Component\Translation\MessageCatalogue; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class QtFileLoader implements LoaderInterface |
| 27 |
{ |
| 28 |
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue |
| 29 |
{ |
| 30 |
if (!class_exists(XmlUtils::class)) { |
| 31 |
throw new RuntimeException('Loading translations from the QT format requires the Symfony Config component.'); |
| 32 |
} |
| 33 |
|
| 34 |
if (!stream_is_local($resource)) { |
| 35 |
throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource)); |
| 36 |
} |
| 37 |
|
| 38 |
if (!file_exists($resource)) { |
| 39 |
throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource)); |
| 40 |
} |
| 41 |
|
| 42 |
try { |
| 43 |
$dom = XmlUtils::loadFile($resource); |
| 44 |
} catch (\InvalidArgumentException $e) { |
| 45 |
throw new InvalidResourceException(\sprintf('Unable to load "%s".', $resource), $e->getCode(), $e); |
| 46 |
} |
| 47 |
|
| 48 |
$internalErrors = libxml_use_internal_errors(true); |
| 49 |
libxml_clear_errors(); |
| 50 |
|
| 51 |
$xpath = new \DOMXPath($dom); |
| 52 |
$nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]'); |
| 53 |
|
| 54 |
$catalogue = new MessageCatalogue($locale); |
| 55 |
if (1 == $nodes->length) { |
| 56 |
$translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); |
| 57 |
foreach ($translations as $translation) { |
| 58 |
$translationValue = (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue; |
| 59 |
|
| 60 |
if ($translationValue) { |
| 61 |
$catalogue->set( |
| 62 |
(string) $translation->getElementsByTagName('source')->item(0)->nodeValue, |
| 63 |
$translationValue, |
| 64 |
$domain |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (class_exists(FileResource::class)) { |
| 70 |
$catalogue->addResource(new FileResource($resource)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
libxml_use_internal_errors($internalErrors); |
| 75 |
|
| 76 |
return $catalogue; |
| 77 |
} |
| 78 |
} |