Author: david
Date: Fri Nov  4 16:21:43 2011
New Revision: 10271

Log:
Add task to remove duplicate source (keys) from plugin i18n files

Added:
   trunk/lib/task/i18nRemoveDuplicates.class.php
      - copied, changed from r10268, trunk/lib/task/i18nRectifyTask.class.php

Copied and modified: trunk/lib/task/i18nRemoveDuplicates.class.php (from 
r10268, trunk/lib/task/i18nRectifyTask.class.php)
==============================================================================
--- trunk/lib/task/i18nRectifyTask.class.php    Fri Nov  4 14:56:45 2011        
(r10268, copy source)
+++ trunk/lib/task/i18nRemoveDuplicates.class.php       Fri Nov  4 16:21:43 
2011        (r10271)
@@ -26,17 +26,13 @@
  * @author     David Juhasz <[email protected]>
  * @version    SVN: $Id$
  */
-class i18nRectifyTask extends sfBaseTask
+class i18nRemoveDuplicatesTask extends sfBaseTask
 {
   /**
    * @see sfTask
    */
   protected function configure()
   {
-    $this->addArguments(array(
-      new sfCommandArgument('culture', sfCommandArgument::REQUIRED, 'The 
target culture'),
-    ));
-
     $this->addOptions(array(
 
       // http://trac.symfony-project.org/ticket/8352
@@ -44,8 +40,8 @@
     ));
 
     $this->namespace = 'i18n';
-    $this->name = 'rectify';
-    $this->briefDescription = 'Copy i18n target messages from application 
source to plugin source. This prevents losing translated string in the 
fragmentation of application message source into multiple plugin message 
sources.';
+    $this->name = 'remove-duplicates';
+    $this->briefDescription = 'Delete duplicate source messages';
 
     $this->detailedDescription = <<<EOF
 FIXME
@@ -57,57 +53,70 @@
    */
   public function execute($arguments = array(), $options = array())
   {
-    $this->logSection('i18n', sprintf('Rectifying existing i18n strings for 
the "%s" application', $options['application']));
+    $this->logSection('i18n', sprintf('Removing duplicate i18n sources for the 
"%s" application', $options['application']));
+
+    // Loop through plugins
+    $pluginNames = 
sfFinder::type('dir')->maxdepth(0)->relative()->not_name('.')->in(sfConfig::get('sf_plugins_dir'));
+    foreach ($pluginNames as $pluginName)
+    {
+      $this->logSection('i18n', sprintf('Removing %s duplicates', 
$pluginName));
 
-    // get i18n configuration from factories.yml
-    $config = 
sfFactoryConfigHandler::getConfiguration($this->configuration->getConfigPaths('config/factories.yml'));
+      foreach 
(sfFinder::type('files')->in(sfConfig::get('sf_plugins_dir').'/'.$pluginName.'/i18n')
 as $file)
+      {
+        self::deleteDuplicateSource($file);
+      }
+    }
+  }
 
-    $class = $config['i18n']['class'];
-    $params = $config['i18n']['param'];
-    unset($params['cache']);
-
-    // Get current (saved) messages from ALL sources (app and plugin)
-    $this->i18n = new $class($this->configuration, new sfNoCache(), $params);
-    $this->i18n->getMessageSource()->setCulture($arguments['culture']);
-    $this->i18n->getMessageSource()->load();
+  public function deleteDuplicateSource($filename)
+  {
+    $modified = false;
 
-    $currentMessages = array();
-    foreach ($this->i18n->getMessageSource()->read() as $catalogue => 
$translations)
+    // create a new dom, import the existing xml
+    $doc = new DOMDocument;
+    $doc->formatOutput = true;
+    $doc->preserveWhiteSpace = false;
+    $doc->load($filename);
+
+    $xpath = new DOMXPath($doc);
+
+    foreach ($xpath->query('//trans-unit') as $unit)
     {
-      foreach ($translations as $key => $value)
+      foreach ($xpath->query('./source', $unit) as $source)
       {
-        // Use first message that has a valid translation
-        if (0 < strlen(trim($value[0])) && !isset($currentMessages[$key][0]))
+        if (isset($sourceStrings[$source->nodeValue]))
+        {
+          // Remove duplicate
+          $unit->parentNode->removeChild($unit);
+          $modified = true;
+        }
+        else
         {
-          $currentMessages[$key] = $value;
+          $sourceStrings[$source->nodeValue] = true;
         }
+
+        break;
       }
     }
 
-    // Loop through plugins
-    $pluginNames = 
sfFinder::type('dir')->maxdepth(0)->relative()->not_name('.')->in(sfConfig::get('sf_plugins_dir'));
-    foreach ($pluginNames as $pluginName)
+    // Update xliff file if modified
+    if ($modified)
     {
-      $this->logSection('i18n', sprintf('rectifying %s plugin strings', 
$pluginName));
+      $fileNode = $xpath->query('//file')->item(0);
+      $fileNode->setAttribute('date', @date('Y-m-d\TH:i:s\Z'));
 
-      $messageSource = 
sfMessageSource::factory($config['i18n']['param']['source'], 
sfConfig::get('sf_plugins_dir').'/'.$pluginName.'/i18n');
-      $messageSource->setCulture($arguments['culture']);
-      $messageSource->load();
-
-      // If the current plugin source *doesn't* have a translation, then try
-      // and get translated value from $currentMessages
-      foreach($messageSource->read() as $catalogue => $translations)
+      if ($doc->save($filename) > 0)
       {
-        foreach ($translations as $key => &$value)
+        /*
+        if (!empty($this->cache))
         {
-          if (0 == strlen(trim($value[0])) && isset($currentMessages[$key]))
-          {
-            $messageSource->update($key, $currentMessages[$key][0], $value[2]);
-          }
+          $this->cache->remove($variant.':'.$this->culture);
         }
-      }
 
-      $messageSource->save();
+        return true;
+        */
+      }
     }
   }
+
 }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" 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/qubit-commits?hl=en.

Reply via email to