Skip to content

Instantly share code, notes, and snippets.

@dbu
Created December 5, 2012 17:21
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 dbu/4217624 to your computer and use it in GitHub Desktop.
Save dbu/4217624 to your computer and use it in GitHub Desktop.
Xliff translation updated
<?php
namespace Liip\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
class SynchronizeTranslationsCommand extends ContainerAwareCommand
{
/**
* @var LoaderInterface
*/
private $loader;
private $path;
protected function configure()
{
$this
->setName('liip:trans:sync')
->setDescription('Synchronize translation files between languages')
;
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->loader = new XliffFileLoader;
$this->path = 'app/Resources/translations';
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$finder = Finder::create()->name('*.en.xliff')->in($this->path);
foreach($finder as $filename) {
$this->synchronize($filename->getRealpath(), $output);
}
}
protected function synchronize($filename, OutputInterface $output)
{
$matches = array();
preg_match('#^.*/(.*)\.en\.xliff$#', $filename, $matches);
$domain = $matches[1];
$filefr = preg_replace('#\.en\.xliff$#', '.fr.xliff', $filename);
$output->write("Updating $filename...");
$caten = $this->loader->load($filename, 'en', $domain);
if (file_exists($filefr)) {
$catfr = $this->loader->load($filefr, 'fr', $domain);
} else {
$catfr = new MessageCatalogue('fr');
}
$modified = false;
foreach ($caten->all($domain) as $key => $value) {
if (! $catfr->has($key, $domain)) {
$catfr->set($key, "TODO: $value", $domain);
$modified = true;
}
}
if ($modified) {
$dumper = new XliffFileDumper();
$dumper->dump($catfr, array('path' => $this->path));
// the dumper has a hardcoded xlf extension, rename the file to xliff
rename($this->path . '/' . $domain . '.fr.xlf', $this->path . '/' . $domain . '.fr.xliff');
$output->write(" - UPDATED - ");
}
$output->write("done", true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment