extract.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 683 行 · 第 1/2 页

PHP
683
字号
<?php/* SVN FILE: $Id: extract.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * Short description for file. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. *                              1785 E. Sahara Avenue, Suite 490-204 *                              Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc. * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package         cake * @subpackage      cake.cake.console.libs * @since           CakePHP(tm) v 1.2.0.5012 * @version         $Revision: 7118 $ * @modifiedby      $LastChangedBy: gwoo $ * @lastmodified    $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license         http://www.opensource.org/licenses/mit-license.php The MIT License *//** * Only used when -debug option */	ob_start();	$singularReturn = __('Singular string  return __()', true);	$singularEcho = __('Singular string  echo __()');	$pluralReturn = __n('% apple in the bowl (plural string return __n())', '% apples in the blowl (plural string 2 return __n())', 3, true);	$pluralEcho = __n('% apple in the bowl (plural string 2 echo __n())', '% apples in the blowl (plural string 2 echo __n()', 3);	$singularDomainReturn = __d('controllers', 'Singular string domain lookup return __d()', true);	$singularDomainEcho = __d('controllers', 'Singular string domain lookup echo __d()');	$pluralDomainReturn = __dn('controllers', '% pears in the bowl (plural string domain lookup return __dn())', '% pears in the blowl (plural string domain lookup return __dn())', 3, true);	$pluralDomainEcho = __dn('controllers', '% pears in the bowl (plural string domain lookup echo __dn())', '% pears in the blowl (plural string domain lookup echo __dn())', 3);	$singularDomainCategoryReturn = __dc('controllers', 'Singular string domain and category lookup return __dc()', 5, true);	$singularDomainCategoryEcho = __dc('controllers', 'Singular string domain and category lookup echo __dc()', 5);	$pluralDomainCategoryReturn = __dcn('controllers', '% apple in the bowl (plural string 1 domain and category lookup return __dcn())', '% apples in the blowl (plural string 2 domain and category lookup return __dcn())', 3, 5, true);	$pluralDomainCategoryEcho = __dcn('controllers', '% apple in the bowl (plural string 1 domain and category lookup echo __dcn())', '% apples in the blowl (plural string 2 domain and category lookup echo __dcn())', 3, 5);	$categoryReturn = __c('Category string lookup line return __c()', 5, true);	$categoryEcho = __c('Category string  lookup line echo __c()', 5);	ob_end_clean();/** * Language string extractor * * @package     cake * @subpackage  cake.cake.console.libs */class ExtractTask extends Shell{/** * Path to use when looking for strings * * @var string * @access public */	var $path = null;/** * Files from where to extract * * @var array * @access public */	var $files = array();/** * Filename where to deposit translations * * @var string * @access private */	var $__filename = 'default';/** * True if all strings should be merged into one file * * @var boolean * @access private */	var $__oneFile = true;/** * Current file being processed * * @var string * @access private */	var $__file = null;/** * Extracted tokens * * @var array * @access private */	var $__tokens = array();/** * Extracted strings * * @var array * @access private */	var $__strings = array();/** * History of file versions * * @var array * @access private */	var $__fileVersions = array();/** * Destination path * * @var string * @access private */	var $__output = null;/** * Execution method always used for tasks * * @access public */	function execute() {		if (isset($this->params['files']) && !is_array($this->params['files'])) {			$this->files = explode(',', $this->params['files']);		}		if (isset($this->params['path'])) {			$this->path = $this->params['path'];		} else {			$response = '';			while ($response == '') {				$response = $this->in("What is the full path you would like to extract?\nExample: " . $this->params['root'] . DS . "myapp\n[Q]uit", null, 'Q');				if (strtoupper($response) === 'Q') {					$this->out('Extract Aborted');					$this->_stop();				}			}			if (is_dir($response)) {				$this->path = $response;			} else {				$this->err('The directory path you supplied was not found. Please try again.');				$this->execute();			}		}		if (isset($this->params['debug'])) {			$this->path = ROOT;			$this->files = array(__FILE__);		}		if (isset($this->params['output'])) {			$this->__output = $this->params['output'];		} else {			$response = '';			while ($response == '') {				$response = $this->in("What is the full path you would like to output?\nExample: " . $this->path . DS . "locale\n[Q]uit", null, $this->path . DS . "locale");				if (strtoupper($response) === 'Q') {					$this->out('Extract Aborted');					$this->_stop();				}			}			if (is_dir($response)) {				$this->__output = $response . DS;			} else {				$this->err('The directory path you supplied was not found. Please try again.');				$this->execute();			}		}		if (empty($this->files)) {			$this->files = $this->__searchDirectory();		}		$this->__extract();	}/** * Extract text * * @access private */	function __extract() {		$this->out('');		$this->out('');		$this->out(__('Extracting...', true));		$this->hr();		$this->out(__('Path: ', true). $this->path);		$this->out(__('Output Directory: ', true). $this->__output);		$this->hr();		$response = '';		$filename = '';		while ($response == '') {		    $response = $this->in(__('Would you like to merge all translations into one file?', true), array('y','n'), 'y');			if (strtolower($response) == 'n') {				$this->__oneFile = false;			} else {				while ($filename == '') {					$filename = $this->in(__('What should we name this file?', true), null, $this->__filename);					if ($filename == '') {						$this->out(__('The filesname you supplied was empty. Please try again.', true));					}				}				$this->__filename = $filename;			}		}		$this->__extractTokens();	}/** * Show help options * * @access public */	function help() {	    $this->out(__('CakePHP Language String Extraction:', true));		$this->hr();		$this->out(__('The Extract script generates .pot file(s) with translations', true));		$this->out(__('By default the .pot file(s) will be place in the locale directory of -app', true));		$this->out(__('By default -app is ROOT/app', true));		$this->hr();		$this->out(__('usage: cake i18n extract [command] [path...]', true));		$this->out('');		$this->out(__('commands:', true));		$this->out(__('   -app [path...]: directory where your application is located', true));		$this->out(__('   -root [path...]: path to install', true));		$this->out(__('   -core [path...]: path to cake directory', true));		$this->out(__('   -path [path...]: Full path to directory to extract strings', true));		$this->out(__('   -output [path...]: Full path to output directory', true));		$this->out(__('   -files: [comma separated list of files, full path to file is needed]', true));		$this->out(__('   cake i18n extract help: Shows this help message.', true));		$this->out(__('   -debug: Perform self test.', true));		$this->out('');	}/** * Extract tokens out of all files to be processed * * @access private */	function __extractTokens() {		foreach ($this->files as $file) {			$this->__file = $file;			$this->out(sprintf(__('Processing %s...', true), $file));			$code = file_get_contents($file);			$this->__findVersion($code, $file);			$allTokens = token_get_all($code);			$this->__tokens = array();			$lineNumber = 1;			foreach ($allTokens as $token) {				if ((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {					if (is_array($token)) {						$token[] = $lineNumber;					}					$this->__tokens[] = $token;				}				if (is_array($token)) {					$lineNumber += count(split("\n", $token[1])) - 1;				} else {					$lineNumber += count(split("\n", $token)) - 1;				}			}			unset($allTokens);			$this->basic();			$this->basic('__c');			$this->extended();			$this->extended('__dc', 2);			$this->extended('__n', 0, true);			$this->extended('__dn', 2, true);			$this->extended('__dcn', 4, true);		}		$this->__buildFiles();		$this->__writeFiles();		$this->out('Done.');	}/** * Will parse  __(), __c() functions * * @param string $functionName Function name that indicates translatable string (e.g: '__') * @access public */	function basic($functionName = '__') {		$count = 0;		$tokenCount = count($this->__tokens);		while (($tokenCount - $count) > 3) {			list($countToken, $parenthesis, $middle, $right) = array($this->__tokens[$count], $this->__tokens[$count + 1], $this->__tokens[$count + 2], $this->__tokens[$count + 3]);			if (!is_array($countToken)) {				$count++;				continue;			}			list($type, $string, $line) = $countToken;			if (($type == T_STRING) && ($string == $functionName) && ($parenthesis == '(')) {				if (in_array($right, array(')', ','))				&& (is_array($middle) && ($middle[0] == T_CONSTANT_ENCAPSED_STRING))) {					if ($this->__oneFile === true) {						$this->__strings[$this->__formatString($middle[1])][$this->__file][] = $line;					} else {						$this->__strings[$this->__file][$this->__formatString($middle[1])][] = $line;					}				} else {					$this->__markerError($this->__file, $line, $functionName, $count);				}			}			$count++;		}	}/** * Will parse __d(), __dc(), __n(), __dn(), __dcn() * * @param string $functionName Function name that indicates translatable string (e.g: '__') * @param integer $shift Number of parameters to shift to find translateable string * @param boolean $plural Set to true if function supports plural format, false otherwise * @access public */	function extended($functionName = '__d', $shift = 0, $plural = false) {		$count = 0;		$tokenCount = count($this->__tokens);		while (($tokenCount - $count) > 7) {			list($countToken, $firstParenthesis) = array($this->__tokens[$count], $this->__tokens[$count + 1]);			if (!is_array($countToken)) {				$count++;				continue;			}			list($type, $string, $line) = $countToken;			if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {				$position = $count;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?