Author: gbastien
Date: 2010-04-18 18:39:12 +0200 (Sun, 18 Apr 2010)
New Revision: 29191

Added:
   plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginExtract.class.php
   plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginModuleExtract.class.php
   
plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nPluginAllExtractTask.class.php
Modified:
   plugins/sfI18nFormExtractorPlugin/lib/sfI18nFormExtract.class.php
   plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nFormExtractTask.class.php
Log:
New features:  
- Added a new plugin extraction task that saves the internationalized strings 
into the plugin's own catalogue
- The forms strings are now extracted recursively

Modified: plugins/sfI18nFormExtractorPlugin/lib/sfI18nFormExtract.class.php
===================================================================
--- plugins/sfI18nFormExtractorPlugin/lib/sfI18nFormExtract.class.php   
2010-04-18 07:30:04 UTC (rev 29190)
+++ plugins/sfI18nFormExtractorPlugin/lib/sfI18nFormExtract.class.php   
2010-04-18 16:39:12 UTC (rev 29191)
@@ -19,7 +19,11 @@
     }
     if (class_exists($form))
     {
-      $this->form = new $form();
+      $class = new ReflectionClass($form);
+      if (!$class->isAbstract())
+        $this->form = new $form();
+      else
+        $this->form = null;
     }
   }
 

Added: plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginExtract.class.php
===================================================================
--- plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginExtract.class.php         
                (rev 0)
+++ plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginExtract.class.php 
2010-04-18 16:39:12 UTC (rev 29191)
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * @author     Geneviève Bastien <[email protected]>
+ */
+class sfI18nPluginExtract extends sfI18nExtract
+{
+  protected $plugin = '';
+  
+  public function setPlugin($plugin) {
+    $this->plugin = $plugin;
+    
$this->i18n->setMessageSource(array_merge(array(sfConfig::get('sf_plugins_dir').'/'.$this->plugin.'/i18n'),$this->i18n->getConfiguration()->getI18NDirs($this->plugin)),
 $this->culture);
+  }
+  
+  protected $extractObjects = array();
+
+  /**
+   * Configures the current extract object.
+   */
+  public function configure()
+  {
+    if (!isset($this->parameters['plugin']))
+    {
+      throw new sfException('You must give a "plugin" parameter when 
extracting for a plugin.');
+    }
+
+    $this->setPlugin($this->parameters['plugin']);
+
+    $this->extractObjects = array();
+
+    $plugindir = sfConfig::get('sf_plugins_dir') . '/' . $this->plugin;
+    // Modules
+    $moduleNames = 
sfFinder::type('dir')->maxdepth(0)->relative()->in($plugindir . '/modules');
+    foreach ($moduleNames as $moduleName)
+    {
+      $this->extractObjects[] = new sfI18nPluginModuleExtract($this->i18n, 
$this->culture, array('module' => $moduleName, 'plugin' => $this->plugin));
+    }
+    
+    $forms = 
sfFinder::type('file')->name('*.php')->discard(array('BaseFormPropel.class.php'))->in($plugindir
 . '/lib/form');
+    foreach ($forms as $form)
+    {
+      $form = basename($form);
+      $extractor = new sfI18nFormExtract($this->i18n, $this->culture);
+      $extractor->setForm($form);
+      $this->extractObjects[] = $extractor;
+    }
+  }
+
+  /**
+   * Extracts i18n strings.
+   *
+   * This class must be implemented by subclasses.
+   */
+  public function extract()
+  {
+    foreach ($this->extractObjects as $extractObject)
+    {
+      $extractObject->extract();
+    }
+
+    // Add global templates
+    $this->extractFromPhpFiles(sfConfig::get('sf_plugins_dir') . '/'. 
$this->plugin . '/lib/model');
+  }
+  
+  /**
+   * Gets the current i18n strings.
+   */
+  public function getCurrentMessages()
+  {
+    return array_unique(array_merge($this->currentMessages, 
$this->aggregateMessages('getCurrentMessages')));
+  }
+
+  /**
+   * Gets all i18n strings seen during the extraction process.
+   */
+  public function getAllSeenMessages()
+  {
+    return array_unique(array_merge($this->allSeenMessages, 
$this->aggregateMessages('getAllSeenMessages')));
+  }
+
+  protected function aggregateMessages($method)
+  {
+    $messages = array();
+    foreach ($this->extractObjects as $extractObject)
+    {
+      $messages = array_merge($messages, $extractObject->$method());
+    }
+
+    return array_unique($messages);
+  }
+}
\ No newline at end of file

Added: plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginModuleExtract.class.php
===================================================================
--- plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginModuleExtract.class.php   
                        (rev 0)
+++ plugins/sfI18nFormExtractorPlugin/lib/sfI18nPluginModuleExtract.class.php   
2010-04-18 16:39:12 UTC (rev 29191)
@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * @author     Geneviève Bastien <[email protected]>
+ */
+class sfI18nPluginModuleExtract extends sfI18nExtract
+{
+  protected $plugin = '';
+  protected $module = '';
+
+  /**
+   * Configures the current extract object.
+   */
+  public function configure()
+  {
+    if (!isset($this->parameters['plugin']))
+    {
+      throw new sfException('You must give a "plugin" parameter when 
extracting for a plugin module.');
+    }
+    
+    if (!isset($this->parameters['module']))
+    {
+      throw new sfException('You must give a "module" parameter when 
extracting for a plugin module.');
+    }
+    $this->plugin = $this->parameters['plugin'];
+    $this->module = $this->parameters['module'];
+
+  }
+
+  /**
+   * Extracts i18n strings.
+   *
+   * This class must be implemented by subclasses.
+   */
+  public function extract()
+  {
+    // Extract from PHP files to find __() calls in actions/ lib/ and 
templates/ directories
+    $moduleDir = sfConfig::get('sf_plugins_dir').'/'.$this->plugin.'/modules/' 
. $this->module;
+
+    $this->extractFromPhpFiles(array(
+      $moduleDir.'/actions',
+      $moduleDir.'/lib',
+      $moduleDir.'/templates',
+    ));
+
+    // Extract from generator.yml files
+    $generator = $moduleDir.'/config/generator.yml';
+    if (file_exists($generator))
+    {
+      $yamlExtractor = new sfI18nYamlGeneratorExtractor();
+      
$this->updateMessages($yamlExtractor->extract(file_get_contents($generator)));
+    }
+
+    // Extract from validate/*.yml files
+    $validateFiles = glob($moduleDir.'/validate/*.yml');
+    if (is_array($validateFiles))
+    {
+      foreach ($validateFiles as $validateFile)
+      {
+        $yamlExtractor = new sfI18nYamlValidateExtractor();
+        
$this->updateMessages($yamlExtractor->extract(file_get_contents($validateFile)));
+      }
+    }
+  }
+}
\ No newline at end of file

Modified: 
plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nFormExtractTask.class.php
===================================================================
--- plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nFormExtractTask.class.php  
2010-04-18 07:30:04 UTC (rev 29190)
+++ plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nFormExtractTask.class.php  
2010-04-18 16:39:12 UTC (rev 29191)
@@ -124,7 +124,7 @@
    */
   private function processFormsInDir($dir,sfI18nFormExtract $extract)
   {
-    $forms = 
sfFinder::type('file')->maxDepth(0)->name('*.php')->discard(array('BaseFormPropel.class.php'))->in($dir);
+    $forms = 
sfFinder::type('file')->name('*.php')->discard(array('BaseFormPropel.class.php'))->in($dir);
     $this->logSection('i18n', sprintf('Found %s forms', count($forms)));
     foreach ($forms as $form)
     {

Added: 
plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nPluginAllExtractTask.class.php
===================================================================
--- 
plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nPluginAllExtractTask.class.php 
                            (rev 0)
+++ 
plugins/sfI18nFormExtractorPlugin/lib/task/sfI18nPluginAllExtractTask.class.php 
    2010-04-18 16:39:12 UTC (rev 29191)
@@ -0,0 +1,155 @@
+<?php 
+
+/**
+ * @author     Geneviève Bastien <[email protected]>
+ */
+
+require_once(dirname(__FILE__).'/../sfI18nFormExtract.class.php');
+require_once(dirname(__FILE__).'/../sfI18nPluginExtract.class.php');
+
+class sfI18nPluginAllExtractTask extends sfBaseTask
+{
+  protected function configure()
+  {
+    $this->addArguments(array(
+                  new sfCommandArgument('plugin', sfCommandArgument::REQUIRED, 
'The plugin to extract for'),
+                  new sfCommandArgument('application', 
sfCommandArgument::REQUIRED, 'The main application'),
+                  new sfCommandArgument('culture', 
sfCommandArgument::REQUIRED, 'The target culture'),
+    ));
+
+    $this->addOptions(array(
+                  new sfCommandOption('display-new', null, 
sfCommandOption::PARAMETER_NONE, 'Output all new found strings'),
+                  new sfCommandOption('display-old', null, 
sfCommandOption::PARAMETER_NONE, 'Output all old strings'),
+                  new sfCommandOption('auto-save', null, 
sfCommandOption::PARAMETER_NONE, 'Save the new strings'),
+                  new sfCommandOption('auto-delete', null, 
sfCommandOption::PARAMETER_NONE, 'Delete old strings'),
+    ));
+
+
+    $this->namespace = 'i18n';
+    $this->name = 'plugin-extract';
+    $this->briefDescription = 'Extracts i18n strings from forms and templates';
+    $this->detailedDescription = <<<EOF
+EOF;
+}
+
+  public function execute($arguments = array(), $options = array())
+  {
+    $this->logSection('i18n', sprintf('extracting i18n strings for the "%s" 
application', $arguments['application']));
+    $config = 
sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
+    
+    $class = $config['i18n']['class'];
+    $params = $config['i18n']['param'];
+    unset($params['cache']);
+    
+    
+    /*
+     * we initialize a default context in case the users call 
sfContext::getInstance in their forms
+     */
+    $config = 
ProjectConfiguration::getApplicationConfiguration($arguments['application'], 
'prod', 'false');
+    sfContext::createInstance($config);
+    
+    
+    // Extract the plugin module's strings
+    $extract = new sfI18nPluginExtract(new $class($this->configuration, new 
sfNoCache(), $params), $arguments['culture'], array('plugin' => 
$arguments['plugin']));
+    
+    $extract->extract();
+
+    $this->logSection('i18n', sprintf('found "%d" new i18n strings', 
count($extract->getNewMessages())));
+    $this->logSection('i18n', sprintf('found "%d" old i18n strings', 
count($extract->getOldMessages())));
+
+    
+    if ($options['display-new'])
+    {
+      $this->logSection('i18n', sprintf('display new i18n strings', 
count($extract->getOldMessages())));
+      foreach ($extract->getNewMessages() as $message)
+      {
+        $this->log('               '.$message."\n");
+      }
+    }
+    
+    if ($options['auto-save'])
+    {
+      $this->logSection('i18n', 'saving new i18n strings');
+
+      $extract->saveNewMessages();
+    }
+
+    if ($options['display-old'])
+    {
+      $this->logSection('i18n', sprintf('display old i18n strings', 
count($extract->getOldMessages())));
+      foreach ($extract->getOldMessages() as $message)
+      {
+        $this->log('               '.$message."\n");
+      }
+    }
+
+    if ($options['auto-delete'])
+    {
+      $this->logSection('i18n', 'deleting old i18n strings');
+
+      $extract->deleteOldMessages();
+    }
+  }
+  
+  /**
+   * This methos process all the forms in plugin dir
+   *
+   * @param string $plugin_name
+   * @param sfI18nFormExtract $extract
+   */
+  private function processModulesInPlugin($plugin_name,sfI18nFormExtract 
$extract)
+  {
+    // Must extract for each module of the plugin
+    $plugindir = sfConfig::get('sf_plugins_dir').'/'.$plugin_name .'/modules';
+    $modules = 
sfFinder::type('dir')->maxDepth(0)->discard(array('.channels','.registry'))->in($plugindir);
+    $this->logSection('i18n', sprintf('Found %s modules', count($modules)));
+    foreach ($modules as $module) {
+      $module = str_replace(sfConfig::get('sf_plugins_dir').'/'.$plugin_name 
.'/modules/','',$module);
+      $this->logSection('i18n', sprintf('Process "%s" module', $module));
+      $extract->setModule($module);
+      $extract->extract();
+    }
+    $extract->setModule($module);
+    $plugin_form_dir = sfConfig::get('sf_plugins_dir').'/'.$plugin_name 
.'/lib/form';
+    $this->processFormsInDir($plugin_form_dir, $extract);
+  }
+  
+  /**
+   * Enter description here...
+   *
+   * @param string $dir
+   * @param sfI18nFormExtract $extract
+   */
+  private function processFormsInDir($dir,sfI18nFormExtract $extract)
+  {
+    $forms = 
sfFinder::type('file')->name('*.php')->discard(array('BaseFormPropel.class.php'))->in($dir);
+    $this->logSection('i18n', sprintf('Found %s forms', count($forms)));
+    foreach ($forms as $form)
+    {
+      $form = basename($form);
+      $this->logSection('i18n', sprintf('Parsing "%s" form', $form));
+      $extract->setForm($form);
+      $extract->extract();
+    }
+  }
+  
+  /**
+   * Enter description here...
+   *
+   * @param sfI18nFormExtract $extract
+   */
+  private function processAll(sfI18nFormExtract $extract)
+  {
+    $this->logSection('i18n', sprintf('Process main %s/lib/form/ dir ', 
sfConfig::get('sf_root_dir')));
+    
$this->processFormsInDir(sfConfig::get('sf_root_dir').'/lib/form/',$extract);
+    $this->logSection('i18n', sprintf('Process plugin %s dir ', 
sfConfig::get('sf_plugins_dir')));
+    $plugins = 
sfFinder::type('dir')->maxDepth(0)->discard(array('.channels','.registry'))->in(sfConfig::get('sf_plugins_dir'));
+    
+    foreach ($plugins as $plugin)
+    {
+      $plugin = str_replace(sfConfig::get('sf_plugins_dir').'/','',$plugin);
+      $this->processPlugin($plugin,$extract);
+    }
+  }
+
+}
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to