[geeklog-cvs] Translator getstrings,1.1.1.1,1.2 StringExtractor.class.php,1.1.1.1,1.2

tony at iowaoutdoors.org tony at iowaoutdoors.org
Mon Jul 19 16:38:36 EDT 2004


Update of /var/cvs/Translator
In directory www:/tmp/cvs-serv9295

Modified Files:
	getstrings StringExtractor.class.php 
Log Message:
Now works with PHP5 only.  StringExtractor is a bit sloppy, though, and should be reviewed for performance at some point.  Also, I have added the notion of sections so $foo->translate('string1','search') would add 'string1' to the search section and $foo->translate('string2') woudl add string2 to the global section.  

Index: StringExtractor.class.php
===================================================================
RCS file: /var/cvs/Translator/StringExtractor.class.php,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** StringExtractor.class.php	15 Apr 2003 21:12:45 -0000	1.1.1.1
--- StringExtractor.class.php	19 Jul 2004 20:38:33 -0000	1.2
***************
*** 4,16 ****
  * Translations Library - StringExtractor.class.php
  *
! * This source file is subject to version 2.02 of the PHP license,
! * that is bundled with this package in the file LICENSE, and is
! * available at through the world-wide-web at
! * http://www.php.net/license/2_02.txt.
! * If you did not receive a copy of the PHP license and are unable to
! * obtain it through the world-wide-web, please send a note to
! * license at php.net so we can mail you a copy immediately.
  *
! * @author Tony Bibbs <tony AT geeklog DOT net>
  * @copyright Tony Bibbs 2003
  * @package net.geeklog.translations
--- 4,14 ----
  * Translations Library - StringExtractor.class.php
  *
! * This source file is subject to version 2.02 of the PHP license, that is bundled with this package
! * in the file LICENSE, and is available at through the world-wide-web at
! * http://www.php.net/license/2_02.txt. If you did not receive a copy of the PHP license and are
! * unable to obtain it through the world-wide-web, please send a note to license at php.net so we can
! * mail you a copy immediately.
  *
! * @author Tony Bibbs <tony at geeklog.net>
  * @copyright Tony Bibbs 2003
  * @package net.geeklog.translations
***************
*** 22,37 ****
  * Extracts native language strings from code files
  *
! * The string extractor is very similar to xgettext in that it will
! * recursively parse the code files in a given directory, get the native
! * string from all calls to the translate() method and output an XML file
! * that can be distributed to translators.  Simply put all strings needing
! * to be translated are extracted from the PHP code itself and inserted
! * into an XML file.  The XML file is what project administrators would
! * send to their translators.  To help incremental updates, the commandline
! * program mergeversions will assist translators by taking all previously
! * translated text in their old XML file and adding it to a copy of the new
! * file leaving only the new entries empty.
  *
! * @author Tony Bibbs <tony AT geeklog DOT net>
  * @package net.geeklog.translations
  *
--- 20,33 ----
  * Extracts native language strings from code files
  *
! * The string extractor is very similar to xgettext in that it will recursively parse the code files
! * in a given directory, get the native string from all calls to the translate() method and output
! * an XML file that can be distributed to translators.  Simply put all strings needing to be
! * translated are extracted from the PHP code itself and inserted into an XML file.  The XML file is
! * what project administrators would send to their translators.  To help incremental updates, the
! * commandline program mergeversions will assist translators by taking all previously translated
! * text in their old XML file and adding it to a copy of the new file leaving only the new entries
! * empty.
  *
! * @author Tony Bibbs <tony at geeklog.net>
  * @package net.geeklog.translations
  *
***************
*** 42,46 ****
      * @var string
      */
!     var $_nativeLang = null;
      
      /**
--- 38,42 ----
      * @var string
      */
!     private $nativeLang = null;
      
      /**
***************
*** 48,52 ****
      * @var string
      */
!     var $_codeDir = null;
      
      /**
--- 44,48 ----
      * @var string
      */
!     private $codeDir = null;
      
      /**
***************
*** 54,58 ****
      * @var array
      */
!     var $_validExtensions = null;
      
      /**
--- 50,54 ----
      * @var array
      */
!     private $validExtensions = null;
      
      /**
***************
*** 60,64 ****
      * @var array
      */
!     var $_outputDir = null;
      
      /**
--- 56,60 ----
      * @var array
      */
!     private $outputDir = null;
      
      /**
***************
*** 66,70 ****
      * @var array
      */
!     var $_codeDirs = null;
      
      /**
--- 62,66 ----
      * @var array
      */
!     private $codeDirs = null;
      
      /**
***************
*** 72,76 ****
      * @var array
      */
!     var $_outputFp = null;
      
      /**
--- 68,72 ----
      * @var array
      */
!     private $outputFp = null;
      
      /**
***************
*** 78,82 ****
      * @var array
      */
!     var $_appName = null;
      
      /**
--- 74,78 ----
      * @var array
      */
!     private $appName = null;
      
      /**
***************
*** 84,88 ****
      * @var array
      */
!     var $_appVersion = null;
      
      /**
--- 80,84 ----
      * @var array
      */
!     private $appVersion = null;
      
      /**
***************
*** 90,94 ****
      * @var array
      */
!     var $_authorName = null;
      
      /**
--- 86,90 ----
      * @var array
      */
!     private $authorName = null;
      
      /**
***************
*** 96,100 ****
      * @var array
      */
!     var $_authorEmail = null;
      
      /**
--- 92,96 ----
      * @var array
      */
!     private $authorEmail = null;
      
      /**
***************
*** 102,106 ****
      * @var array
      */
!     var $_commandLineMode = null;
      
      /**
--- 98,108 ----
      * @var array
      */
!     private $commandLineMode = null;
!     
!     /**
!     * @access private
!     * @var array
!     */
!     private $stringHolder = null;
      
      /**
***************
*** 109,122 ****
      * Initializes a few private variables
      *
!     * @author Tony Bibbs <tony AT geeklog DOT net>
      * @param string $nativeLang Native language used in code files
      *
      */
!     function StringExtractor($nativeLang)
      {
!         $this->_validExtensions = Array();
!         $this->_codeDirs = Array();
!         $this->_nativeLang = $nativeLang;
!         $this->_commandLineMode = false;
          
      }
--- 111,124 ----
      * Initializes a few private variables
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @param string $nativeLang Native language used in code files
      *
      */
!     public function __construct($nativeLang)
      {
!         $this->validExtensions = Array();
!         $this->codeDirs = Array();
!         $this->nativeLang = $nativeLang;
!         $this->commandLineMode = false;
          
      }
***************
*** 128,144 ****
      * that should be searched
      *
!     * @author Tony Bibbs <tony AT geeklog DOT net>
      * @access private
      * @param string $curDir Current working directory
      *
      */
!     function _getDirs($curDir = '')
      {
          $dirsFound = array();
          
          if (empty($curDir)) {
!             $curDir = $this->_codeDir;
          }
!         $this->_codeDirs[] = $curDir;
          
          $fd = opendir($curDir);
--- 130,146 ----
      * that should be searched
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access private
      * @param string $curDir Current working directory
      *
      */
!     private function getDirs($curDir = '')
      {
          $dirsFound = array();
          
          if (empty($curDir)) {
!             $curDir = $this->codeDir;
          }
!         $this->codeDirs[] = $curDir;
          
          $fd = opendir($curDir);
***************
*** 150,154 ****
          }
          foreach ($dirsFound as $nextDir) {
!             $this->_getDirs($nextDir);
          }
          closedir($fd);
--- 152,156 ----
          }
          foreach ($dirsFound as $nextDir) {
!             $this->getDirs($nextDir);
          }
          closedir($fd);
***************
*** 169,173 ****
      *
      */
!     function _processFiles($dirName)
      {
          $results = array();
--- 171,175 ----
      *
      */
!     private function processFiles($dirName)
      {
          $results = array();
***************
*** 176,187 ****
              if (is_file($dirName . $curFile) AND stristr($curFile,'.php')) {
                  $fileContents = file_get_contents($dirName . $curFile);
!                 preg_match_all("/.->translate\(['\"](.*)['\"]\);/im", $fileContents, $results);
                  $matches = $results[1];
!                 if ($this->_commandLineMode) {
                      echo 'processing file: ' . $dirName . $curFile . "\n";
                      echo 'found ' . count($matches) . " strings\n";
                  }
                  
!                 $this->_addStrings($matches);
              }
          }
--- 178,190 ----
              if (is_file($dirName . $curFile) AND stristr($curFile,'.php')) {
                  $fileContents = file_get_contents($dirName . $curFile);
!                 preg_match_all("/.->translate\((['\"](.*?)['\"])(,['\"](.*?)['\"])?\);/im", $fileContents, $results);
                  $matches = $results[1];
!                 $sections = $results[4];
!                 if ($this->commandLineMode) {
                      echo 'processing file: ' . $dirName . $curFile . "\n";
                      echo 'found ' . count($matches) . " strings\n";
                  }
                  
!                 $this->addStrings($matches, $sections);
              }
          }
***************
*** 201,213 ****
      *
      */
!     function _addStrings($matches)
      {
!         foreach ($matches as $curString) {
!             // NOTE: following line will convert only &,< and >
!             // quotes are going to be OK within XML tags
!             $curString = htmlspecialchars($curString, ENT_NOQUOTES);
!             fwrite($this->_outputFp,
!                 sprintf("    <ENTRY>\n        <STRING_ID>%s</STRING_ID>\n        <STRING></STRING>\n    </ENTRY>\n",
!                 $curString));
          }
      }
--- 204,238 ----
      *
      */
!     private function addStrings($matches, $sections)
      {
!         $numStrings = count($matches);
!         for ($i = 0; $i < $numStrings; $i++) {
!             // NOTE: following line will convert only &,< and > quotes are going to be OK within
!             // XML tags
!             $curString = htmlspecialchars($matches[$i], ENT_NOQUOTES);
!             if (empty($sections[$i])) {
!                 $this->stringHolder['GLOBAL'][] = sprintf("            <ENTRY>\n
!                     <STRING_ID>%s</STRING_ID>\n                <STRING></STRING>\n
!                     </ENTRY>\n", $matches[$i]);
!             } else {
!                 $this->stringHolder[strtoupper($sections[$i])][] = sprintf("
!                     <ENTRY>\n                <STRING_ID>%s</STRING_ID>\n                <STRING>
!                     </STRING>\n            </ENTRY>\n", $matches[$i]);
!             }
!         }
!     }
!     
!     private function writeStrings()
!     {
!         reset($this->stringHolder);
!         $numSections = count($this->stringHolder);
!         for ($i = 0; $i < $numSections; $i++) {
!             $curSection = key($this->stringHolder);
!             fwrite($this->outputFp, sprintf("        <SECTION name=\"%s\">\n", $curSection));
!             foreach ($this->stringHolder[$curSection] as $curString) {
!                 fwrite($this->outputFp, $curString);
!             }
!             fwrite($this->outputFp, "        </SECTION>\n");
!             next($this->stringHolder);
          }
      }
***************
*** 223,235 ****
      *
      */
!     function _addXMLHeaders()
      {
!         fwrite($this->_outputFp,
!             sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<APPLICATION name=\"%s\" version=\"%s\">\n    <TRANSLATOR name=\"%s\" email=\"%s\" />\n    <TRANSLATION language=\"%s\">\n",
!             $this->_appName,
!             $this->_appVersion,
!             $this->_authorName,
!             $this->_authorEmail,
!             $this->_nativeLang));
      }
  
--- 248,262 ----
      *
      */
!     private function addXMLHeaders()
      {
!         fwrite($this->outputFp,
!             sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
!                 <APPLICATION name=\"%s\" version=\"%s\">\n
!                 <TRANSLATOR name=\"%s\" email=\"%s\" />\n    <TRANSLATION language=\"%s\">\n",
!             $this->appName,
!             $this->appVersion,
!             $this->authorName,
!             $this->authorEmail,
!             $this->nativeLang));
      }
  
***************
*** 243,250 ****
      *
      */
!     function _addXMLFooters()
      {
!         fwrite($this->_outputFp,"    </TRANSLATION>\n</APPLICATION>");
!         fclose($this->_outputFp);
      }
      
--- 270,277 ----
      *
      */
!     private function addXMLFooters()
      {
!         fwrite($this->outputFp,"    </TRANSLATION>\n</APPLICATION>");
!         fclose($this->outputFp);
      }
      
***************
*** 262,266 ****
      *
      */
!     function _handleCommandLineArgs($args)
      {
          // This array hold valid arguments and whether or not they are required
--- 289,293 ----
      *
      */
!     private function handleCommandLineArgs($args)
      {
          // This array hold valid arguments and whether or not they are required
***************
*** 273,277 ****
          // all other arguments
          if (in_array('--help',array_keys($args))) {
!             $this->_getHelp();
              return;
          }
--- 300,304 ----
          // all other arguments
          if (in_array('--help',array_keys($args))) {
!             $this->getHelp();
              return;
          }
***************
*** 281,285 ****
              if (!in_array(key($args),$optionNames)) {
                  echo "Invalid argument: $curArg\n";
!                 $this->_getHelp();
                  return;
              }
--- 308,312 ----
              if (!in_array(key($args),$optionNames)) {
                  echo "Invalid argument: $curArg\n";
!                 $this->getHelp();
                  return;
              }
***************
*** 290,294 ****
              if ($validOptions[$curName] == 1 AND empty($args[$curName])) {
                  echo "Missing required argument: $curName\n";
!                 $this->_getHelp();
                  return;
              }
--- 317,321 ----
              if ($validOptions[$curName] == 1 AND empty($args[$curName])) {
                  echo "Missing required argument: $curName\n";
!                 $this->getHelp();
                  return;
              }
***************
*** 357,361 ****
      *
      */
!     function _getHelp()
      {
          echo "\n--lang       Native language used in code.  This is used to\n";
--- 384,388 ----
      *
      */
!     private function getHelp()
      {
          echo "\n--lang       Native language used in code.  This is used to\n";
***************
*** 381,388 ****
      *
      */
!     function setAppProperties($appName, $appVersion)
      {
!         $this->_appName = $appName;
!         $this->_appVersion = $appVersion;
      }
      
--- 408,415 ----
      *
      */
!     public function setAppProperties($appName, $appVersion)
      {
!         $this->appName = $appName;
!         $this->appVersion = $appVersion;
      }
      
***************
*** 397,404 ****
      *
      */
!     function setAuthorProperties($authorName, $authorEmail)
      {
!         $this->_authorName = $authorName;
!         $this->_authorEmail = $authorEmail;
      }
      
--- 424,431 ----
      *
      */
!     public function setAuthorProperties($authorName, $authorEmail)
      {
!         $this->authorName = $authorName;
!         $this->authorEmail = $authorEmail;
      }
      
***************
*** 406,417 ****
      * Sets the main directory where code resides
      *
!     * @author Tony Bibbs <tony AT geeklog DOT net>
      * @access public
      * @param string $path Path of base code directory
      *
      */
!     function setCodeDir($path)
      {
!         $this->_codeDir = $path;
      }
      
--- 433,444 ----
      * Sets the main directory where code resides
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access public
      * @param string $path Path of base code directory
      *
      */
!     public function setCodeDir($path)
      {
!         $this->codeDir = $path;
      }
      
***************
*** 420,436 ****
      * be placed
      *
!     * @author Tony Bibbs <tony AT geeklog DOT net>
      * @access public
      * @param string $path Path where XML file should be placed
      *
      */
!     function setOutputDir($path = './') {
!         if (!is_dir($path) OR !is_writable($path)) {
!             trigger_error('Specified Output Directory ' . $path . ' is not writeable');
              exit;
          }
!         $this->_outputDir = $path;
!         $this->_outputFile = $path . $this->_nativeLang . '.xml';
!         $this->_outputFp = fopen($this->_outputFile,'w');
      }
      
--- 447,468 ----
      * be placed
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access public
      * @param string $path Path where XML file should be placed
      *
      */
!     public function setOutputDir($path = './') {
!         if (!is_dir($path)) {
!             trigger_error("Specified Output Directory, $path, does not exist");
              exit;
          }
!         
!         if (!is_writable($path)) {
!             trigger_error("Specified Output Directory $path is not writeable");
!             exit;
!         }
!         $this->outputDir = $path;
!         $this->outputFile = $path . $this->nativeLang . '.xml';
!         $this->outputFp = fopen($this->outputFile,'w');
      }
      
***************
*** 441,445 ****
      * strings.  The default is php only
      *
!     * @author Tony Bibbs <tony AT geeklog DOT net>
      * @access public
      * @param array $extensionArray Array of valid file extensions
--- 473,477 ----
      * strings.  The default is php only
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access public
      * @param array $extensionArray Array of valid file extensions
***************
*** 448,457 ****
      *
      */
!     function setValidExtensions($extensionArray)
      {
          if (count($extensionArray) == 0) {
              $extensionArray = Array('php');
          }
!         $this->_validExtensions = $extensionArray;
      }
      
--- 480,489 ----
      *
      */
!     public function setValidExtensions($extensionArray)
      {
          if (count($extensionArray) == 0) {
              $extensionArray = Array('php');
          }
!         $this->validExtensions = $extensionArray;
      }
      
***************
*** 463,498 ****
      * translated
      *
!     * @author Tony Bibbs <tony At geeklog DOT net>
      * @access public
      *
      */
!     function getStrings($args = '')
      {
          if (is_array($args)) {
!             $this->_commandLineMode = true;
!             $this->_handleCommandLineArgs($args);
          }
!         if (empty($this->_outputDir)) {
              $this->setOutputDir();
          }
          
!         if (empty($this->_codeDir)) {
              trigger_error('No code directory specified');
              exit;
          }
          
!         if (empty($this->_validExtensions)) {
              $this->setValidExtensions(Array());
          }
          
!         $this->_getDirs();
!         $this->_addXMLHeaders();
!         foreach ($this->_codeDirs as $nextDir) {
!             $this->_processFiles($nextDir);
          }
!         $this->_addXMLFooters();
          
!         if ($this->_commandLineMode) {
!             echo "Done processing strings. XML file generated can be found in " . $this->_outputDir . $this->_nativeLang . ".xml\n";
          }
      }
--- 495,531 ----
      * translated
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access public
      *
      */
!     public function getStrings($args = '')
      {
          if (is_array($args)) {
!             $this->commandLineMode = true;
!             $this->handleCommandLineArgs($args);
          }
!         if (empty($this->outputDir)) {
              $this->setOutputDir();
          }
          
!         if (empty($this->codeDir)) {
              trigger_error('No code directory specified');
              exit;
          }
          
!         if (empty($this->validExtensions)) {
              $this->setValidExtensions(Array());
          }
          
!         $this->getDirs();
!         $this->addXMLHeaders();
!         foreach ($this->codeDirs as $nextDir) {
!             $this->processFiles($nextDir);
          }
!         $this->writeStrings();
!         $this->addXMLFooters();
          
!         if ($this->commandLineMode) {
!             echo "Done processing strings. XML file generated can be found in " . $this->outputDir . $this->nativeLang . ".xml\n";
          }
      }

Index: getstrings
===================================================================
RCS file: /var/cvs/Translator/getstrings,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** getstrings	15 Apr 2003 21:12:45 -0000	1.1.1.1
--- getstrings	19 Jul 2004 20:38:33 -0000	1.2
***************
*** 1,3 ****
! #!/usr/local/bin/php -q
  <?php
  
--- 1,3 ----
! #!/usr/bin/php -q
  <?php
  
***************
*** 5,23 ****
  * Translations Library - getstrings
  *
! * This source file is subject to version 2.02 of the PHP license,
! * that is bundled with this package in the file LICENSE, and is
! * available at through the world-wide-web at
! * http://www.php.net/license/2_02.txt.
! * If you did not receive a copy of the PHP license and are unable to
! * obtain it through the world-wide-web, please send a note to
! * license at php.net so we can mail you a copy immediately.
  *
  * Here's an example of how to use this command:
! * ./getstrings --lang=en --source=./tmp/ --target=./output/
! *    --appname=Geeklog --appversion=2.0 --author="Tony Bibbs"
! *    --email=tony at geeklog.net
  *
! * @author Tony Bibbs <tony AT geeklog DOT net>
! * @copyright Tony Bibbs 2003
  * @package net.geeklog.translations
  * @version $Id$
--- 5,20 ----
  * Translations Library - getstrings
  *
! * This source file is subject to version 2.02 of the PHP license, that is bundled with this package
! * in the file LICENSE, and is available at through the world-wide-web at
! * http://www.php.net/license/2_02.txt. If you did not receive a copy of the PHP license and are
! * unable to obtain it through the world-wide-web, please send a note to license at php.net so we can
! * mail you a copy immediately.
  *
  * Here's an example of how to use this command:
! * ./getstrings --lang=en --source=./tmp/ --target=./output/ --appname=Geeklog --appversion=2.0
! *    --author="Tony Bibbs" --email=tony at geeklog.net
  *
! * @author Tony Bibbs <tony at geeklog.net>
! * @copyright Tony Bibbs 2003-2004
  * @package net.geeklog.translations
  * @version $Id$




More information about the geeklog-cvs mailing list