Development

Changeset 4395

You must first sign up to be able to contribute.

Changeset 4395

Show
Ignore:
Timestamp:
06/25/07 20:40:01 (1 year ago)
Author:
fabien
Message:

added a very simple i18n-find task to find strings that are not i18n ready in your templates

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/data/tasks/sfPakeI18N.php

    r4373 r4395  
    1717pake_desc('extract i18n strings from php files'); 
    1818pake_task('i18n-extract'); 
     19 
     20pake_desc('find non "i18n ready" strings in an application'); 
     21pake_task('i18n-find'); 
     22 
     23function run_i18n_find($task, $args) 
     24{ 
     25  if (!count($args)) 
     26  { 
     27    throw new Exception('You must provide the application.'); 
     28  } 
     29 
     30  $app = $args[0]; 
     31 
     32  if (!is_dir(sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.$app)) 
     33  { 
     34    throw new Exception(sprintf('The app "%s" does not exist.', $app)); 
     35  } 
     36 
     37  pake_echo_action('i18n', sprintf('find non "i18n ready" strings in the "%s" application', $app)); 
     38 
     39  sfConfig::set('sf_app', $app); 
     40  sfConfig::set('sf_environment', 'dev'); 
     41  include(sfConfig::get('sf_symfony_data_dir').'/config/constants.php'); 
     42 
     43  // Look in templates 
     44  $moduleNames = sfFinder::type('dir')->maxdepth(0)->ignore_version_control()->relative()->in(sfConfig::get('sf_app_dir').'/modules'); 
     45  $strings = array(); 
     46  foreach ($moduleNames as $moduleName) 
     47  { 
     48    $dir = sfConfig::get('sf_app_dir').'/modules/'.$moduleName.'/templates'; 
     49    $templates = pakeFinder::type('file')->name('*.php')->relative()->in($dir); 
     50    foreach ($templates as $template) 
     51    { 
     52      $dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8')); 
     53      @$dom->loadXML('<doc>'.file_get_contents($dir.'/'.$template).'</doc>'); 
     54 
     55      $nodes = array($dom); 
     56      while ($nodes) 
     57      { 
     58        $node = array_shift($nodes); 
     59 
     60        if (XML_TEXT_NODE === $node->nodeType) 
     61        { 
     62          if (!$node->isWhitespaceInElementContent()) 
     63          { 
     64            if (!isset($strings[$moduleName][$template])) 
     65            { 
     66              if (!isset($strings[$moduleName])) 
     67              { 
     68                $strings[$moduleName] = array(); 
     69              } 
     70 
     71              $strings[$moduleName][$template] = array(); 
     72            } 
     73 
     74            $strings[$moduleName][$template][] = $node->nodeValue; 
     75          } 
     76        } 
     77        else if ($node->childNodes) 
     78        { 
     79          for ($i = 0, $max = $node->childNodes->length; $i < $max; $i++) 
     80          { 
     81            $nodes[] = $node->childNodes->item($i); 
     82          } 
     83        } 
     84      } 
     85    } 
     86  } 
     87 
     88  foreach ($strings as $moduleName => $templateStrings) 
     89  { 
     90    foreach ($templateStrings as $template => $messages) 
     91    { 
     92      pake_echo_action('i18n', sprintf('strings in "%s:%s"', $moduleName, $template)); 
     93      foreach ($messages as $message) 
     94      { 
     95        echo "  $message\n"; 
     96      } 
     97    } 
     98  } 
     99} 
    19100 
    20101function run_i18n_extract($task, $args, $options)