sfI18nExtractTask.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * Current known limitations:
  4. * - Can only works with the default "messages" catalogue
  5. * - For file backends (XLIFF and gettext), it only saves/deletes strings in the "most global" file
  6. */
  7. /*
  8. * This file is part of the symfony package.
  9. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. /**
  15. * Extracts i18n strings from php files.
  16. *
  17. * @package symfony
  18. * @subpackage task
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. * @version SVN: $Id: sfI18nExtractTask.class.php 9883 2008-06-26 09:04:13Z FabianLange $
  21. */
  22. class sfI18nExtractTask extends sfBaseTask
  23. {
  24. /**
  25. * @see sfTask
  26. */
  27. protected function configure()
  28. {
  29. $this->addArguments(array(
  30. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  31. new sfCommandArgument('culture', sfCommandArgument::REQUIRED, 'The target culture'),
  32. ));
  33. $this->addOptions(array(
  34. new sfCommandOption('display-new', null, sfCommandOption::PARAMETER_NONE, 'Output all new found strings'),
  35. new sfCommandOption('display-old', null, sfCommandOption::PARAMETER_NONE, 'Output all old strings'),
  36. new sfCommandOption('auto-save', null, sfCommandOption::PARAMETER_NONE, 'Save the new strings'),
  37. new sfCommandOption('auto-delete', null, sfCommandOption::PARAMETER_NONE, 'Delete old strings'),
  38. ));
  39. $this->namespace = 'i18n';
  40. $this->name = 'extract';
  41. $this->briefDescription = 'Extracts i18n strings from php files';
  42. $this->detailedDescription = <<<EOF
  43. The [i18n:extract|INFO] task extracts i18n strings from your project files
  44. for the given application and target culture:
  45. [./symfony i18n:extract frontend fr|INFO]
  46. By default, the task only displays the number of new and old strings
  47. it found in the current project.
  48. If you want to display the new strings, use the [--display-new|COMMENT] option:
  49. [./symfony i18n:extract --display-new frontend fr|INFO]
  50. To save them in the i18n message catalogue, use the [--auto-save|COMMENT] option:
  51. [./symfony i18n:extract --auto-save frontend fr|INFO]
  52. If you want to display strings that are present in the i18n messages
  53. catalogue but are not found in the application, use the
  54. [--display-old|COMMENT] option:
  55. [./symfony i18n:extract --display-old frontend fr|INFO]
  56. To automatically delete old strings, use the [--auto-delete|COMMENT] but
  57. be careful, especially if you have translations for plugins as they will
  58. appear as old strings but they are not:
  59. [./symfony i18n:extract --auto-delete frontend fr|INFO]
  60. EOF;
  61. }
  62. /**
  63. * @see sfTask
  64. */
  65. public function execute($arguments = array(), $options = array())
  66. {
  67. $this->logSection('i18n', sprintf('extracting i18n strings for the "%s" application', $arguments['application']));
  68. // get i18n configuration from factories.yml
  69. $config = sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
  70. $class = $config['i18n']['class'];
  71. $params = $config['i18n']['param'];
  72. unset($params['cache']);
  73. $extract = new sfI18nApplicationExtract(new $class($this->configuration, new sfNoCache(), $params), $arguments['culture']);
  74. $extract->extract();
  75. $this->logSection('i18n', sprintf('found "%d" new i18n strings', count($extract->getNewMessages())));
  76. $this->logSection('i18n', sprintf('found "%d" old i18n strings', count($extract->getOldMessages())));
  77. if ($options['display-new'])
  78. {
  79. $this->logSection('i18n', sprintf('display new i18n strings', count($extract->getOldMessages())));
  80. foreach ($extract->getNewMessages() as $message)
  81. {
  82. $this->log(' '.$message."\n");
  83. }
  84. }
  85. if ($options['auto-save'])
  86. {
  87. $this->logSection('i18n', 'saving new i18n strings');
  88. $extract->saveNewMessages();
  89. }
  90. if ($options['display-old'])
  91. {
  92. $this->logSection('i18n', sprintf('display old i18n strings', count($extract->getOldMessages())));
  93. foreach ($extract->getOldMessages() as $message)
  94. {
  95. $this->log(' '.$message."\n");
  96. }
  97. }
  98. if ($options['auto-delete'])
  99. {
  100. $this->logSection('i18n', 'deleting old i18n strings');
  101. $extract->deleteOldMessages();
  102. }
  103. }
  104. }