PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Shell/I18nShell.php

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