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