/** * @Route("/translations/{projectId}/{catalog}/new-key", name="translations_new_key") * @Template() * @ParamConverter("project", class="TranslationsBundle:Project", options={"id" = "projectId"}) */ public function newKeyAction(Request $request, Project $project, $catalog) { $this->init(); $permission = $this->translationsManager->getPermissionForUserAndProject($this->user, $project); if (!$permission instanceof Permission) { return $this->printResult(array('result' => false, 'reason' => $this->translator->trans('error.acl.not_enough_permissions_to_manage_this_project'))); } $catalog = trim($catalog); $bundle = trim($request->get('bundle')); $keyName = trim($request->get('key')); $current = trim($request->get('current')); if (!$bundle || !$keyName) { return $this->printResult(array('result' => false, 'reason' => $this->translator->trans('translations.new_key_dialog.error.not_enough_parameters'))); } $translationRepository = $this->getTranslationRepository(); $key = $translationRepository->findOneBy(array('projectId' => $project->getId(), 'catalog' => $catalog, 'key' => $keyName)); if ($key) { return $this->printResult(array('result' => false, 'reason' => $this->translator->trans('translations.new_key_dialog.error.key_already_exists', array('%key%' => $keyName)))); } $managedLocales = explode(',', $project->getManagedLocales()); $translation = new Translation(); $translation->setProjectId($project->getId()); $translation->setCatalog($catalog); $translation->setBundle($bundle); $translation->setKey($keyName); $translation = $this->translationsManager->normalizeTranslation($translation, $managedLocales); $this->dm->persist($translation); $this->dm->flush($translation); $this->translationsManager->saveLog($translation->getId(), '', $keyName, TranslationLog::NEW_KEY, $this->user, TranslationLog::TRANSLATIONS_GROUP); /** @var TranslationRepository $translationRepository */ $translationRepository = $this->dm->getRepository('TranslationsBundle:Translation'); if (strpos($current, "Bundle") !== false) { $keys = $translationRepository->getKeysByBundle($project->getId(), $current); } else { $keys = $translationRepository->getKeys($project->getId(), $current); } $tree = $this->keysToPlainArray($keys); $languages = $this->getLanguageRepository()->findAllLanguageIn($managedLocales, true); $bundles = $this->getProjectInfoRepository()->getBundles($project->getId()); $html = $this->renderView("TranslationsBundle:Default:messages.html.twig", array('translation' => $translation, 'managed_locales' => $managedLocales, 'permissions' => $permission->getPermissions(), 'languages' => $languages, 'bundles' => $bundles)); return $this->printResult(array('result' => true, 'tree' => $tree, 'key' => $keyName, 'html' => $html)); }
/** * * $data[key][locale] * { * message, * updatedAt, * bundle, * fileName, * } * */ protected function receiveKeys(Project $project, $catalog, $data) { if (!$project || !$catalog || !$data) { return $this->exception('Validation exceptions, missing parameters'); } $result = array(); /** @var Translation[] $messages */ $messages = $this->getTranslationRepository()->findBy(array('projectId' => $project->getId(), 'catalog' => $catalog)); if ($this->debug) { echo sprintf("found %d in translations\n", count($messages)); } foreach ($messages as $message) { $key = $message->getKey(); $bundle = ''; $translations = $message->getTranslations(); $dirty = false; if (count($translations)) { foreach ($translations as $locale => $translation) { if (isset($data[$key][$locale])) { $current = $data[$key][$locale]; $updatedAt = new \DateTime($current['updatedAt']); if ($message->getUpdatedAt()->sec < intval($updatedAt->format("U"))) { $result[$key][$locale] = $current['updatedAt']; $translations[$locale]['message'] = $current['message']; $translations[$locale]['updatedAt'] = $updatedAt; if (isset($current['approved'])) { $translations[$locale]['approved'] = $current['approved']; } $dirty = true; } $translations[$locale]['fileName'] = $current['fileName']; if (!$bundle) { if ($current['bundle']) { $bundle = $current['bundle']; } else { preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches); if (isset($matches['bundle'])) { $bundle = $matches['bundle']; } } } unset($data[$key][$locale]); } } if ($dirty) { $message->setBundle($bundle); $message->setTranslations($translations); $this->dm->persist($message); } } } if ($this->debug) { echo sprintf("found %d keys in data\n", count($data)); } foreach ($data as $key => $dataLocale) { if (count($dataLocale)) { if ($this->debug) { echo sprintf("processing key %s\n", $key); } $bundle = ''; $translation = new Translation(); $translation->setCatalog($catalog); $translation->setKey($key); $translation->setProjectId($project->getId()); $translation->setBundle($bundle); $translations = array(); foreach ($dataLocale as $locale => $message) { $translations[$locale] = array('message' => $message['message'], 'updatedAt' => new \DateTime($message['updatedAt']), 'approved' => true, 'fileName' => $message['fileName']); if (!$bundle) { if ($message['bundle']) { $bundle = $message['bundle']; } else { preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches); if (isset($matches['bundle'])) { $bundle = $matches['bundle']; } } } } $translation->setBundle($bundle); $translation->setTranslations($translations); $this->dm->persist($translation); } } $this->dm->flush(); // normalize $this->translationsManager->regenerateProjectInfo($project->getId()); return $this->resultOk($result); }