Exemplo n.º 1
0
 /**
  * @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'));
 }
 /**
  * @return \JMS\TranslationBundle\Model\Message\XliffMessage
  */
 private function createMessage($id, $desc, $source)
 {
     $message = new Message\XliffMessage($id, $this->domain);
     $message->addSource(new FileSource($source));
     $message->setDesc($desc);
     $message->setLocaleString($desc);
     $message->addNote('key: ' . $id);
     return $message;
 }
 /**
  * @param $message
  * @return Message\XliffMessage
  */
 private function makeXliffMessage(Message $message)
 {
     $newMessage = new Message\XliffMessage($message->getId(), $message->getDomain());
     $newMessage->setNew($message->isNew());
     $newMessage->setLocaleString($message->getLocaleString());
     $newMessage->setSources($message->getSources());
     $newMessage->addNote('key: ' . $message->getId());
     if ($desc = $message->getDesc()) {
         $newMessage->setDesc($desc);
     }
     return $newMessage;
 }
 public function testMergeScanned()
 {
     $existingMessage = new XliffMessage('foo');
     $existingMessage->setLocaleString('bar');
     $existingMessage->addSource(new FileSource('bar'));
     $existingMessage->setApproved(false);
     $existingMessage->setState(XliffMessage::STATE_NONE);
     $scannedMessage = new XliffMessage('foo');
     $scannedMessage->setDesc('foo');
     $scannedMessage->setApproved(true);
     $scannedMessage->setState(XliffMessage::STATE_TRANSLATED);
     $scannedMessage1 = new Message('foo');
     $scannedMessage1->setDesc('foo');
     $existingMessage1 = clone $existingMessage;
     $scannedMessage1 = clone $scannedMessage;
     $existingMessage1->mergeScanned($scannedMessage1);
     $this->assertEquals('foo', $existingMessage1->getDesc());
     $this->assertEquals('bar', $existingMessage1->getLocaleString());
     $this->assertFalse($existingMessage1->isNew());
     $this->assertEquals(array(), $existingMessage1->getSources());
     $this->assertTrue($existingMessage1->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage1->getState());
     $existingMessage2 = clone $existingMessage;
     $existingMessage2->setDesc('bar');
     $existingMessage2->setApproved(true);
     $existingMessage2->setState(XliffMessage::STATE_TRANSLATED);
     $scannedMessage2 = clone $scannedMessage;
     $scannedMessage2->setDesc('foo');
     $scannedMessage2->setApproved(false);
     $scannedMessage2->setState(XliffMessage::STATE_NONE);
     $existingMessage2->mergeScanned($scannedMessage2);
     $this->assertEquals('bar', $existingMessage2->getDesc());
     $this->assertFalse($existingMessage2->isNew());
     $this->assertEquals(array(), $existingMessage2->getSources());
     $this->assertTrue($existingMessage2->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage2->getState());
     $existingMessage3 = clone $existingMessage;
     $scannedMessage3 = clone $scannedMessage1;
     $existingMessage3->mergeScanned($scannedMessage3);
     $this->assertEquals('foo', $existingMessage3->getDesc());
     $this->assertEquals('bar', $existingMessage3->getLocaleString());
     $this->assertFalse($existingMessage3->isNew());
     $this->assertEquals(array(), $existingMessage3->getSources());
     $this->assertTrue($existingMessage3->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage3->getState());
     $existingMessage4 = clone $existingMessage;
     $existingMessage4->setDesc('bar');
     $existingMessage4->setApproved(true);
     $existingMessage4->setState(XliffMessage::STATE_TRANSLATED);
     $scannedMessage4 = clone $scannedMessage1;
     $scannedMessage4->setDesc('foo');
     $existingMessage4->mergeScanned($scannedMessage4);
     $this->assertEquals('bar', $existingMessage4->getDesc());
     $this->assertEquals(array(), $existingMessage4->getSources());
     $this->assertTrue($existingMessage4->isApproved());
     $this->assertEquals(XliffMessage::STATE_TRANSLATED, $existingMessage4->getState());
 }