/** * @param string $file The path to the xliff file */ protected function validateXliff($file) { try { $this->loader->load($file, 'en'); $this->assertTrue(true, sprintf("Successful loading file: %s", $file)); } catch (InvalidResourceException $e) { $this->errors[] = sprintf("%s => %s", $file, $e->getMessage()); } }
/** * Load translations and metadata of the trans-unit. * * {@inheritdoc} * * @api */ public function load($resource, $locale, $domain = 'messages') { /* @var MessageCatalogue $catalogue */ $base_catalogue = parent::load($resource, $locale, $domain); $catalogue = new MessageCatalogue($locale); $catalogue->addCatalogue($base_catalogue); // Process a second pass over the file to collect metadata $xml = simplexml_load_file($resource); $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); foreach ($xml->xpath('//xliff:trans-unit') as $translation) { // Read the attributes $attributes = (array) $translation->attributes(); $attributes = $attributes['@attributes']; if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) { continue; } $key = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; $metadata = (array) $attributes; // read the notes if (isset($translation->note)) { $metadata['note'] = (string) $translation->note; } $catalogue->setMetadata((string) $key, $metadata, $domain); } return $catalogue; }
public function getCatalogueFromPaths($paths, $locale, $pattern = null) { $messageCatalogue = new MessageCatalogue($locale); $xliffFileLoader = new XliffFileLoader(); $finder = new Finder(); if (null !== $pattern) { $finder->name($pattern); } $translationFiles = $finder->files()->in($paths); if (count($translationFiles) === 0) { throw new \Exception('There is no translation file available.'); } foreach ($translationFiles as $file) { $fileCatalogue = $xliffFileLoader->load($file->getPathname(), $locale, $file->getBasename('.xlf')); $messageCatalogue->addCatalogue($fileCatalogue); } return $messageCatalogue; }
public function testLoadNotes() { $loader = new XliffFileLoader(); $catalogue = $loader->load(__DIR__ . '/../fixtures/withnote.xlf', 'en', 'domain1'); $this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo'))), $catalogue->getMetadata('foo', 'domain1')); // message without target $this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo'))), $catalogue->getMetadata('extra', 'domain1')); // message with empty target $this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux'))), $catalogue->getMetadata('key', 'domain1')); }
public function testLoadVersion2() { $loader = new XliffFileLoader(); $resource = __DIR__ . '/../fixtures/resources-2.0.xlf'; $catalogue = $loader->load($resource, 'en', 'domain1'); $this->assertEquals('en', $catalogue->getLocale()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertSame(array(), libxml_get_errors()); $domains = $catalogue->all(); $this->assertCount(3, $domains['domain1']); $this->assertContainsOnly('string', $catalogue->all('domain1')); // target attributes $this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1')); }
/** * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException * @expectedExceptionMessage Document types are not allowed. */ public function testDocTypeIsNotAllowed() { $loader = new XliffFileLoader(); $loader->load(__DIR__ . '/../fixtures/withdoctype.xlf', 'en', 'domain1'); }
public function testParseEmptyFile() { $loader = new XliffFileLoader(); $resource = __DIR__ . '/../fixtures/empty.xlf'; $this->setExpectedException('Symfony\\Component\\Translation\\Exception\\InvalidResourceException', sprintf('Unable to load "%s":', $resource)); $loader->load($resource, 'en', 'domain1'); }
/** * @expectedException \InvalidArgumentException */ public function testLoadThrowsAnExceptionIfFileNotLocal() { $loader = new XliffFileLoader(); $resource = 'http://example.com/resources.xlf'; $loader->load($resource, 'en', 'domain1'); }
/** * @expectedException Exception */ public function testLoadResourceDoesNotValidate() { $loader = new XliffFileLoader(); $catalogue = $loader->load(__DIR__ . '/../fixtures/non-valid.xliff', '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))); } } } }