diff --git a/Command/ExportCommand.php b/Command/ExportCommand.php index c956e03..52a9f13 100644 --- a/Command/ExportCommand.php +++ b/Command/ExportCommand.php @@ -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(); @@ -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') ; } @@ -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); } @@ -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]); } @@ -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_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); + } + } } - diff --git a/Command/ImportCommand.php b/Command/ImportCommand.php index b0c4a65..e0eb95f 100644 --- a/Command/ImportCommand.php +++ b/Command/ImportCommand.php @@ -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 @@ -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; @@ -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; } } diff --git a/MongoStorageManager.php b/MongoStorageManager.php index 76d5d5b..63ac6a6 100644 --- a/MongoStorageManager.php +++ b/MongoStorageManager.php @@ -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) { @@ -36,4 +36,4 @@ public function getCollection() { return $this->getDB()->selectCollection($this->container->getParameter('translation_editor.collection')); } -} \ No newline at end of file +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 788971b..b3144bb 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -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 }