/** * {@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; }
/** * 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; }
/** * Get the proper File for this TransUnit and locale * * @param TransUnitInterface $transUnit * @param string $locale * * @return FileInterface|null */ protected function getTranslationFile(TransUnitInterface &$transUnit, $locale) { $file = null; foreach ($transUnit->getTranslations() as $translationModel) { if (null !== ($file = $translationModel->getFile())) { break; } } //if we found a file if ($file !== null) { //make sure we got the correct file for this locale and domain $name = sprintf('%s.%s.%s', $file->getDomain(), $locale, $file->getExtention()); $file = $this->fileManager->getFor($name, $this->kernelRootDir . DIRECTORY_SEPARATOR . $file->getPath()); } return $file; }