1: <?php
2:
3: App::uses('Translator', 'Base.Lib');
4: App::uses('FileSystem', 'Base.Lib');
5:
6: class TranslatorShell extends Shell {
7:
8: 9: 10: 11: 12:
13: public function getOptionParser() {
14: $parser = parent::getOptionParser();
15: $parser->addSubcommand('i18n_extract');
16: $parser->addSubcommand('terms');
17: $parser->addSubcommand('terms_file');
18: return $parser;
19: }
20:
21: public function () {
22: $directoryWithTerms = $this->_directoryWithTermsToTranslate();
23: $plugins = $this->_pluginPaths();
24: $shell = 'i18n extract --app ' . APP .
25: ' --paths ' . APP . ',' . $directoryWithTerms . ',' . implode(',', $plugins) .
26: ' --extract-core no' .
27: ' --merge no' .
28: ' --output ' . APP . 'Locale' .
29: ' --overwrite yes';
30: $this->dispatchShell($shell);
31: FileSystem::recursiveRemoveDirectory($directoryWithTerms);
32: }
33:
34: public function terms() {
35: foreach (Translator::termsToTranslate() as $plugin => $terms) {
36: foreach ($terms as $term) {
37: $this->out("<info>$plugin</info>|$term|");
38: }
39: }
40: }
41:
42: public function terms_file() {
43: echo $this->out($this->_termsToTranslateFileContent());
44: }
45:
46: private function _pluginPaths() {
47: $paths = array();
48: foreach (CakePlugin::loaded() as $plugin) {
49: switch ($plugin) {
50: case 'Migrations':
51: break;
52:
53: default:
54: $paths[] = CakePlugin::path($plugin);
55: }
56: }
57: return $paths;
58: }
59:
60: private function _directoryWithTermsToTranslate() {
61: $tempDir = FileSystem::createTemporaryDirectory();
62: $tempFile = tempnam($tempDir, 'to_translate') . '.php';
63: file_put_contents($tempFile, $this->_termsToTranslateFileContent());
64: return $tempDir;
65: }
66:
67: private function _termsToTranslateFileContent() {
68: $b = "<?php\n";
69: foreach (Translator::termsToTranslate() as $plugin => $terms) {
70: foreach ($terms as $term) {
71: if ($plugin == '') {
72: $b .= '__("' . addslashes($term) . '");' . PHP_EOL;
73: } else {
74: $b .= '__d("' . $plugin . '","' . addslashes($term) . '");' . PHP_EOL;
75: }
76: }
77: }
78: return $b;
79: }
80:
81: }
82: