Skip to content

Commit 12dd82a

Browse files
author
Mark Scherer
committed
Provide initialize command to create PO from POT.
1 parent 1422d39 commit 12dd82a

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

src/Shell/I18nShell.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ public function main()
4242
$this->out('<info>I18n Shell</info>');
4343
$this->hr();
4444
$this->out('[E]xtract POT file from sources');
45+
$this->out('[I]inialize a language from POT file');
4546
$this->out('[H]elp');
4647
$this->out('[Q]uit');
4748

48-
$choice = strtolower($this->in('What would you like to do?', ['E', 'H', 'Q']));
49+
$choice = strtolower($this->in('What would you like to do?', ['E', 'I', 'H', 'Q']));
4950
switch ($choice) {
5051
case 'e':
5152
$this->Extract->main();
5253
break;
54+
case 'i':
55+
$this->init();
56+
break;
5357
case 'h':
5458
$this->out($this->OptionParser->help());
5559
break;
@@ -63,6 +67,54 @@ public function main()
6367
$this->main();
6468
}
6569

70+
/**
71+
* Inits PO file from POT file.
72+
*
73+
* @return void
74+
*/
75+
public function init($language = null) {
76+
if (!$language) {
77+
$language = strtolower($this->in('What language? Please use the two-letter ISO code, e.g. `en`.'));
78+
}
79+
if (strlen($language) !== 2) {
80+
return $this->error('Must be a two-letter ISO code');
81+
}
82+
83+
$this->_paths = [APP];
84+
if (!empty($this->params['plugin'])) {
85+
$plugin = Inflector::camelize($this->params['plugin']);
86+
$this->_paths = [Plugin::classPath($plugin)];
87+
$this->params['plugin'] = $plugin;
88+
}
89+
90+
$response = $this->in('What folder?', null, rtrim($this->_paths[0], DS) . DS . 'Locale');
91+
$sourceFolder = rtrim($response, DS) . DS;
92+
$targetFolder = $sourceFolder . $language . DS;
93+
if (!is_dir($targetFolder)) {
94+
mkdir($targetFolder, 0770, true);
95+
}
96+
97+
$count = 0;
98+
$iterator = new \DirectoryIterator($sourceFolder);
99+
foreach ($iterator as $fileinfo) {
100+
if (!$fileinfo->isFile()) {
101+
continue;
102+
}
103+
$filename = $fileinfo->getFilename();
104+
$newFilename = $fileinfo->getBasename('.pot');
105+
$newFilename = $newFilename . '.po';
106+
if (empty($this->params['force']) && is_file($targetFolder . $newFilename)) {
107+
$this->err('File ' . $newFilename . ' exists, skipping. Use --force or -f to force overwriting');
108+
continue;
109+
}
110+
111+
copy($sourceFolder . $filename, $targetFolder . $newFilename);
112+
$count++;
113+
}
114+
115+
$this->out('Generated ' . $count . ' PO files in ' . $targetFolder);
116+
}
117+
66118
/**
67119
* Gets the option parser instance and configures it.
68120
*
@@ -71,12 +123,34 @@ public function main()
71123
public function getOptionParser()
72124
{
73125
$parser = parent::getOptionParser();
126+
$initParser = [
127+
'options' => [
128+
'plugin' => [
129+
'help' => 'Plugin name.',
130+
'short' => 'p'
131+
],
132+
'force' => [
133+
'help' => 'Force overwriting.',
134+
'short' => 'f',
135+
'boolean' => true
136+
]
137+
],
138+
'arguments' => [
139+
'language' => [
140+
'help' => 'Two-letter language code.'
141+
]
142+
]
143+
];
74144

75145
$parser->description(
76146
'I18n Shell generates .pot files(s) with translations.'
77147
)->addSubcommand('extract', [
78148
'help' => 'Extract the po translations from your application',
79149
'parser' => $this->Extract->getOptionParser()
150+
])
151+
->addSubcommand('init', [
152+
'help' => 'Init PO language file from POT file',
153+
'parser' => $initParser
80154
]);
81155

82156
return $parser;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 3.0.8
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Test\TestCase\Shell;
16+
17+
use Cake\Cache\Cache;
18+
use Cake\Datasource\ConnectionManager;
19+
use Cake\Shell\I18nShell;
20+
use Cake\TestSuite\TestCase;
21+
22+
/**
23+
* I18nShell test.
24+
*/
25+
class I18nShellTest extends TestCase
26+
{
27+
28+
/**
29+
* setup method
30+
*
31+
* @return void
32+
*/
33+
public function setUp()
34+
{
35+
parent::setUp();
36+
$this->io = $this->getMock('Cake\Console\ConsoleIo');
37+
$this->shell = new I18nShell($this->io);
38+
}
39+
40+
/**
41+
* Teardown
42+
*
43+
* @return void
44+
*/
45+
public function tearDown()
46+
{
47+
parent::tearDown();
48+
}
49+
50+
/**
51+
* Tests that init() creates the PO files from POT files.
52+
*
53+
* @return void
54+
*/
55+
public function testInit()
56+
{
57+
$localeDir = TMP . 'Locale' . DS;
58+
$deDir = $localeDir . 'de' . DS;
59+
if (!is_dir($deDir)) {
60+
mkdir($deDir, 0770, true);
61+
}
62+
file_put_contents($localeDir . 'default.pot', 'Testing POT file.');
63+
file_put_contents($localeDir . 'cake.pot', 'Testing POT file.');
64+
if (file_exists($deDir . 'default.po')) {
65+
unlink($deDir . 'default.po');
66+
}
67+
if (file_exists($deDir . 'default.po')) {
68+
unlink($deDir . 'cake.po');
69+
}
70+
71+
$this->shell->io()->expects($this->at(0))
72+
->method('ask')
73+
->will($this->returnValue('de'));
74+
$this->shell->io()->expects($this->at(1))
75+
->method('ask')
76+
->will($this->returnValue($localeDir));
77+
78+
$this->shell->params['verbose'] = true;
79+
$this->shell->init();
80+
81+
$this->assertTrue(file_exists($deDir . 'default.po'));
82+
$this->assertTrue(file_exists($deDir . 'cake.po'));
83+
}
84+
}

0 commit comments

Comments
 (0)