/**
  * {@inheritDoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     if (!array_key_exists('path', $options)) {
         throw new \InvalidArgumentException('The file dumper needs a path option.');
     }
     $path = $options['path'];
     $bundleName = isset($options['bundleName']) ? $options['bundleName'] : '';
     $generatedFiles = array();
     // save a file for each domain
     foreach ($messages->getDomains() as $domain) {
         $fileName = !empty($bundleName) ? sprintf('%s.%s.%s.xlsx', $bundleName, $domain, $messages->getLocale()) : sprintf('%s.%s.xlsx', $domain, $messages->getLocale());
         // create the exporter file
         $exporter = new ExcelExporter($path);
         // create the header row
         $row = array('key', $messages->getLocale());
         $exporter->writeRow($row);
         // format the data and write to file
         $data = $this->format($messages, $domain);
         foreach ($data as $row) {
             $exporter->writeRow($row);
         }
         $generatedFiles[] = $exporter->generateFile($fileName);
     }
     return $generatedFiles;
 }
Beispiel #2
0
 /**
  * Loads translation messages from a directory to the catalogue.
  *
  * @param string $directory the directory to look into
  * @param MessageCatalogue $catalogue the catalogue
  */
 public function loadMessages($directory, MessageCatalogue $catalogue)
 {
     foreach ($this->loaders as $format => $loader) {
         // load any existing translation files
         $finder = new Finder();
         $extension = $catalogue->getLocale() . '.' . $format;
         $files = $finder->files()->name('*.' . $extension)->in($directory);
         foreach ($files as $file) {
             $domain = substr($file->getFileName(), 0, -1 * strlen($extension) - 1);
             $catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
         }
     }
 }
 /**
  * Dumps the message catalogue.
  *
  * @param MessageCatalogue $messages The message catalogue
  * @param array            $options  Options that are used by the dumper
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     $this->loadAll = false;
     $locale = $messages->getLocale();
     try {
         foreach ($messages->getDomains() as $eachDomain) {
             foreach ($messages->all($eachDomain) as $eachKey => $eachTranslation) {
                 $queryBuilder = $this->entityManager->createQueryBuilder();
                 $queryBuilder->select('t')->from('MjrLibraryEntitiesBundle:System\\Translation', 't')->where($queryBuilder->expr()->andX($queryBuilder->expr()->eq('t.Identity', '?1'), $queryBuilder->expr()->eq('t.Locale', '?2')));
                 $query = $this->entityManager->createQuery($queryBuilder->getDQL());
                 $query->setParameters(array(1 => $eachKey, 2 => $locale));
                 $result = $query->getArrayResult();
                 if (count($result) < 1) {
                     $entry = new Translation();
                     $entry->setLocale($locale);
                     $entry->setIdentity($eachKey);
                     $entry->setTranslation($eachKey);
                     $this->entityManager->persist($entry);
                     $this->entityManager->flush();
                 }
                 unset($query, $queryBuilder, $entry, $eachKey, $eachTranslation);
             }
         }
     } catch (\Exception $ex) {
         var_dump($ex);
         die;
     }
 }
Beispiel #4
0
 /**
  * {@inheritDoc}
  */
 protected function format(MessageCatalogue $messages, $domain)
 {
     $dom = new \DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $xliff = $dom->appendChild($dom->createElement('xliff'));
     $xliff->setAttribute('version', '1.2');
     $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $xliffFile = $xliff->appendChild($dom->createElement('file'));
     $xliffFile->setAttribute('source-language', $messages->getLocale());
     $xliffFile->setAttribute('datatype', 'plaintext');
     $xliffFile->setAttribute('original', 'file.ext');
     $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
     $id = 1;
     foreach ($messages->all($domain) as $source => $target) {
         $trans = $dom->createElement('trans-unit');
         $trans->setAttribute('id', $id);
         $s = $trans->appendChild($dom->createElement('source'));
         $s->appendChild($dom->createTextNode($source));
         $t = $trans->appendChild($dom->createElement('target'));
         $t->appendChild($dom->createTextNode($target));
         $xliffBody->appendChild($trans);
         $id++;
     }
     return $dom->saveXML();
 }
 /**
  * {@inheritdoc}
  */
 protected function format(MessageCatalogue $messages, $domain)
 {
     $dom = new \DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $xliff = $dom->appendChild($dom->createElement('xliff'));
     $xliff->setAttribute('version', '1.2');
     $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $xliffFile = $xliff->appendChild($dom->createElement('file'));
     $xliffFile->setAttribute('source-language', $messages->getLocale());
     $xliffFile->setAttribute('datatype', 'plaintext');
     $xliffFile->setAttribute('original', 'file.ext');
     $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
     foreach ($messages->all($domain) as $source => $target) {
         $translation = $dom->createElement('trans-unit');
         $translation->setAttribute('id', md5($source));
         $translation->setAttribute('resname', $source);
         $s = $translation->appendChild($dom->createElement('source'));
         $s->appendChild($dom->createTextNode($source));
         // Does the target contain characters requiring a CDATA section?
         $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
         $t = $translation->appendChild($dom->createElement('target'));
         $t->appendChild($text);
         $xliffBody->appendChild($translation);
     }
     return $dom->saveXML();
 }
 /**
  * {@inheritDoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     if (!array_key_exists('path', $options)) {
         throw new \InvalidArgumentException('The file dumper needs a path option.');
     }
     // save a file for each domain
     $generatedFiles = array();
     $bundleName = isset($options['bundleName']) ? $options['bundleName'] : '';
     foreach ($messages->getDomains() as $domain) {
         $fileName = !empty($bundleName) ? sprintf('%s.%s.%s.%s', $bundleName, $domain, $messages->getLocale(), $this->getExtension()) : sprintf('%s.%s.%s', $domain, $messages->getLocale(), $this->getExtension());
         $fullpath = sprintf('%s/%s', $options['path'], $fileName);
         // save file
         file_put_contents($fullpath, $this->format($messages, $domain));
         $generatedFiles[] = $fullpath;
     }
     return $generatedFiles;
 }
Beispiel #7
0
 /**
  * Loads translation messages from a directory to the catalogue.
  * 
  * @param string $directory the directory to look into
  * @param MessageCatalogue $catalogue the catalogue
  */
 public function loadMessages($directory, MessageCatalogue $catalogue)
 {
     foreach($this->loaders as $format => $loader) {
         // load any existing translation files
         $finder = new Finder();
         $files = $finder->files()->name('*.'.$catalogue->getLocale().$format)->in($directory);
         foreach ($files as $file) {
             $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale').$format) - 1);
             $catalogue->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
         }
     }
 }
Beispiel #8
0
 /**
  * {@inheritdoc}
  */
 protected function format(MessageCatalogue $messages, $domain)
 {
     $dom = new \DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $xliff = $dom->appendChild($dom->createElement('xliff'));
     $xliff->setAttribute('version', '1.2');
     $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $xliffFile = $xliff->appendChild($dom->createElement('file'));
     $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
     $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
     $xliffFile->setAttribute('datatype', 'plaintext');
     $xliffFile->setAttribute('original', 'file.ext');
     $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
     foreach ($messages->all($domain) as $source => $target) {
         $translation = $dom->createElement('trans-unit');
         $translation->setAttribute('id', md5($source));
         $translation->setAttribute('resname', $source);
         $s = $translation->appendChild($dom->createElement('source'));
         $s->appendChild($dom->createTextNode($source));
         // Does the target contain characters requiring a CDATA section?
         $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
         $targetElement = $dom->createElement('target');
         $metadata = $messages->getMetadata($source, $domain);
         if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
             foreach ($metadata['target-attributes'] as $name => $value) {
                 $targetElement->setAttribute($name, $value);
             }
         }
         $t = $translation->appendChild($targetElement);
         $t->appendChild($text);
         if ($this->hasMetadataArrayInfo('notes', $metadata)) {
             foreach ($metadata['notes'] as $note) {
                 if (!isset($note['content'])) {
                     continue;
                 }
                 $n = $translation->appendChild($dom->createElement('note'));
                 $n->appendChild($dom->createTextNode($note['content']));
                 if (isset($note['priority'])) {
                     $n->setAttribute('priority', $note['priority']);
                 }
                 if (isset($note['from'])) {
                     $n->setAttribute('from', $note['from']);
                 }
             }
         }
         $xliffBody->appendChild($translation);
     }
     return $dom->saveXML();
 }
Beispiel #9
0
 /**
  * {@inheritDoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     if (!array_key_exists('path', $options)) {
         throw new \InvalidArgumentException('The file dumper need a path options.');
     }
     
     // save a file for each domain
     foreach ($messages->getDomains() as $domain) {
         $file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension();
         // backup
         if (file_exists($options['path'].$file)) {
             copy($options['path'].$file, $options['path'].'/'.$file.'~');
         }
         // save file
         file_put_contents($options['path'].'/'.$file, $this->format($messages, $domain));
     }
 }
Beispiel #10
0
 /**
  * {@inheritdoc}
  */
 public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
 {
     $output = 'msgid ""' . "\n";
     $output .= 'msgstr ""' . "\n";
     $output .= '"Content-Type: text/plain; charset=UTF-8\\n"' . "\n";
     $output .= '"Content-Transfer-Encoding: 8bit\\n"' . "\n";
     $output .= '"Language: ' . $messages->getLocale() . '\\n"' . "\n";
     $output .= "\n";
     $newLine = false;
     foreach ($messages->all($domain) as $source => $target) {
         if ($newLine) {
             $output .= "\n";
         } else {
             $newLine = true;
         }
         $output .= sprintf('msgid "%s"' . "\n", $this->escape($source));
         $output .= sprintf('msgstr "%s"', $this->escape($target));
     }
     return $output;
 }
Beispiel #11
0
 /**
  * {@inheritdoc}
  */
 protected function format(MessageCatalogue $messages, $domain)
 {
     $dom = new \DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $xliff = $dom->appendChild($dom->createElement('xliff'));
     $xliff->setAttribute('version', '1.2');
     $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $xliffFile = $xliff->appendChild($dom->createElement('file'));
     $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
     $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
     $xliffFile->setAttribute('datatype', 'plaintext');
     $xliffFile->setAttribute('original', 'file.ext');
     $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
     foreach ($messages->all($domain) as $source => $target) {
         $translation = $dom->createElement('trans-unit');
         $translation->setAttribute('id', md5($source));
         $translation->setAttribute('resname', $source);
         $s = $translation->appendChild($dom->createElement('source'));
         $s->appendChild($dom->createTextNode($source));
         $t = $translation->appendChild($dom->createElement('target'));
         $t->appendChild($dom->createTextNode($target));
         $metadata = $messages->getMetadata($source, $domain);
         if (null !== $metadata && array_key_exists('notes', $metadata) && is_array($metadata['notes'])) {
             foreach ($metadata['notes'] as $note) {
                 if (!isset($note['content'])) {
                     continue;
                 }
                 $n = $translation->appendChild($dom->createElement('note'));
                 $n->appendChild($dom->createTextNode($note['content']));
                 if (isset($note['priority'])) {
                     $n->setAttribute('priority', $note['priority']);
                 }
                 if (isset($note['from'])) {
                     $n->setAttribute('from', $note['from']);
                 }
             }
         }
         $xliffBody->appendChild($translation);
     }
     return $dom->saveXML();
 }
 /**
  * {@inheritdoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     $connection = \Propel::getConnection($this->query->getDbName());
     $connection->beginTransaction();
     $now = new \DateTime();
     $locale = $messages->getLocale();
     foreach ($messages->getDomains() as $eachDomain) {
         foreach ($messages->all($eachDomain) as $eachKey => $eachTranslation) {
             $query = clone $this->query;
             $query->filterBy($this->getColumnPhpname('locale'), $locale)->filterBy($this->getColumnPhpname('domain'), $eachDomain)->filterBy($this->getColumnPhpname('key'), $eachKey);
             $translation = $query->findOneOrCreate($connection);
             $translation->setByName($this->getColumnPhpname('translation'), (string) $eachTranslation);
             $translation->setByName($this->getColumnPhpname('updated_at'), $now);
             $translation->save($connection);
         }
     }
     if (!$connection->commit()) {
         $connection->rollBack();
         throw new \RuntimeException(sprintf('An error occurred while committing the transaction. [%s: %s]', $connection->errorCode(), $connection->errorInfo()));
     }
 }
Beispiel #13
0
 /**
  * {@inheritDoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     if (!array_key_exists('path', $options)) {
         throw new \InvalidArgumentException('The file dumper needs a path option.');
     }
     // save a file for each domain
     foreach ($messages->getDomains() as $domain) {
         // backup
         $fullpath = $options['path'] . '/' . $this->getRelativePath($domain, $messages->getLocale());
         if (file_exists($fullpath)) {
             copy($fullpath, $fullpath . '~');
         } else {
             $directory = dirname($fullpath);
             if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
                 throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
             }
         }
         // save file
         file_put_contents($fullpath, $this->format($messages, $domain));
     }
 }
Beispiel #14
0
 /**
  * {@inheritdoc}
  */
 public function dump(MessageCatalogue $messages, $options = array())
 {
     if (!array_key_exists('path', $options)) {
         throw new \InvalidArgumentException('The file dumper needs a path option.');
     }
     // save a file for each domain
     foreach ($messages->getDomains() as $domain) {
         // backup
         $fullpath = $options['path'] . '/' . $this->getRelativePath($domain, $messages->getLocale());
         if (file_exists($fullpath)) {
             if ($this->backup) {
                 @trigger_error('Creating a backup while dumping a message catalogue is deprecated since version 3.1 and will be removed in 4.0. Use TranslationWriter::disableBackup() to disable the backup.', E_USER_DEPRECATED);
                 copy($fullpath, $fullpath . '~');
             }
         } else {
             $directory = dirname($fullpath);
             if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
                 throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
             }
         }
         // save file
         file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
     }
 }
Beispiel #15
0
 private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = array())
 {
     $dom = new \DOMDocument('1.0', 'utf-8');
     $dom->formatOutput = true;
     $xliff = $dom->appendChild($dom->createElement('xliff'));
     $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
     $xliff->setAttribute('version', '2.0');
     $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
     $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
     $xliffFile = $xliff->appendChild($dom->createElement('file'));
     $xliffFile->setAttribute('id', $domain . '.' . $messages->getLocale());
     foreach ($messages->all($domain) as $source => $target) {
         $translation = $dom->createElement('unit');
         $translation->setAttribute('id', md5($source));
         $segment = $translation->appendChild($dom->createElement('segment'));
         $s = $segment->appendChild($dom->createElement('source'));
         $s->appendChild($dom->createTextNode($source));
         // Does the target contain characters requiring a CDATA section?
         $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
         $targetElement = $dom->createElement('target');
         $metadata = $messages->getMetadata($source, $domain);
         if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
             foreach ($metadata['target-attributes'] as $name => $value) {
                 $targetElement->setAttribute($name, $value);
             }
         }
         $t = $segment->appendChild($targetElement);
         $t->appendChild($text);
         $xliffFile->appendChild($translation);
     }
     return $dom->saveXML();
 }
 /**
  * Set the new id into the MessageCatalogue
  * @param $id
  * @param $domain
  * @param MessageCatalogue $catalogue
  */
 protected function setNewId($id, $domain, MessageCatalogue $catalogue)
 {
     $value = $this->checkForDefaultValue($id, $catalogue->getLocale());
     if (!$value && $catalogue->getFallbackCatalogue()) {
         $value = $this->checkForDefaultValue($id, $catalogue->getFallbackCatalogue()->getLocale());
     }
     if (!$value) {
         $value = $this->decorate($id);
     }
     $catalogue->set($id, $value, $domain);
 }
 public function testGetLocale()
 {
     $catalogue = new MessageCatalogue('en');
     $this->assertEquals('en', $catalogue->getLocale());
 }
 private function filterCatalogue(MessageCatalogue $catalogue, $domain)
 {
     $filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
     if ($messages = $catalogue->all($domain)) {
         $filteredCatalogue->add($messages, $domain);
     }
     foreach ($catalogue->getResources() as $resource) {
         $filteredCatalogue->addResource($resource);
     }
     if ($metadata = $catalogue->getMetadata('', $domain)) {
         foreach ($metadata as $k => $v) {
             $filteredCatalogue->setMetadata($k, $v, $domain);
         }
     }
     return $filteredCatalogue;
 }
Beispiel #19
0
 /**
  * @param string $format
  * @param string $resource
  * @param string $domain
  * @param MessageCatalogue $catalogue
  * @throws LoaderNotFoundException
  */
 public function loadResource($format, $resource, $domain, MessageCatalogue $catalogue)
 {
     if (!isset($this->loaders[$format])) {
         if (!isset($this->serviceIds[$format])) {
             throw new LoaderNotFoundException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
         }
         $this->loaders[$format] = $this->serviceLocator->getService($this->serviceIds[$format]);
         unset($this->serviceIds[$format]);
     }
     $catalogue->addCatalogue($this->loaders[$format]->load($resource, $catalogue->getLocale(), $domain));
 }
Beispiel #20
0
 /**
  * Заполняет каталог данными из БД
  * @param MessageCatalogue $catalogue
  */
 public function fillCatalogue(MessageCatalogue $catalogue)
 {
     /** @var Translation[] $res */
     $res = $this->getEntityManager()->getRepository('OctavaMuiBundle:Translation')->findAll();
     $messages = [];
     foreach ($res as $object) {
         $translations = $object->getTranslations();
         if (empty($messages[$object->getDomain()])) {
             $messages[$object->getDomain()] = [];
         }
         if (!empty($translations[$catalogue->getLocale()])) {
             $messages[$object->getDomain()][$object->getSource()] = $translations[$catalogue->getLocale()];
         }
     }
     foreach ($messages as $domain => $data) {
         $catalogue->add($data, $domain);
     }
 }