コード例 #1
0
 public static function newFromArray($catalog, $key, $locale, $data, $bundle = '', $file = '')
 {
     $trans = new Translation();
     $trans->setKey($key);
     $trans->setDomain($catalog);
     $trans->setLocale($locale);
     $trans->setMessage($data['message']);
     $trans->setUpdatedAt(self::dateTimeFromArray($data['updatedAt']));
     $trans->setBundle($bundle ?: '');
     $trans->setFile($file);
     return $trans;
 }
コード例 #2
0
 /**
  * @param string    $bundleName
  * @param string    $file
  * @param string    $key
  * @param string    $locale
  * @param string    $content
  * @param string    $catalog
  * @param \DateTime $updatedAt
  */
 protected function updateOrInsertEntry($bundleName, $file, $key, $locale, $content, $catalog, \DateTime $updatedAt = null)
 {
     $shortFile = str_replace($this->srcDir, '', $file);
     $shortFile = str_replace('\\/', '/', $shortFile);
     $mod = $updatedAt ?: new \DateTime(date('c', filemtime($file)));
     /** @var Translation $entry */
     $entry = $this->translationRepository->findOneBy(array('domain' => $bundleName, 'key' => $key, 'locale' => $locale));
     if (!$entry) {
         $entry = new Translation();
     }
     $entry->setDomain($catalog);
     $entry->setBundle($bundleName);
     $entry->setFile($shortFile);
     $entry->setKey($key);
     $entry->setMessage($content);
     $entry->setUpdatedAt($mod);
     $entry->setLocale($locale);
     $this->em->persist($entry);
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = $output;
     $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 documents ***</info>');
     $config = $this->getContainer()->getParameter('jlaso_translations');
     $managedLocales = $config['managed_locales'];
     $finder = new Finder();
     $finder->files()->in($this->rootDir)->name('jlaso_translations.yml');
     $this->output->writeln($this->rootDir);
     $transDocs = array();
     foreach ($finder as $file) {
         $yml = $file->getRealpath();
         $relativePath = $file->getRelativePath();
         $fileName = $file->getRelativePathname();
         $rules = Yaml::parse($yml);
         //var_dump($rules);
         if (preg_match('/\\/(\\w*)Bundle\\//', $relativePath, $matches)) {
             $bundle = $matches[1] . 'Bundle';
         } else {
             $bundle = "app*";
         }
         $this->output->writeln(PHP_EOL . $this->center($bundle));
         if (isset($rules['files'])) {
             foreach ($rules['files'] as $key => $fileRule) {
                 foreach ($managedLocales as $locale) {
                     $transFile = $relativePath . '/' . str_replace('%locale%', $locale, $fileRule);
                     if (file_exists($transFile)) {
                         $this->output->writeln(sprintf('<info>Processing file "%s"</info>', $transFile));
                         $result = $this->syncDoc($bundle, $key, $locale, $transFile);
                     } else {
                         $this->output->writeln(sprintf('<comment>File "%s" not found</comment>', $transFile));
                         $result = $this->getDoc($bundle, $key, $locale, $transFile);
                     }
                 }
             }
         }
     }
     die('ok');
     $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();
             $data[$key][$locale] = array('message' => $message->getMessage(), 'updatedAt' => $message->getUpdatedAt()->format('c'));
         }
         //print_r($data); die;
         $this->output->writeln('uploadKeys("' . $catalog . '", $data)');
         $result = $this->clientApiService->uploadKeys($catalog, $data);
     }
     $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);
         //var_dump($result); die;
         foreach ($result['data'] as $key => $data) {
             foreach ($data as $locale => $messageData) {
                 //$this->output->writeln(sprintf("\t|-- key %s:%s/%s ... ", $catalog, $key, $locale));
                 echo '.';
                 $trans = Translation::newFromArray($catalog, $key, $locale, $messageData);
                 $this->em->persist($trans);
             }
         }
         // meter las traducciones en local
     }
     $this->output->writeln(PHP_EOL . sprintf('<info>Flushing to DB ...</info>', $catalog));
     $this->em->flush();
     $this->output->writeln(PHP_EOL . '<info>Clearing SF cache ...</info>');
     /** @var Translator $translator */
     //$translator = $this->getContainer()->get('translator');
     //$translator->removeLocalesCacheFiles($managedLocales);
     exec("rm -rf " . $this->rootDir . "/app/cache/*");
     $this->output->writeln('');
 }
コード例 #4
0
 /**
  * {@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('');
 }