/** * @Route("/normalize/{projectId}/{erase}", name="normalize") * @ Method("POST") * @ParamConverter("project", class="TranslationsBundle:Project", options={"id" = "projectId"}) */ public function normalizeAction(Request $request, Project $project, $erase = '') { $this->init(); // completar los documentos a los que le falten subdocumentos de traducciones //$this->translationsManager->userHasProject($this->user, $project); $permissions = $this->translationsManager->getPermissionForUserAndProject($this->user, $project); $permissions = $permissions->getPermissions(); if ($permissions['general'] != Permission::OWNER) { return $this->printResult(array('result' => false, 'reason' => 'not enough permissions to do this')); } $managedLocales = explode(',', $project->getManagedLocales()); /** @var Translation[] $translations */ $translations = $this->getTranslationRepository()->findBy(array('projectId' => $project->getId())); $normalized = array(); foreach ($translations as $translation) { $bundle = ""; $transArray = $translation->getTranslations(); foreach ($managedLocales as $locale) { if (!isset($transArray[$locale])) { $transArray[$locale] = Translation::genTranslationItem(''); $normalized[] = $translation->getKey() . "[{$locale}]"; } else { if (!$bundle && isset($transArray[$locale]['fileName']) && $transArray[$locale]['fileName']) { if (preg_match("@/(?<bundle>\\w*?Bundle)/@", $transArray[$locale]['fileName'], $match)) { $bundle = $match['bundle']; } else { if (preg_match("@/app/@", $transArray[$locale]['fileName'])) { $bundle = "app*"; } } } } } if ($bundle && !$translation->getBundle()) { $normalized[] = $translation->getKey() . " -> " . $bundle; $translation->setBundle($bundle); } $translation->setTranslations($transArray); $this->dm->persist($translation); } $this->dm->flush(); if ($erase === 'erase-duplicates') { // eliminar los documentos que no tengan translation en ingles (para borrar duplicados) foreach ($translations as $translation) { $transArray = $translation->getTranslations(); if (!$transArray['en']['message']) { print 'erasing ... ' . $translation->getId() . '<br/>'; $this->dm->remove($translation); } } $this->dm->flush(); } return $this->printResult(array('result' => true, 'normalized' => $normalized)); }
/** * saves the message into $translation[$key][$locale][$message] and normalize rest of translations of this key * * @param Project $project * @param $criteria * @param $key * @param $locale * @param $message * * @return Translation */ public function putTranslation(Project $project, $criteria, $key, $locale, $message) { // first get the record if (strpos($criteria, "Bundle") != false) { $translation = $this->getTranslationRepository()->getTranslationByBundle($project->getId(), $criteria, $key); } else { $translation = $this->getTranslationRepository()->getTranslation($project->getId(), $criteria, $key); } if (!$translation) { return; } $managedLocales = explode(',', $project->getManagedLocales()); $translation = $this->normalizeTranslation($translation, $managedLocales); // now normalize (creates items for managed locales that not exists) $transArray = $translation->getTranslations(); $transArray[$locale] = array_merge($transArray[$locale], Translation::genTranslationItem($message)); $translation->setTranslations($transArray); // last, return return $translation; }