From 12dd82a47c4eb86a830d5c53f7bb84204141372e Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Wed, 24 Jun 2015 23:58:38 +0200 Subject: [PATCH] Provide initialize command to create PO from POT. --- src/Shell/I18nShell.php | 76 ++++++++++++++++++++++- tests/TestCase/Shell/I18nShellTest.php | 84 ++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 tests/TestCase/Shell/I18nShellTest.php diff --git a/src/Shell/I18nShell.php b/src/Shell/I18nShell.php index 5593db992c4..174ed003d41 100644 --- a/src/Shell/I18nShell.php +++ b/src/Shell/I18nShell.php @@ -42,14 +42,18 @@ public function main() $this->out('I18n Shell'); $this->hr(); $this->out('[E]xtract POT file from sources'); + $this->out('[I]inialize a language from POT file'); $this->out('[H]elp'); $this->out('[Q]uit'); - $choice = strtolower($this->in('What would you like to do?', ['E', 'H', 'Q'])); + $choice = strtolower($this->in('What would you like to do?', ['E', 'I', 'H', 'Q'])); switch ($choice) { case 'e': $this->Extract->main(); break; + case 'i': + $this->init(); + break; case 'h': $this->out($this->OptionParser->help()); break; @@ -63,6 +67,54 @@ public function main() $this->main(); } + /** + * Inits PO file from POT file. + * + * @return void + */ + public function init($language = null) { + if (!$language) { + $language = strtolower($this->in('What language? Please use the two-letter ISO code, e.g. `en`.')); + } + if (strlen($language) !== 2) { + return $this->error('Must be a two-letter ISO code'); + } + + $this->_paths = [APP]; + if (!empty($this->params['plugin'])) { + $plugin = Inflector::camelize($this->params['plugin']); + $this->_paths = [Plugin::classPath($plugin)]; + $this->params['plugin'] = $plugin; + } + + $response = $this->in('What folder?', null, rtrim($this->_paths[0], DS) . DS . 'Locale'); + $sourceFolder = rtrim($response, DS) . DS; + $targetFolder = $sourceFolder . $language . DS; + if (!is_dir($targetFolder)) { + mkdir($targetFolder, 0770, true); + } + + $count = 0; + $iterator = new \DirectoryIterator($sourceFolder); + foreach ($iterator as $fileinfo) { + if (!$fileinfo->isFile()) { + continue; + } + $filename = $fileinfo->getFilename(); + $newFilename = $fileinfo->getBasename('.pot'); + $newFilename = $newFilename . '.po'; + if (empty($this->params['force']) && is_file($targetFolder . $newFilename)) { + $this->err('File ' . $newFilename . ' exists, skipping. Use --force or -f to force overwriting'); + continue; + } + + copy($sourceFolder . $filename, $targetFolder . $newFilename); + $count++; + } + + $this->out('Generated ' . $count . ' PO files in ' . $targetFolder); + } + /** * Gets the option parser instance and configures it. * @@ -71,12 +123,34 @@ public function main() public function getOptionParser() { $parser = parent::getOptionParser(); + $initParser = [ + 'options' => [ + 'plugin' => [ + 'help' => 'Plugin name.', + 'short' => 'p' + ], + 'force' => [ + 'help' => 'Force overwriting.', + 'short' => 'f', + 'boolean' => true + ] + ], + 'arguments' => [ + 'language' => [ + 'help' => 'Two-letter language code.' + ] + ] + ]; $parser->description( 'I18n Shell generates .pot files(s) with translations.' )->addSubcommand('extract', [ 'help' => 'Extract the po translations from your application', 'parser' => $this->Extract->getOptionParser() + ]) + ->addSubcommand('init', [ + 'help' => 'Init PO language file from POT file', + 'parser' => $initParser ]); return $parser; diff --git a/tests/TestCase/Shell/I18nShellTest.php b/tests/TestCase/Shell/I18nShellTest.php new file mode 100644 index 00000000000..f107fbf72f9 --- /dev/null +++ b/tests/TestCase/Shell/I18nShellTest.php @@ -0,0 +1,84 @@ +io = $this->getMock('Cake\Console\ConsoleIo'); + $this->shell = new I18nShell($this->io); + } + + /** + * Teardown + * + * @return void + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * Tests that init() creates the PO files from POT files. + * + * @return void + */ + public function testInit() + { + $localeDir = TMP . 'Locale' . DS; + $deDir = $localeDir . 'de' . DS; + if (!is_dir($deDir)) { + mkdir($deDir, 0770, true); + } + file_put_contents($localeDir . 'default.pot', 'Testing POT file.'); + file_put_contents($localeDir . 'cake.pot', 'Testing POT file.'); + if (file_exists($deDir . 'default.po')) { + unlink($deDir . 'default.po'); + } + if (file_exists($deDir . 'default.po')) { + unlink($deDir . 'cake.po'); + } + + $this->shell->io()->expects($this->at(0)) + ->method('ask') + ->will($this->returnValue('de')); + $this->shell->io()->expects($this->at(1)) + ->method('ask') + ->will($this->returnValue($localeDir)); + + $this->shell->params['verbose'] = true; + $this->shell->init(); + + $this->assertTrue(file_exists($deDir . 'default.po')); + $this->assertTrue(file_exists($deDir . 'cake.po')); + } +}