Created
April 30, 2018 13:19
-
-
Save lenybernard/d9d11ae3dd30652178f6e633077e7700 to your computer and use it in GitHub Desktop.
Will parse every xliff files and will add missing id attributes to be compliant with strict Symfony xliff parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Command; | |
use AppBundle\Entity\Event\Event; | |
use AppBundle\Entity\Event\Topic; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Finder\Finder; | |
use Taiga\Api; | |
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