I18nShell.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell;
  16. use Cake\Console\Shell;
  17. use Cake\Core\Plugin;
  18. use Cake\Utility\Inflector;
  19. use DirectoryIterator;
  20. /**
  21. * Shell for I18N management.
  22. *
  23. * @property \Cake\Shell\Task\ExtractTask $Extract
  24. */
  25. class I18nShell extends Shell
  26. {
  27. /**
  28. * Contains tasks to load and instantiate
  29. *
  30. * @var array
  31. */
  32. public $tasks = ['Extract'];
  33. /**
  34. * @var string[]
  35. */
  36. protected $_paths;
  37. /**
  38. * Override main() for help message hook
  39. *
  40. * @return void
  41. */
  42. public function main()
  43. {
  44. $this->out('<info>I18n Shell</info>');
  45. $this->hr();
  46. $this->out('[E]xtract POT file from sources');
  47. $this->out('[I]nitialize a language from POT file');
  48. $this->out('[H]elp');
  49. $this->out('[Q]uit');
  50. $choice = strtolower($this->in('What would you like to do?', ['E', 'I', 'H', 'Q']));
  51. switch ($choice) {
  52. case 'e':
  53. $this->Extract->main();
  54. break;
  55. case 'i':
  56. $this->init();
  57. break;
  58. case 'h':
  59. $this->out($this->OptionParser->help());
  60. break;
  61. case 'q':
  62. $this->_stop();
  63. return;
  64. default:
  65. $this->out('You have made an invalid selection. Please choose a command to execute by entering E, I, H, or Q.');
  66. }
  67. $this->hr();
  68. $this->main();
  69. }
  70. /**
  71. * Inits PO file from POT file.
  72. *
  73. * @param string|null $language Language code to use.
  74. * @return void
  75. */
  76. public function init($language = null)
  77. {
  78. if (!$language) {
  79. $language = $this->in('Please specify language code, e.g. `en`, `eng`, `en_US` etc.');
  80. }
  81. if (strlen($language) < 2) {
  82. $this->abort('Invalid language code. Valid is `en`, `eng`, `en_US` etc.');
  83. }
  84. $this->_paths = [APP];
  85. if ($this->param('plugin')) {
  86. $plugin = Inflector::camelize($this->param('plugin'));
  87. $this->_paths = [Plugin::classPath($plugin)];
  88. }
  89. $response = $this->in('What folder?', null, rtrim($this->_paths[0], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'Locale');
  90. $sourceFolder = rtrim($response, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  91. $targetFolder = $sourceFolder . $language . DIRECTORY_SEPARATOR;
  92. if (!is_dir($targetFolder)) {
  93. mkdir($targetFolder, 0775, true);
  94. }
  95. $count = 0;
  96. $iterator = new DirectoryIterator($sourceFolder);
  97. foreach ($iterator as $fileinfo) {
  98. if (!$fileinfo->isFile()) {
  99. continue;
  100. }
  101. $filename = $fileinfo->getFilename();
  102. $newFilename = $fileinfo->getBasename('.pot');
  103. $newFilename = $newFilename . '.po';
  104. $this->createFile($targetFolder . $newFilename, file_get_contents($sourceFolder . $filename));
  105. $count++;
  106. }
  107. $this->out('Generated ' . $count . ' PO files in ' . $targetFolder);
  108. }
  109. /**
  110. * Gets the option parser instance and configures it.
  111. *
  112. * @return \Cake\Console\ConsoleOptionParser
  113. */
  114. public function getOptionParser()
  115. {
  116. $parser = parent::getOptionParser();
  117. $initParser = [
  118. 'options' => [
  119. 'plugin' => [
  120. 'help' => 'Plugin name.',
  121. 'short' => 'p'
  122. ],
  123. 'force' => [
  124. 'help' => 'Force overwriting.',
  125. 'short' => 'f',
  126. 'boolean' => true
  127. ]
  128. ],
  129. 'arguments' => [
  130. 'language' => [
  131. 'help' => 'Two-letter language code.'
  132. ]
  133. ]
  134. ];
  135. $parser->setDescription(
  136. 'I18n Shell generates .pot files(s) with translations.'
  137. )->addSubcommand('extract', [
  138. 'help' => 'Extract the po translations from your application',
  139. 'parser' => $this->Extract->getOptionParser()
  140. ])
  141. ->addSubcommand('init', [
  142. 'help' => 'Init PO language file from POT file',
  143. 'parser' => $initParser
  144. ]);
  145. return $parser;
  146. }
  147. }