/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->init(); if ($input->getOption('force') != 'yes') { /** @var DialogHelper $dialog */ $dialog = $this->getHelper('dialog'); if (!$dialog->askConfirmation($output, '<question>The local DB will be erased, it is ok ?</question>', false)) { die('Please, repeat the command with --force==yes in order to update remote DB with local changes'); } } // truncate local translations table $this->translationRepository->truncateTranslations(); $config = $this->getContainer()->getParameter('translations_api'); $managedLocales = $config['managed_locales']; if (!count($managedLocales)) { die('not found managed locales' . PHP_EOL); } $managedLocales[] = self::COMMENTS; //$apiConfig = $this->getContainer()->getParameter('jlaso_translations_api_access'); $this->output->writeln('<info>*** Syncing bundles translation files ***</info>'); $allLocalBundles = $this->getApplication()->getKernel()->getBundles(); /** @var BundleInterface[] $allLocalBundles */ foreach ($allLocalBundles as $bundle) { // just added bundles that are within / src as the other are not responsible for their translations if (strpos($bundle->getPath(), $this->srcDir) === 0) { $name = $bundle->getName(); if (!isset($allBundles[$name])) { $allBundles[$name] = $name; } } } // adding a fake bundle to process translations from /app/Resources/translations $allBundles[self::APP_BUNDLE_KEY] = self::APP_BUNDLE_KEY; $catalogs = $this->getLocalCatalogs(); // proccess local keys foreach ($allBundles as $bundleName) { $this->output->writeln(PHP_EOL . sprintf("<error>%s</error>", $this->center($bundleName))); foreach ($managedLocales as $locale) { $this->output->writeln(sprintf('· %s/%s', $bundleName, $locale)); foreach ($catalogs as $catalog) { if (self::APP_BUNDLE_KEY == $bundleName) { $bundle = null; $filePattern = $this->srcDir . '../app/Resources/translations/%s.%s.yml'; } else { $bundle = $this->getBundleByName($bundleName); $filePattern = $bundle->getPath() . '/Resources/translations/%s.%s.yml'; } $fileName = sprintf($filePattern, $catalog, $locale); if (!file_exists($fileName)) { //$this->output->writeln(sprintf("· · <comment>File '%s' not found</comment>", $fileName)); } else { // $maxDate = new \DateTime(date('c',filemtime($fileName))); $hasChanged = false; $localKeys = $this->getYamlAsArray($fileName); $this->output->writeln(sprintf("· · <info>Processing</info> '%s', found <info>%d</info> translations", $this->fileTrim($fileName), count($localKeys))); //$this->output->writeln(sprintf("\t|-- <info>getKeys</info> informs that there are %d keys ", count($remoteKeys))); foreach ($localKeys as $localKey => $message) { $this->output->writeln(sprintf("\t|-- key %s:%s/%s ... ", $bundleName, $localKey, $locale)); $this->updateOrInsertEntry($bundleName, $fileName, $localKey, $locale, $message, $catalog); } } //unlink($fileName); //$this->output->writeln(''); } } } $this->output->writeln(PHP_EOL . '<info>Flushing to DB ...</info>'); $this->em->flush(); if ($this->input->getOption('cache-clear')) { $this->output->writeln(PHP_EOL . '<info>Removing translations cache files ...</info>'); $this->getContainer()->get('translator')->removeLocalesCacheFiles($managedLocales); } $this->output->writeln(''); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $ymlOptions = array('regenerate' => false, 'backup' => false, 'blank' => false); $yml = $this->input->getOption('yml'); if ($yml) { $aux = explode(",", $yml); if (count($aux)) { foreach ($aux as $option) { $ymlOptions[$option] = true; } } } if (count($ymlOptions) != 3) { var_dump($ymlOptions); die('Sorry, but you can use only regenerate,blank and backup with --yml option'); } $this->init($input->getOption('address'), $input->getOption('port')); $config = $this->getContainer()->getParameter('translations_api'); $managedLocales = $config['managed_locales']; $this->output->writeln(PHP_EOL . '<info>*** Syncing translations ***</info>'); /** * uploading local catalog keys (from local table) to remote server */ if ($input->getOption('upload-first') == 'yes') { $catalogs = $this->translationsRepository->getCatalogs(); foreach ($catalogs as $catalog) { // data para enviar al servidor $data = array(); $this->output->writeln(PHP_EOL . sprintf('<info>Processing catalog %s ...</info>', $catalog)); /** @var Translation[] $messages */ $messages = $this->translationsRepository->findBy(array('domain' => $catalog)); foreach ($messages as $message) { $key = $message->getKey(); $locale = $message->getLocale(); $bundle = $message->getBundle(); $fileName = $message->getFile(); $data[$key][$locale] = array('message' => $message->getMessage(), 'updatedAt' => $message->getUpdatedAt()->format('c'), 'fileName' => $message->getFile(), 'bundle' => $message->getBundle()); } $this->output->writeln('uploadKeys("' . $catalog . '", $data)'); $result = $this->clientApiService->uploadKeys($catalog, $data); } } /** * download the remote catalogs and integrate into local table (previously truncate local table) */ // truncate local translations table $this->translationsRepository->truncateTranslations(); $result = $this->clientApiService->getCatalogIndex(); if ($result['result']) { $catalogs = $result['catalogs']; } else { die('error getting catalogs'); } foreach ($catalogs as $catalog) { $this->output->writeln(PHP_EOL . sprintf('<info>Processing catalog %s ...</info>', $catalog)); $result = $this->clientApiService->downloadKeys($catalog); file_put_contents(sys_get_temp_dir() . DIRECTORY_SEPARATOR . $catalog . '.json', json_encode($result)); $bundles = $result['bundles']; foreach ($result['data'] as $key => $data) { foreach ($data as $locale => $messageData) { echo '.'; $fileName = isset($messageData['fileName']) ? $messageData['fileName'] : ''; $trans = Translation::newFromArray($catalog, $key, $locale, $messageData, $bundles[$key], $fileName); $this->em->persist($trans); } } } $this->output->writeln(PHP_EOL . '<info>Flushing to DB ...</info>'); $this->em->flush(); /** * regeneration of local .yml files if user wants */ /** @var DialogHelper $dialog */ $dialog = $this->getHelper('dialog'); if ($ymlOptions['regenerate']) { $bundles = $this->translationsRepository->getBundles(); foreach ($bundles as $bundle) { if (!$bundle) { continue; } $keys = array(); $filenames = array(); $scheme = ""; // in order to deduce filename from other keys $translations = $this->translationsRepository->getKeysByBundle($bundle); foreach ($translations as $translation) { $locale = $translation->getLocale(); $file = $translation->getFile(); if ($file && $locale && !$scheme) { $scheme = str_replace(".{$locale}.", ".%s.", $file); break; } } foreach ($translations as $translation) { $locale = $translation->getLocale(); $file = $translation->getFile(); if ($locale && !$file && $scheme) { $file = sprintf($scheme, $locale); } if ($file && $locale) { if (!isset($filenames[$locale])) { $filenames[$locale] = $file; } $keys[$locale][$translation->getKey()] = $translation->getMessage(); } } foreach ($filenames as $locale => $file) { $this->output->writeln(sprintf('Generating <info>"%s"</info> ...', $file)); $subKeys = $keys[$locale]; $file = dirname($this->rootDir) . '/src/' . $file; if ($ymlOptions['blank']) { foreach ($subKeys as $key => $value) { if (!$value) { $subKeys[$key] = $key; } } } if ($ymlOptions['backup'] && file_exists($file)) { copy($file, $file . '.' . date('U')); } @mkdir(dirname($file), 0777, true); file_put_contents($file, ArrayTools::prettyYamlDump($subKeys)); } } } /** * erasing cached translations files */ $this->output->writeln(PHP_EOL . '<info>Clearing SF cache ...</info>'); $finder = new Finder(); $finder->files()->in($this->rootDir . "/cache")->name('*'); foreach ($finder as $file) { $fileFull = $file->getRealpath(); //$relativePath = $file->getRelativePath(); $fileName = $file->getRelativePathname(); if (preg_match('!/translations/.+$!i', $fileName)) { $this->output->writeln('removing ' . $fileName); unlink($file); } } $this->output->writeln(''); }