Skip to content

Instantly share code, notes, and snippets.

@dbu
Created June 28, 2013 07:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbu/5883029 to your computer and use it in GitHub Desktop.
Save dbu/5883029 to your computer and use it in GitHub Desktop.
Synchronize Translations
<?php
namespace Dbu\UtilBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/**
* @author David Buchmann <mail@davidbu.ch>
*/
class SynchronizeTranslationsCommand extends ContainerAwareCommand
{
/**
* @var LoaderInterface
*/
private $loader;
private $path;
protected function configure()
{
$this
->setName('trans:sync')
->setDescription('Synchronize translation files between languages.
It takes the translations of one language and creates keys
prefixed with TODO in other languages.
Currently it is hardcoded against app/Resources/translations
and expecting english xliff translations as being complete,
adding to french xliff translations.')
;
}
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
// TODO: probably we should use the symfony Filesystem component instead for renaming
rename($this->path . '/' . $domain . '.fr.xlf', $this->path . '/' . $domain . '.fr.xliff');
$output->write(" - UPDATED - ");
}
$output->write("done", true);
}
}
@dbu
Copy link
Author

dbu commented Jun 28, 2013

one annoying drawback with the xliff dumper is that it loses the original translation id and generates numeric ids. if you care about that, there seems to be no nice solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment