/** * 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; }
/** * 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; }
private function manageFileUpload(TranslationFile $translationFile) { if ($translationFile->getFile()) { $translationFile->setCreatedAt(new \DateTime()); $file = $translationFile->getFile()->getRealPath(); if (!is_file($file)) { throw new \Exception('File does not exists!'); } $translations = $this->yamlParser->parse(file_get_contents($file)); foreach ($translations as $key => $value) { $keys = explode('__', $key); list($domain, $translationKey, $id, $locale) = $keys; $transUnit = $this->translationStorage->getTransUnitById($id); if (in_array($locale, $this->managedLocales)) { $this->transUnitManager->updateTranslation($transUnit, $locale, $value, true); } } $this->translator->removeLocalesCacheFiles($this->managedLocales); } }
/** * {@inheritdoc} */ public function createFormData() { return $this->transUnitManager->newInstance($this->localeManager->getLocales()); }
/** * {@inheritdoc} */ public function createFormData() { return $this->transUnitManager->newInstance($this->managedLoales); }