Development

/branches/1.3/lib/task/i18n/sfI18nFindTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/task/i18n/sfI18nFindTask.class.php

Revision 8453, 3.9 kB (checked in by fabien, 7 years ago)

added logic to i18n:find to also find strings in PHP code (closes #3320)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Finds non "i18n ready" strings in an application.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfI18nFindTask extends sfBaseTask
20 {
21   /**
22    * @see sfTask
23    */
24   protected function configure()
25   {
26     $this->addArguments(array(
27       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
28     ));
29
30     $this->addOptions(array(
31       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
32     ));
33
34     $this->namespace = 'i18n';
35     $this->name = 'find';
36     $this->briefDescription = 'Finds non "i18n ready" strings in an application';
37
38     $this->detailedDescription = <<<EOF
39 The [i18n:find|INFO] task finds non internationalized strings embedded in templates:
40
41   [./symfony i18n:find frontend|INFO]
42
43 This task is able to find non internationalized strings in pure HTML and in PHP code:
44
45   <p>Non i18n text</p>
46   <p><?php echo 'Test' ?></p>
47
48 As the task returns all strings embedded in PHP, you can have some false positive (especially
49 if you use the string syntax for helper arguments).
50 EOF;
51   }
52
53   /**
54    * @see sfTask
55    */
56   public function execute($arguments = array(), $options = array())
57   {
58     $this->logSection('i18n', sprintf('find non "i18n ready" strings in the "%s" application', $arguments['application']));
59
60     // Look in templates
61     $dirs = array();
62     $moduleNames = sfFinder::type('dir')->maxdepth(0)->relative()->in(sfConfig::get('sf_app_module_dir'));
63     foreach ($moduleNames as $moduleName)
64     {
65       $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates';
66     }
67     $dirs[] = sfConfig::get('sf_app_dir').'/templates';
68
69     $strings = array();
70     foreach ($dirs as $dir)
71     {
72       $templates = sfFinder::type('file')->name('*.php')->in($dir);
73       foreach ($templates as $template)
74       {
75         if (!isset($strings[$template]))
76         {
77           $strings[$template] = array();
78         }
79
80         $dom = new DomDocument('1.0', sfConfig::get('sf_charset', 'UTF-8'));
81         $content = file_get_contents($template);
82
83         // remove doctype
84         $content = preg_replace('/<!DOCTYPE.*?>/', '', $content);
85
86         @$dom->loadXML('<doc>'.$content.'</doc>');
87
88         $nodes = array($dom);
89         while ($nodes)
90         {
91           $node = array_shift($nodes);
92
93           if (XML_TEXT_NODE === $node->nodeType)
94           {
95             if (!$node->isWhitespaceInElementContent())
96             {
97               $strings[$template][] = $node->nodeValue;
98             }
99           }
100           else if ($node->childNodes)
101           {
102             for ($i = 0, $max = $node->childNodes->length; $i < $max; $i++)
103             {
104               $nodes[] = $node->childNodes->item($i);
105             }
106           }
107           else if ('DOMProcessingInstruction' == get_class($node) && 'php' == $node->target)
108           {
109             // processing instruction node
110             $tokens = token_get_all('<?php '.$node->nodeValue);
111             foreach ($tokens as $token)
112             {
113               if (is_array($token))
114               {
115                 list($id, $text) = $token;
116
117                 if (T_CONSTANT_ENCAPSED_STRING === $id)
118                 {
119                   $strings[$template][] = substr($text, 1, -1);
120                 }
121               }
122             }
123           }
124         }
125       }
126     }
127
128     foreach ($strings as $template => $messages)
129     {
130       if (!$messages)
131       {
132         continue;
133       }
134
135       $this->logSection('i18n', sprintf('strings in "%s"', str_replace(sfConfig::get('sf_root_dir'), '', $template)), 1000);
136       foreach ($messages as $message)
137       {
138         $this->log("  $message\n");
139       }
140     }
141   }
142 }
143
Note: See TracBrowser for help on using the browser.