コード例 #1
0
    /**
     * @see Command
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $twig = $this->getContainer()->get('twig');
        $this->prefix = $input->getOption('prefix');

        if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) {
            $output->writeln('You must choose one of --force or --dump-messages');
        } else {

            // get bundle directory
            $foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
            $bundleTransPath = $foundBundle->getPath() . '/Resources/translations';
            $output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));

            $output->writeln('Parsing files.');

            // load any messages from templates
            $this->messages = new \Symfony\Component\Translation\MessageCatalogue($input->getArgument('locale'));
            $finder = new Finder();
            $files = $finder->files()->name('*.html.twig')->in($foundBundle->getPath() . '/Resources/views/');
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing template <comment>%s</comment>', $file->getPathname()));
                $tree = $twig->parse($twig->tokenize(file_get_contents($file->getPathname())));
                $this->_crawlNode($tree);
            }

            // load any existing yml translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.yml')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.yml') - 1);
                $yml_loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
                $this->messages->addCatalogue($yml_loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing xliff translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.xliff')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.xliff') - 1);
                $loader = new \Symfony\Component\Translation\Loader\XliffFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing php translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.php')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.php') - 1);
                $loader = new \Symfony\Component\Translation\Loader\PhpFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // load any existing pot translation files
            $finder = new Finder();
            $files = $finder->files()->name('*.' . $input->getArgument('locale') . '.pot')->in($bundleTransPath);
            foreach ($files as $file) {
                $output->writeln(sprintf(' > parsing translation <comment>%s</comment>', $file->getPathname()));
                $domain = substr($file->getFileName(), 0, strrpos($file->getFileName(), $input->getArgument('locale') . '.pot') - 1);
                $loader = new \BCC\ExtraToolsBundle\Translation\Loader\PotFileLoader();
                $this->messages->addCatalogue($loader->load($file->getPathname(), $input->getArgument('locale'), $domain));
            }

            // show compiled list of messages
            if($input->getOption('dump-messages') === true){
                foreach ($this->messages->getDomains() as $domain) {
                    $output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
                    $output->writeln(\Symfony\Component\Yaml\Yaml::dump($this->messages->all($domain),10));
                }
            }

            // save the files
            if($input->getOption('force') === true) {
                $output->writeln("\nWriting files.\n");
                $path = $foundBundle->getPath() . '/Resources/translations/';
                if ($input->getOption('output-format') == 'yml') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\YmlFormatter();
                } elseif ($input->getOption('output-format') == 'php') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\PhpFormatter();
                } elseif ($input->getOption('output-format') == 'pot') {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\PotFormatter();
                } else {
                    $formatter = new \BCC\ExtraToolsBundle\Translation\Formatter\XliffFormatter($input->getOption('source-lang'));
                }
                foreach ($this->messages->getDomains() as $domain) {
                    $file = $domain . '.' . $input->getArgument('locale') . '.' . $input->getOption('output-format');
                    if (file_exists($path . $file)) {
                        copy($path . $file, $path . '~' . $file . '.bak');
                    }
                    $output->writeln(sprintf(' > generating <comment>%s</comment>', $path . $file));
                    file_put_contents($path . $file, $formatter->format($this->messages->all($domain)));
                }
            }
        }
    }
コード例 #2
0
 /**
  * This method generate the form for errors translations
  */
 public function initFormErrors()
 {
     $name_var = $this->translations_informations[$this->type_selected]['var'];
     $GLOBALS[$name_var] = $this->fileExists();
     $count_empty = array();
     /* List files to parse */
     $string_to_translate = array();
     $file_by_directory = $this->getFileToParseByTypeTranslation();
     if ($modules = $this->getListModules()) {
         foreach ($modules as $module) {
             if (is_dir(_PS_MODULE_DIR_ . $module) && !in_array($module, self::$ignore_folder)) {
                 $file_by_directory['php'] = array_merge($file_by_directory['php'], $this->listFiles(_PS_MODULE_DIR_ . $module . '/', array(), 'php'));
             }
         }
     }
     foreach ($file_by_directory['php'] as $dir => $files) {
         foreach ($files as $file) {
             if (preg_match('/\\.php$/', $file) && Tools::file_exists_cache($file_path = $dir . $file) && !in_array($file, self::$ignore_folder)) {
                 if (!filesize($file_path)) {
                     continue;
                 }
                 // Get content for this file
                 $content = file_get_contents($file_path);
                 // Parse this content
                 $matches = $this->userParseFile($content, $this->type_selected);
                 foreach ($matches as $key) {
                     if (array_key_exists(md5($key), $GLOBALS[$name_var])) {
                         $string_to_translate[$key]['trad'] = html_entity_decode($GLOBALS[$name_var][md5($key)], ENT_COMPAT, 'UTF-8');
                     } else {
                         $string_to_translate[$key]['trad'] = '';
                         if (!isset($count_empty[$key])) {
                             $count_empty[$key] = 1;
                         }
                     }
                     $string_to_translate[$key]['use_sprintf'] = $this->checkIfKeyUseSprintf($key);
                 }
             }
         }
     }
     //adding sf2 form translations
     $sf2_loader = new Symfony\Component\Translation\Loader\XliffFileLoader();
     try {
         $sf2_trans = $sf2_loader->load(_PS_VENDOR_DIR_ . '/symfony/symfony/src/Symfony/Component/Validator/Resources/translations/validators.' . $this->lang_selected->iso_code . '.xlf', $this->lang_selected->iso_code);
     } catch (\Exception $e) {
         $sf2_trans = $sf2_loader->load(_PS_VENDOR_DIR_ . '/symfony/symfony/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf', $this->lang_selected->iso_code);
     }
     foreach ($sf2_trans->all()['messages'] as $k => $v) {
         if (array_key_exists(md5($k), $GLOBALS[$name_var])) {
             $string_to_translate[$k]['trad'] = html_entity_decode($GLOBALS[$name_var][md5($k)], ENT_COMPAT, 'UTF-8');
         } else {
             $string_to_translate[$k]['trad'] = '';
             if (!isset($count_empty[$k])) {
                 $count_empty[$k] = 1;
             }
         }
         $string_to_translate[$k]['use_sprintf'] = false;
     }
     $this->tpl_view_vars = array_merge($this->tpl_view_vars, array('count' => count($string_to_translate), 'cancel_url' => $this->context->link->getAdminLink('AdminTranslations'), 'limit_warning' => $this->displayLimitPostWarning(count($string_to_translate)), 'mod_security_warning' => Tools::apacheModExists('mod_security'), 'errorsArray' => $string_to_translate, 'missing_translations' => $count_empty));
     $this->initToolbar();
     $this->base_tpl_view = 'translation_errors.tpl';
     return parent::renderView();
 }