/** * @param mixed $resource * @param string $locale * @param string $domain * @return MessageCatalogue */ public function load($resource, $locale, $domain = 'messages') { $previous = libxml_use_internal_errors(true); if (false === ($doc = simplexml_load_file($resource))) { libxml_use_internal_errors($previous); $libxmlError = libxml_get_last_error(); throw new RuntimeException(sprintf('Could not load XML-file "%s": %s', $resource, $libxmlError->message)); } libxml_use_internal_errors($previous); $doc->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); $doc->registerXPathNamespace('jms', 'urn:jms:translation'); $hasReferenceFiles = in_array('urn:jms:translation', $doc->getNamespaces(true)); $catalogue = new MessageCatalogue(); $catalogue->setLocale($locale); /** @var \SimpleXMLElement $trans */ foreach ($doc->xpath('//xliff:trans-unit') as $trans) { $id = ($resName = (string) $trans->attributes()->resname) ? $resName : (string) $trans->source; /** @var Message $m */ $m = Message::create($id, $domain)->setDesc((string) $trans->source)->setLocaleString((string) $trans->target); $m->setApproved($trans['approved'] == 'yes'); if (isset($trans->target['state'])) { $m->setState((string) $trans->target['state']); } // Create closure $addNoteToMessage = function (Message $m, $note) { $m->addNote((string) $note, isset($note['from']) ? (string) $note['from'] : null); }; // If the translation has a note if (isset($trans->note)) { // If we have more than one note. We can't use is_array becuase $trans->note is a \SimpleXmlElement if (count($trans->note) > 1) { foreach ($trans->note as $note) { $addNoteToMessage($m, $note); } } else { $addNoteToMessage($m, $trans->note); } } $catalogue->add($m); if ($hasReferenceFiles) { foreach ($trans->xpath('./jms:reference-file') as $file) { $line = (string) $file->attributes()->line; $column = (string) $file->attributes()->column; $m->addSource(new FileSource((string) $file, $line ? (int) $line : null, $column ? (int) $column : null)); } } if ($meaning = (string) $trans->attributes()->extradata) { if (0 === strpos($meaning, 'Meaning: ')) { $meaning = substr($meaning, 9); } $m->setMeaning($meaning); } if (!($state = (string) $trans->target->attributes()->state) || 'new' !== $state) { $m->setNew(false); } } return $catalogue; }
public function testLoadWithSymfonyFormat() { $loader = new XliffLoader(); $expected = new MessageCatalogue(); $expected->setLocale('en'); $expected->add(XliffMessage::create('foo1')->setDesc('foo1')->setLocaleString('bar')->setNew(false)); $expected->add(XliffMessage::create('foo2')->setDesc('foo2')->setLocaleString('bar')->setNew(false)); $expected->add(XliffMessage::create('foo3')->setDesc('foo3')->setLocaleString('bar')->setNew(false)); $expected->add(XliffMessage::create('foo4')->setDesc('foo4')->setLocaleString('bar')->setNew(false)); $this->assertEquals($expected, $loader->load(__DIR__ . '/Symfony/xliff/old_format.xml', 'en')); }