Esempio n. 1
0
 /**
  * Execute
  *
  * @param InputInterface  $input  input
  * @param OutputInterface $output output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Import labels</info>');
     try {
         $em = $this->getEntityManager();
         $labelRepo = $em->getRepository('BigfootCoreBundle:TranslatableLabel');
         $transRepo = $this->getContainer()->get('bigfoot_core.translation.repository');
         $importDir = $this->getContainer()->getParameter('import_dir') . 'translations/';
         $langs = array('fr', 'de', 'es', 'it', 'nl');
         /** @var ProgressHelper $progress */
         $progress = $this->getHelperSet()->get('progress');
         $files = scandir($importDir);
         $files = array_diff($files, array('.', '..'));
         $changedLabels = array('hotel.show.restaurant.type.autres' => 'hotel.show.restaurant.type.autre', 'destination.sheet.hotel' => 'destination.sheet.hotels');
         /* IMPORT FILES */
         $i = 1;
         foreach ($files as $fileName) {
             $file = $importDir . $fileName;
             $content = file_get_contents($file);
             file_put_contents($file, iconv(mb_detect_encoding($content, mb_detect_order(), true), "UTF-8", $content));
             $nbLabels = 0;
             $handle = fopen($file, 'r');
             while (!feof($handle)) {
                 fgetcsv($handle, null, ';', '"');
                 $nbLabels++;
             }
             $output->writeln(sprintf(' > <comment>Importing file %s</comment>', $fileName));
             $progress->start($output, $nbLabels - 1);
             rewind($handle);
             fgetcsv($handle, null, ';', '"');
             while (($line = fgetcsv($handle, null, ';', '"')) !== false) {
                 list($domain, $name, $value_fr, $value_en, $value_de, $value_es, $value_it, $value_nl) = $line;
                 if (!$domain) {
                     $domain = 'messages';
                 }
                 if (array_key_exists($name, $changedLabels)) {
                     $name = $changedLabels[$name];
                 }
                 if (!($label = $labelRepo->findOneBy(array('name' => $name, 'domain' => $domain)))) {
                     $label = new TranslatableLabel();
                     $label->setName($name);
                     $label->setDomain($domain);
                 }
                 $label->setValue($value_en);
                 $label->setPlural(strpos($value_en, '|') !== false);
                 $label->setMultiline(strpos($value_en, "\n") !== false);
                 foreach ($langs as $locale) {
                     $value = sprintf('value_%s', $locale);
                     if ($label->getId()) {
                         $transRepo->translate($label, 'value', $locale, ${$value});
                     } else {
                         $label->addTranslation(new TranslatableLabelTranslation($locale, 'value', ${$value}));
                     }
                 }
                 $em->persist($label);
                 $progress->advance();
             }
             $output->writeln(' > <comment>Flushing</comment>');
             $em->flush();
             $em->clear();
             $progress->finish();
             $i++;
         }
         $output->writeln(' > <comment>OK</comment>');
     } catch (\Exception $e) {
         $output->writeln(' > <error>Erreur : ' . $e->getMessage() . '</error>');
     }
 }
 /**
  * {@inheritdoc}
  *
  * @throws \InvalidArgumentException When the target directory does not exist
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $target = rtrim($input->getArgument('target'), '/');
     $overwrite = $input->getOption('overwrite');
     $noDelete = $input->getOption('no-delete');
     if (!is_dir($target) && !file_exists($target)) {
         throw new \InvalidArgumentException(sprintf('The target "%s" does not exist.', $input->getArgument('target')));
     }
     /** @var EntityManager $em */
     $em = $this->getContainer()->get('doctrine')->getManager();
     /** @var TranslatableLabelRepository $repo */
     $repo = $em->getRepository('BigfootCoreBundle:TranslatableLabel');
     $transRepo = $this->getContainer()->get('bigfoot_core.translation.repository');
     $defaultLocale = $this->getContainer()->getParameter('locale');
     /** @var ProgressBar $progress */
     $progress = new ProgressBar($output, 30);
     /** @var ContextService $context */
     $context = $this->getContainer()->get('bigfoot_context.service.context');
     $locales = array_keys($context->getValues('language'));
     $files = array();
     if (is_dir($target)) {
         $files = glob($target . '/*.yml');
     } else {
         $files[] = $target;
     }
     $i = 0;
     $processedLabels = array();
     foreach ($files as $file) {
         $fileName = pathinfo($file, PATHINFO_FILENAME);
         $content = Yaml::parse(file_get_contents($file));
         if ($content) {
             $nbTranslations = count($content);
             $output->writeln(sprintf(' > <comment>Importing file %s with %s translations</comment>', $fileName, $nbTranslations));
             $progress->start($output, $nbTranslations);
             foreach ($content as $name => $translation) {
                 if (substr_count($fileName, '.') == 1) {
                     $name = $fileName . '.' . $name;
                 }
                 $domain = isset($translation['domain']) && $translation['domain'] ? $translation['domain'] : 'messages';
                 $label = $repo->findOneBy(array('name' => $name, 'domain' => $domain));
                 if (!$label) {
                     $label = new TranslatableLabel();
                     $label->setName($name);
                     $label->setDomain($domain);
                 }
                 if (isset($translation['description'])) {
                     if (is_array($translation['description'])) {
                         foreach ($translation['description'] as $locale => $description) {
                             if ($locale == $defaultLocale) {
                                 $label->setDescription($description);
                             } else {
                                 $label->addTranslation(new TranslatableLabelTranslation($locale, 'description', $description));
                             }
                         }
                     } else {
                         $label->setDescription($translation['description']);
                     }
                 }
                 if (isset($translation['plural'])) {
                     $label->setPlural((bool) $translation['plural']);
                 }
                 if (isset($translation['multiline'])) {
                     $label->setMultiline((bool) $translation['multiline']);
                 }
                 if (isset($translation['richtext'])) {
                     $label->setRichtext((bool) $translation['richtext']);
                 }
                 if (isset($translation['value']) && ($overwrite || !$label->getId())) {
                     if (!is_array($translation['value'])) {
                         $translation['value'] = array($defaultLocale => $translation['value']);
                     }
                     foreach ($locales as $locale) {
                         $value = '';
                         if (isset($translation['value'][$locale])) {
                             $value = $translation['value'][$locale];
                         }
                         if ($locale == $defaultLocale) {
                             $label->setValue($value);
                         } elseif ($label->getId()) {
                             $transRepo->translate($label, 'value', $locale, $value);
                         } else {
                             $label->addTranslation(new TranslatableLabelTranslation($locale, 'value', $value));
                         }
                     }
                 }
                 $em->persist($label);
                 if (0 == $i % 100) {
                     $em->flush();
                     $em->clear();
                 }
                 $processedLabels[] = $name . '-' . $domain;
                 $progress->advance();
                 $i++;
             }
             $progress->finish();
         }
     }
     $em->flush();
     if (!$noDelete) {
         $labels = $repo->findAll();
         /** @var TranslatableLabel $label */
         foreach ($labels as $label) {
             $nameDomain = $label->getName() . '-' . $label->getDomain();
             if (!in_array($nameDomain, $processedLabels)) {
                 $em->remove($label);
             }
         }
         $em->flush();
     }
     /** @var TranslatableLabelManager $labelManager */
     $labelManager = $this->getContainer()->get('bigfoot_core.manager.translatable_label');
     $labelManager->clearTranslationCache();
 }