Example #1
0
 public function testLoad()
 {
     $loader = new PhpFileLoader();
     $resource = __DIR__ . '/../fixtures/resources.php';
     $catalogue = $loader->load($resource, 'en', 'domain1');
     $this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
     $this->assertEquals('en', $catalogue->getLocale());
     $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
 }
Example #2
0
 /**
  * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  */
 public function testLoadThrowsAnExceptionIfFileNotLocal()
 {
     $loader = new PhpFileLoader();
     $resource = 'http://example.com/resources.php';
     $loader->load($resource, 'en', 'domain1');
 }
 /**
  *
  * {@inheritDoc} @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 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 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 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 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 \Sasedev\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(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 \Sasedev\ExtraToolsBundle\Translation\Formatter\YmlFormatter();
             } elseif ($input->getOption('output-format') == 'php') {
                 $formatter = new \Sasedev\ExtraToolsBundle\Translation\Formatter\PhpFormatter();
             } elseif ($input->getOption('output-format') == 'pot') {
                 $formatter = new \Sasedev\ExtraToolsBundle\Translation\Formatter\PotFormatter();
             } else {
                 $formatter = new \Sasedev\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)));
             }
         }
     }
 }