1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell;
16:
17: use Cake\Console\Shell;
18: use Cake\Core\App;
19: use Cake\Core\Plugin;
20: use Cake\Utility\Inflector;
21: use DirectoryIterator;
22:
23: 24: 25: 26: 27:
28: class I18nShell extends Shell
29: {
30:
31: 32: 33: 34: 35:
36: public $tasks = ['Extract'];
37:
38: 39: 40:
41: protected $_paths;
42:
43: 44: 45: 46: 47: 48: 49: 50:
51: public function main()
52: {
53: $this->out('<info>I18n Shell</info>');
54: $this->hr();
55: $this->out('[E]xtract POT file from sources');
56: $this->out('[I]nitialize a language from POT file');
57: $this->out('[H]elp');
58: $this->out('[Q]uit');
59:
60: $choice = strtolower($this->in('What would you like to do?', ['E', 'I', 'H', 'Q']));
61: switch ($choice) {
62: case 'e':
63: $this->Extract->main();
64: break;
65: case 'i':
66: $this->init();
67: break;
68: case 'h':
69: $this->out($this->OptionParser->help());
70: break;
71: case 'q':
72: $this->_stop();
73:
74: return;
75: default:
76: $this->out('You have made an invalid selection. Please choose a command to execute by entering E, I, H, or Q.');
77: }
78: $this->hr();
79: $this->main();
80: }
81:
82: 83: 84: 85: 86: 87: 88:
89: public function init($language = null)
90: {
91: if (!$language) {
92: $language = $this->in('Please specify language code, e.g. `en`, `eng`, `en_US` etc.');
93: }
94: if (strlen($language) < 2) {
95: $this->abort('Invalid language code. Valid is `en`, `eng`, `en_US` etc.');
96: }
97:
98: $this->_paths = App::path('Locale');
99: if ($this->param('plugin')) {
100: $plugin = Inflector::camelize($this->param('plugin'));
101: $this->_paths = App::path('Locale', $plugin);
102: }
103:
104: $response = $this->in('What folder?', null, rtrim($this->_paths[0], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
105: $sourceFolder = rtrim($response, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
106: $targetFolder = $sourceFolder . $language . DIRECTORY_SEPARATOR;
107: if (!is_dir($targetFolder)) {
108: mkdir($targetFolder, 0775, true);
109: }
110:
111: $count = 0;
112: $iterator = new DirectoryIterator($sourceFolder);
113: foreach ($iterator as $fileinfo) {
114: if (!$fileinfo->isFile()) {
115: continue;
116: }
117: $filename = $fileinfo->getFilename();
118: $newFilename = $fileinfo->getBasename('.pot');
119: $newFilename .= '.po';
120:
121: $this->createFile($targetFolder . $newFilename, file_get_contents($sourceFolder . $filename));
122: $count++;
123: }
124:
125: $this->out('Generated ' . $count . ' PO files in ' . $targetFolder);
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function getOptionParser()
135: {
136: $parser = parent::getOptionParser();
137: $initParser = [
138: 'options' => [
139: 'plugin' => [
140: 'help' => 'Plugin name.',
141: 'short' => 'p'
142: ],
143: 'force' => [
144: 'help' => 'Force overwriting.',
145: 'short' => 'f',
146: 'boolean' => true
147: ]
148: ],
149: 'arguments' => [
150: 'language' => [
151: 'help' => 'Two-letter language code.'
152: ]
153: ]
154: ];
155:
156: $parser->setDescription(
157: 'I18n Shell generates .pot files(s) with translations.'
158: )->addSubcommand('extract', [
159: 'help' => 'Extract the po translations from your application',
160: 'parser' => $this->Extract->getOptionParser()
161: ])
162: ->addSubcommand('init', [
163: 'help' => 'Init PO language file from POT file',
164: 'parser' => $initParser
165: ]);
166:
167: return $parser;
168: }
169: }
170: