/**
  * @return array
  */
 public function getStats()
 {
     $stats = array();
     $countByDomains = $this->storage->getCountTransUnitByDomains();
     foreach ($countByDomains as $domain => $total) {
         $stats[$domain] = array();
         $byLocale = $this->storage->getCountTranslationByLocales($domain);
         foreach ($this->localeManager->getLocales() as $locale) {
             $localeCount = isset($byLocale[$locale]) ? $byLocale[$locale] : 0;
             $stats[$domain][$locale] = array('keys' => $total, 'translated' => $localeCount, 'completed' => $total > 0 ? floor($localeCount / $total * 100) : 0);
         }
     }
     return $stats;
 }
 /**
  * Query the database to get translation resources and set it on the event.
  *
  * @param GetDatabaseResourcesEvent $event
  */
 public function onGetDatabaseResources(GetDatabaseResourcesEvent $event)
 {
     // prevent errors on command such as cache:clear if doctrine schema has not been updated yet
     if (StorageInterface::STORAGE_ORM == $this->storageType && !$this->storage->translationsTablesExist()) {
         $resources = array();
     } else {
         $resources = $this->storage->getTransUnitDomainsByLocale();
     }
     $event->setResources($resources);
 }
 /**
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     if ($event->isMasterRequest() && $this->isCacheExpired()) {
         $lastUpdateTime = $this->storage->getLatestUpdatedAt();
         if ($lastUpdateTime instanceof \DateTime) {
             $finder = new Finder();
             $finder->files()->in($this->cacheDirectory . '/translations')->date('< ' . $lastUpdateTime->format('Y-m-d H:i:s'));
             if ($finder->count() > 0) {
                 $this->translator->removeLocalesCacheFiles($this->managedLocales);
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     $catalogue = new MessageCatalogue($locale);
     $transUnits = $this->storage->getTransUnitsByLocaleAndDomain($locale, $domain);
     foreach ($transUnits as $transUnit) {
         foreach ($transUnit['translations'] as $translation) {
             if ($translation['locale'] == $locale) {
                 $catalogue->set($transUnit['key'], $translation['content'], $domain);
             }
         }
     }
     return $catalogue;
 }
 /**
  * {@inheritdoc}
  */
 public function create($name, $path, $flush = false)
 {
     $path = $this->getFileRelativePath($path);
     $class = $this->storage->getModelClass('file');
     $file = new $class();
     $file->setName($name);
     $file->setPath($path);
     $file->setHash($this->generateHash($name, $path));
     $this->storage->persist($file);
     if ($flush) {
         $this->storage->flush();
     }
     return $file;
 }
 /**
  * {@inheritdoc}
  */
 public function process(FormInterface $form, Request $request)
 {
     $valid = false;
     if ($request->isMethod('POST')) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $transUnit = $form->getData();
             $translations = $transUnit->filterNotBlankTranslations();
             // only keep translations with a content
             // link new translations to a file to be able to export them.
             foreach ($translations as $translation) {
                 if (!$translation->getFile()) {
                     $file = $this->fileManager->getFor(sprintf('%s.%s.yml', $transUnit->getDomain(), $translation->getLocale()), $this->rootDir . '/Resources/translations');
                     if ($file instanceof FileInterface) {
                         $translation->setFile($file);
                     }
                 }
             }
             if ($transUnit instanceof PropelTransUnit) {
                 // The setTranslations() method only accepts PropelCollections
                 $translations = new \PropelObjectCollection($translations);
             }
             $transUnit->setTranslations($translations);
             $this->storage->persist($transUnit);
             $this->storage->flush();
             $valid = true;
         }
     }
     return $valid;
 }
 /**
  * Updates a trans unit from the request.
  *
  * @param integer $id
  * @param Request $request
  * @throws NotFoundHttpException
  * @return \Lexik\Bundle\TranslationBundle\Model\TransUnit
  */
 public function updateFromRequest($id, Request $request)
 {
     $transUnit = $this->storage->getTransUnitById($id);
     if (!$transUnit) {
         throw new NotFoundHttpException(sprintf('No TransUnit found for "%s"', $id));
     }
     $translationsContent = array();
     foreach ($this->managedLoales as $locale) {
         $translationsContent[$locale] = $request->request->get($locale);
     }
     $this->transUnitManager->updateTranslationsContent($transUnit, $translationsContent);
     if ($transUnit instanceof TransUnitDocument) {
         $transUnit->convertMongoTimestamp();
     }
     $this->storage->flush();
     return $transUnit;
 }
 /**
  * @param TransUnitInterface $transUnit
  * @param string             $locale
  * @return bool
  */
 public function deleteTranslation(TransUnitInterface $transUnit, $locale)
 {
     try {
         $translation = $transUnit->getTranslation($locale);
         $this->storage->remove($translation);
         $this->storage->flush();
         return true;
     } catch (\Exception $e) {
         return false;
     }
 }
 /**
  * Impoort the given file and return the number of inserted translations.
  *
  * @param \Symfony\Component\Finder\SplFileInfo $file
  * @param boolean                               $forceUpdate  force update of the translations
  * @return int
  */
 public function import(\Symfony\Component\Finder\SplFileInfo $file, $forceUpdate = false)
 {
     $imported = 0;
     list($domain, $locale, $extention) = explode('.', $file->getFilename());
     if (isset($this->loaders[$extention])) {
         $messageCatalogue = $this->loaders[$extention]->load($file->getPathname(), $locale, $domain);
         $translationFile = $this->fileManager->getFor($file->getFilename(), $file->getPath());
         foreach ($messageCatalogue->all($domain) as $key => $content) {
             // skip empty translation values
             if (!isset($content)) {
                 continue;
             }
             $transUnit = $this->storage->getTransUnitByKeyAndDomain($key, $domain);
             if (!$transUnit instanceof TransUnitInterface) {
                 $transUnit = $this->transUnitManager->create($key, $domain);
             }
             $translation = $this->transUnitManager->addTranslation($transUnit, $locale, $content, $translationFile);
             if ($translation instanceof TranslationInterface) {
                 $imported++;
             } else {
                 if ($forceUpdate) {
                     $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content);
                     $imported++;
                 }
             }
             // convert MongoTimestamp objects to time to don't get an error in:
             // Doctrine\ODM\MongoDB\Mapping\Types\TimestampType::convertToDatabaseValue()
             if ($transUnit instanceof TransUnitDocument) {
                 $transUnit->convertMongoTimestamp();
             }
         }
         $this->storage->flush();
         // clear only Lexik entities
         foreach (array('file', 'trans_unit', 'translation') as $name) {
             $this->storage->clear($this->storage->getModelClass($name));
         }
     } else {
         throw new \RuntimeException(sprintf('No load found for "%s" format.', $extention));
     }
     return $imported;
 }
 /**
  * {@inheritdoc}
  */
 public function updateTranslationsContent(TransUnitInterface $transUnit, array $translations, $flush = false)
 {
     foreach ($translations as $locale => $content) {
         if (!empty($content)) {
             if ($transUnit->hasTranslation($locale)) {
                 $this->updateTranslation($transUnit, $locale, $content);
                 if ($this->storage instanceof PropelStorage) {
                     $this->storage->persist($transUnit);
                 }
             } else {
                 //We need to get a proper file for this translation
                 $file = $this->getTranslationFile($transUnit, $locale);
                 $this->addTranslation($transUnit, $locale, $content, $file);
             }
         }
     }
     if ($flush) {
         $this->storage->flush();
     }
 }