public function dump(MessageCatalogue $catalogue, $domain = 'messages', $filePath = null)
 {
     $structure = $catalogue->getDomain($domain)->all();
     if ($this->prettyPrint) {
         $tmpStructure = array();
         foreach ($structure as $id => $message) {
             $pointer =& $tmpStructure;
             $parts = explode('.', $id);
             // this algorithm only works if the messages are alphabetically
             // ordered, in particular it must be guaranteed that parent paths
             // are before sub-paths, e.g.
             // array_keys($structure) = array('foo.bar', 'foo.bar.baz')
             // but NOT: array_keys($structure) = array('foo.bar.baz', 'foo.bar')
             for ($i = 0, $c = count($parts); $i < $c; $i++) {
                 if ($i + 1 === $c) {
                     $pointer[$parts[$i]] = $message;
                     break;
                 }
                 if (!isset($pointer[$parts[$i]])) {
                     $pointer[$parts[$i]] = array();
                 }
                 if ($pointer[$parts[$i]] instanceof Message) {
                     $subPath = implode('.', array_slice($parts, $i));
                     $pointer[$subPath] = $message;
                     break;
                 }
                 $pointer =& $pointer[$parts[$i]];
             }
         }
         $structure = $tmpStructure;
         unset($tmpStructure);
     }
     return $this->dumpStructure($structure);
 }
 public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
 {
     $newCatalogue = new MessageCatalogue();
     $newCatalogue->setLocale($catalogue->getLocale());
     foreach (array_keys($catalogue->getDomains()) as $catalogueDomainString) {
         if ($catalogue->getLocale() !== 'en' && $this->hasEnglishCatalogue($filePath)) {
             $englishCatalogue = $this->loadEnglishCatalogue($filePath, $domain, $format);
         }
         $domainMessageCollection = $catalogue->getDomain($catalogueDomainString);
         /** @var Message $message */
         foreach ($domainMessageCollection->all() as $message) {
             if ($message->getDomain() !== $domain) {
                 continue;
             }
             $newMessage = $this->makeXliffMessage($message);
             if ($message->getId() === $message->getSourceString()) {
                 if (isset($englishCatalogue)) {
                     try {
                         $newMessage->setDesc($englishCatalogue->get($message->getId(), $message->getDomain())->getLocaleString());
                     } catch (InvalidArgumentException $e) {
                         continue;
                     }
                 } else {
                     $newMessage->setDesc($message->getLocaleString());
                 }
             }
             $newCatalogue->add($newMessage);
         }
     }
     $this->innerFileWriter->write($newCatalogue, $domain, $filePath, $format);
 }
示例#3
0
 /**
  * @param \JMS\TranslationBundle\Model\MessageCatalogue $catalogue
  * @param string $domain
  * @param string $filePath
  * @param string $format
  * @throws \JMS\TranslationBundle\Exception\InvalidArgumentException
  */
 public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
 {
     if (!isset($this->dumpers[$format])) {
         throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $format));
     }
     // sort messages before dumping
     $catalogue->getDomain($domain)->sort(function ($a, $b) {
         return strcmp($a->getId(), $b->getId());
     });
     file_put_contents($filePath, $this->dumpers[$format]->dump($catalogue, $domain));
 }
示例#4
0
 /**
  * @param \JMS\TranslationBundle\Model\MessageCatalogue $catalogue
  * @param string $domain
  * @param string $filePath
  * @param string $format
  * @throws \JMS\TranslationBundle\Exception\InvalidArgumentException
  */
 public function write(MessageCatalogue $catalogue, $domain, $filePath, $format)
 {
     if (!isset($this->dumpers[$format])) {
         $allowedFormats = array_keys($this->dumpers);
         $allowedFormatsString = join(',', $allowedFormats);
         throw new InvalidArgumentException(sprintf('The format "%s" is not supported. Allowed formats:%s', $format, $allowedFormatsString));
     }
     // sort messages before dumping
     $catalogue->getDomain($domain)->sort(function ($a, $b) {
         return strcmp($a->getId(), $b->getId());
     });
     file_put_contents($filePath, $this->dumpers[$format]->dump($catalogue, $domain));
 }
示例#5
0
 /**
  * @param \JMS\TranslationBundle\Model\MessageCatalogue $domain
  * @param $filePath
  * @param $format
  * @param array $outputOptions
  * @throws \JMS\TranslationBundle\Exception\InvalidArgumentException
  */
 public function write(MessageCatalogue $catalogue, $domain, $filePath, $format, $outputOptions)
 {
     if (!isset($this->dumpers[$format])) {
         throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $format));
     }
     // sort messages before dumping
     $catalogue->getDomain($domain)->sort(function ($a, $b) {
         return strcmp($a->getId(), $b->getId());
     });
     $dumper = $this->dumpers[$format];
     if ($dumper instanceof \JMS\TranslationBundle\Translation\Dumper\XliffDumper) {
         if (isset($outputOptions['add_date'])) {
             $dumper->setAddDate($outputOptions['add_date']);
         }
         if (isset($outputOptions['add_filerefs'])) {
             $dumper->setAddFileRefs($outputOptions['add_filerefs']);
         }
     }
     file_put_contents($filePath, $dumper->dump($catalogue, $domain, $filePath));
 }
 public function dump(MessageCatalogue $catalogue, $domain = 'messages', $filePath = null)
 {
     $symfonyCatalogue = new SymfonyCatalogue($catalogue->getLocale());
     foreach ($catalogue->getDomain($domain)->all() as $id => $message) {
         $symfonyCatalogue->add(array($id => $message->getLocaleString()), $domain);
     }
     $tmpPath = sys_get_temp_dir() . '/' . uniqid('translation', false);
     if (!is_dir($tmpPath) && false === @mkdir($tmpPath, 0777, true)) {
         throw new RuntimeException(sprintf('Could not create temporary directory "%s".', $tmpPath));
     }
     $this->dumper->dump($symfonyCatalogue, array('path' => $tmpPath));
     if (!is_file($tmpFile = $tmpPath . '/' . $domain . '.' . $catalogue->getLocale() . '.' . $this->format)) {
         throw new RuntimeException(sprintf('Could not find dumped translation file "%s".', $tmpFile));
     }
     $contents = file_get_contents($tmpFile);
     $fs = new Filesystem();
     $fs->remove($tmpPath);
     if ('' === $contents) {
         throw new RuntimeException(sprintf('Could not dump message catalogue using dumper "%s". It could be that it is not compatible.', get_class($this->dumper)));
     }
     return $contents;
 }
 /**
  * @param \JMS\TranslationBundle\Model\MessageCatalogue $domain
  * @return string
  */
 public function dump(MessageCatalogue $catalogue, $domain = 'messages')
 {
     $doc = new \DOMDocument('1.0', 'utf-8');
     $doc->formatOutput = true;
     $doc->appendChild($root = $doc->createElement('xliff'));
     $root->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $root->setAttribute('xmlns:jms', 'urn:jms:translation');
     $root->setAttribute('version', '1.2');
     $root->appendChild($file = $doc->createElement('file'));
     if ($this->addDate) {
         $date = new \DateTime();
         $file->setAttribute('date', $date->format('Y-m-d\\TH:i:s\\Z'));
     }
     $file->setAttribute('source-language', $this->sourceLanguage);
     $file->setAttribute('target-language', $catalogue->getLocale());
     $file->setAttribute('datatype', 'plaintext');
     $file->setAttribute('original', 'not.available');
     $file->appendChild($header = $doc->createElement('header'));
     $header->appendChild($tool = $doc->createElement('tool'));
     $tool->setAttribute('tool-id', 'JMSTranslationBundle');
     $tool->setAttribute('tool-name', 'JMSTranslationBundle');
     $tool->setAttribute('tool-version', JMSTranslationBundle::VERSION);
     $header->appendChild($note = $doc->createElement('note'));
     $note->appendChild($doc->createTextNode('The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.'));
     $file->appendChild($body = $doc->createElement('body'));
     foreach ($catalogue->getDomain($domain)->all() as $id => $message) {
         $body->appendChild($unit = $doc->createElement('trans-unit'));
         $unit->setAttribute('id', hash('sha1', $id));
         $unit->setAttribute('resname', $id);
         $unit->appendChild($source = $doc->createElement('source'));
         if (preg_match('/[<>&]/', $message->getSourceString())) {
             $source->appendChild($doc->createCDATASection($message->getSourceString()));
         } else {
             $source->appendChild($doc->createTextNode($message->getSourceString()));
         }
         $unit->appendChild($target = $doc->createElement('target'));
         if (preg_match('/[<>&]/', $message->getLocaleString())) {
             $target->appendChild($doc->createCDATASection($message->getLocaleString()));
         } else {
             $target->appendChild($doc->createTextNode($message->getLocaleString()));
         }
         if ($message->isNew()) {
             $target->setAttribute('state', 'new');
         }
         // As per the OASIS XLIFF 1.2 non-XLIFF elements must be at the end of the <trans-unit>
         if ($sources = $message->getSources()) {
             foreach ($sources as $source) {
                 if ($source instanceof FileSource) {
                     $unit->appendChild($refFile = $doc->createElement('jms:reference-file', $source->getPath()));
                     if ($source->getLine()) {
                         $refFile->setAttribute('line', $source->getLine());
                     }
                     if ($source->getColumn()) {
                         $refFile->setAttribute('column', $source->getColumn());
                     }
                     continue;
                 }
                 $unit->appendChild($doc->createElementNS('jms:reference', (string) $source));
             }
         }
         if ($meaning = $message->getMeaning()) {
             $unit->setAttribute('extradata', 'Meaning: ' . $meaning);
         }
     }
     return $doc->saveXML();
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testGetDomainWhenDomainDoesNotExist()
 {
     $catalogue = new MessageCatalogue();
     $catalogue->getDomain('messages');
 }