Skip to content

Instantly share code, notes, and snippets.

@lenybernard
Created January 26, 2017 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenybernard/0ea62e8d8eb30d83e5d6afb9312a79cc to your computer and use it in GitHub Desktop.
Save lenybernard/0ea62e8d8eb30d83e5d6afb9312a79cc to your computer and use it in GitHub Desktop.
Symfony console command to parse xliff files to add or update missing id attribute
<?php
namespace AppBundle\Command;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/**
* @author leny@troopers.email
*/
class XliffFixerCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
public function configure()
{
parent::configure();
$this
->setName('xliff:fixer')
->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Where the script will search for xliff files', __DIR__.'/..')
->setDescription('Regenerate ids for each trans-unit');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$finder = new Finder();
$finder->files()->in($input->getOption('path'))->name('*\.xliff');
foreach ($finder as $file) {
$output->write(sprintf('<comment>%s</comment>', $file->getFilename()));
$xml = simplexml_load_file($file->getRealPath());
foreach ($xml->file->body->{'trans-unit'} as $item) {
/** @var \SimpleXMLElement $item */
$item->attributes()->{'id'} = uniqid();
if ((string) $item->attributes()->{'id'} === '') {
$item->addAttribute('id', uniqid());
}
}
$xml->saveXML($file->getRealPath());
$output->writeln(sprintf('<info> OK</info>', $file->getFilename()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment