Skip to content
Open
69 changes: 48 additions & 21 deletions Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Dumper;

/**
* Command for exporting translations into files
*/

class ExportCommand extends Base
{


protected function configure()
{
parent::configure();
Expand All @@ -24,7 +22,8 @@ protected function configure()
->setName('locale:editor:export')
->setDescription('Export translations into files')
->addArgument('filename')
->addOption("dry-run")
->addOption('dry-run')
->addOption('pretty-print')
;

}
Expand Down Expand Up @@ -71,7 +70,7 @@ public function execute(InputInterface $input, OutputInterface $output)
}
$output->writeln(sprintf("Found %d files, exporting...", count($files)));

foreach($files as $filename) {
foreach ($files as $filename) {
$this->export($filename);
}

Expand All @@ -84,15 +83,15 @@ public function export($filename)

list($name, $locale, $type) = explode('.', $fname);

$data = $this->getContainer()->get('server_grove_translation_editor.storage_manager')->getCollection()->findOne(array('locale'=>$locale));
if (!$data) {
$this->output->writeln("Could not find data for this locale");
return;
}

switch($type) {
case 'yml':
$data = $this->getContainer()->get('server_grove_translation_editor.storage_manager')->getCollection()->findOne(array('filename'=>$filename));
if (!$data) {
$this->output->writeln("Could not find data for this locale");
return;
}

foreach($data['entries'] as $key => $val) {
foreach ($data['entries'] as $key => $val) {
if (empty($val)) {
unset($data['entries'][$key]);
}
Expand All @@ -101,19 +100,47 @@ public function export($filename)
$dumper = new Dumper();
$result = $dumper->dump($data['entries'], 1);

$this->output->writeln(" Writing ".count($data['entries'])." entries to $filename");
if (!$this->input->getOption('dry-run')) {
file_put_contents($filename, $result);
}

break;
case 'xliff':
$this->output->writeln(" Skipping, not implemented");
$xml = new \SimpleXMLElement('<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"></xliff>');

$xliff_file = $xml->addChild("file");
$xliff_file->addAttribute("source-language", $locale);
$xliff_file->addAttribute("datatype", "plaintext");
$xliff_file->addAttribute("original", "file.ext");

$body = $xliff_file->addChild('body');

$i = 0;
foreach ($data['entries'] as $source => $target) {
if (empty($target)) {
continue;
}

$unit = $body->addChild('trans-unit');
$unit->addAttribute("id", ++$i);
$unit->source = $source;
$unit->target = $target;
}

$result = $xml->asXML();

if ($this->input->getOption('pretty-print')) {
$dom = new \DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($result);

$result = $dom->saveXML();
}

break;
}
}


$this->output->writeln(" Writing ".count($data['entries'])." entries to $filename");
if (!$this->input->getOption('dry-run')) {
file_put_contents($filename, $result);
}
}
}


45 changes: 32 additions & 13 deletions Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Translation\Loader\XliffFileLoader;

/**
* Command for importing translation files
Expand Down Expand Up @@ -86,22 +87,22 @@ public function import($filename)

$this->setIndexes();

$data = $this->getContainer()->get('server_grove_translation_editor.storage_manager')->getCollection()->findOne(array('filename'=>$filename));
if (!$data) {
$data = array(
'filename' => $filename,
'locale' => $locale,
'type' => $type,
'entries' => array(),
);

}

switch($type) {
case 'yml':
$yaml = new Parser();
$value = $yaml->parse(file_get_contents($filename));

$data = $this->getContainer()->get('server_grove_translation_editor.storage_manager')->getCollection()->findOne(array('filename'=>$filename));
if (!$data) {
$data = array(
'filename' => $filename,
'locale' => $locale,
'type' => $type,
'entries' => array(),
);

}

$this->output->writeln(" Found ".count($value)." entries...");
$data['entries'] = $value;

Expand All @@ -110,8 +111,26 @@ public function import($filename)
}
break;
case 'xliff':
$this->output->writeln(" Skipping, not implemented");
break;
$loader = new XliffFileLoader();
$xliff = $loader->load($filename, $locale, $name);

$this->output->writeln(" Found ".count($xliff->getDomains())." domains...");

$entries = $xliff->all();

foreach ($xliff->getDomains() as $domain) {
$this->output->writeln(" Processing '$domain' domain");
$value = $entries[$domain];

$this->output->writeln(" Found ".count($value)." entries...");
$data['entries'] = $value;

if (!$this->input->getOption('dry-run')) {
$this->updateValue($data);
}
}

break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions MongoStorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(ContainerInterface $container)
function getMongo()
{
if (!$this->mongo) {
$this->mongo = new \Mongo($this->container->getParameter('translation_editor.mongodb'));
$this->mongo = new \MongoClient($this->container->getParameter('translation_editor.mongodb'));
}

if (!$this->mongo) {
Expand All @@ -36,4 +36,4 @@ public function getCollection()
{
return $this->getDB()->selectCollection($this->container->getParameter('translation_editor.collection'));
}
}
}
8 changes: 4 additions & 4 deletions Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
sg_localeditor_list:
pattern: /translations/editor
path: /translations/editor
defaults: { _controller: ServerGroveTranslationEditorBundle:Editor:list }

sg_localeditor_update:
pattern: /translations/update
path: /translations/update
defaults: { _controller: ServerGroveTranslationEditorBundle:Editor:update }

sg_localeditor_add:
pattern: /translations/add
path: /translations/add
defaults: { _controller: ServerGroveTranslationEditorBundle:Editor:add }

sg_localeditor_remove:
pattern: /translations/remove
path: /translations/remove
defaults: { _controller: ServerGroveTranslationEditorBundle:Editor:remove }