2007-01-01 22:03:15 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* Short description for file .
*
* Long description for file
*
* PHP versions 4 and 5
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
2007-03-29 16:53:10 +00:00
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2007-01-01 22:03:15 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2007-03-29 16:53:10 +00:00
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
2007-05-14 01:14:41 +00:00
* @ subpackage cake . cake . console . libs
* @ since CakePHP ( tm ) v 1.2 . 0.5012
2007-03-29 16:53:10 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2007-01-01 22:03:15 +00:00
*/
2007-05-13 23:20:47 +00:00
/**
* Only used when - debug option
*/
2007-09-18 03:34:44 +00:00
ob_start ();
2007-04-01 07:43:52 +00:00
$singularReturn = __ ( 'Singular string return __()' , true );
$singularEcho = __ ( 'Singular string echo __()' );
2007-03-29 16:53:10 +00:00
$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 );
2007-04-01 07:43:52 +00:00
$singularDomainReturn = __d ( 'controllers' , 'Singular string domain lookup return __d()' , true );
$singularDomainEcho = __d ( 'controllers' , 'Singular string domain lookup echo __d()' );
2007-03-29 16:53:10 +00:00
$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 );
2007-04-01 07:43:52 +00:00
$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 );
2007-03-29 16:53:10 +00:00
$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 );
2007-04-01 07:43:52 +00:00
$categoryReturn = __c ( 'Category string lookup line return __c()' , 5 , true );
2007-03-29 16:53:10 +00:00
$categoryEcho = __c ( 'Category string lookup line echo __c()' , 5 );
2007-09-18 03:34:44 +00:00
ob_end_clean ();
2007-01-01 22:03:15 +00:00
/**
2007-03-29 16:53:10 +00:00
* Language string extractor
2007-01-01 22:03:15 +00:00
*
2007-03-29 16:53:10 +00:00
* @ package cake
2007-05-14 01:14:41 +00:00
* @ subpackage cake . cake . console . libs
2007-01-01 22:03:15 +00:00
*/
2007-09-18 03:34:44 +00:00
class ExtractTask extends Shell {
2007-10-22 05:52:20 +00:00
/**
* Path to use when looking for strings
*
* @ var string
* @ access public
*/
2007-03-29 16:53:10 +00:00
var $path = null ;
2007-10-22 05:52:20 +00:00
/**
* Files from where to extract
*
* @ var array
* @ access public
*/
2007-03-29 16:53:10 +00:00
var $files = array ();
2007-10-22 05:52:20 +00:00
/**
* Filename where to deposit translations
*
* @ var string
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__filename = 'default' ;
2007-10-22 05:52:20 +00:00
/**
* True if all strings should be merged into one file
*
* @ var boolean
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__oneFile = true ;
2007-10-22 05:52:20 +00:00
/**
* Current file being processed
*
* @ var string
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__file = null ;
2007-10-22 05:52:20 +00:00
/**
* Extracted tokens
*
* @ var array
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__tokens = array ();
2007-10-22 05:52:20 +00:00
/**
* Extracted strings
*
* @ var array
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__strings = array ();
2007-10-22 05:52:20 +00:00
/**
* History of file versions
*
* @ var array
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__fileVersions = array ();
2007-10-22 05:52:20 +00:00
/**
* Destination path
*
* @ var string
* @ access private
*/
2007-03-29 16:53:10 +00:00
var $__output = null ;
2007-09-18 03:34:44 +00:00
/**
* Override initialize
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-09-18 03:34:44 +00:00
*/
2007-05-13 23:20:47 +00:00
function initialize () {
2007-06-20 07:51:52 +00:00
if ( isset ( $this -> params [ 'files' ]) && ! is_array ( $this -> params [ 'files' ])) {
2007-05-13 23:20:47 +00:00
$this -> files = explode ( ',' , $this -> params [ 'files' ]);
}
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> params [ 'path' ])) {
2007-05-13 23:20:47 +00:00
$this -> path = $this -> params [ 'path' ];
} else {
2007-10-20 05:55:37 +00:00
$response = '' ;
while ( $response == '' ) {
$response = $this -> in ( " What is the full path you would like to extract? \n Example: " . $this -> params [ 'root' ] . DS . " myapp \n [Q]uit " , null , 'Q' );
if ( strtoupper ( $response ) === 'Q' ) {
$this -> out ( 'Extract Aborted' );
exit ();
}
}
if ( is_dir ( $response )) {
$this -> path = $response ;
} else {
$this -> err ( 'The directory path you supplied was not found. Please try again.' );
$this -> initialize ();
}
2007-05-13 23:20:47 +00:00
}
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> params [ 'debug' ])) {
2007-05-13 23:20:47 +00:00
$this -> path = ROOT ;
$this -> files = array ( __FILE__ );
}
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> params [ 'output' ])) {
2007-05-13 23:20:47 +00:00
$this -> __output = $this -> params [ 'output' ];
} else {
2007-10-20 05:55:37 +00:00
$response = '' ;
while ( $response == '' ) {
$response = $this -> in ( " What is the full path you would like to output? \n Example: " . $this -> path . DS . " locale \n [Q]uit " , null , $this -> path . DS . " locale " );
if ( strtoupper ( $response ) === 'Q' ) {
$this -> out ( 'Extract Aborted' );
exit ();
}
}
if ( is_dir ( $response )) {
$this -> __output = $response . DS ;
} else {
$this -> err ( 'The directory path you supplied was not found. Please try again.' );
$this -> initialize ();
}
2007-05-13 23:20:47 +00:00
}
2007-06-20 07:51:52 +00:00
if ( empty ( $this -> files )) {
2007-05-13 23:20:47 +00:00
$this -> files = $this -> __searchDirectory ();
}
2007-01-01 22:03:15 +00:00
}
2007-09-18 03:34:44 +00:00
/**
* Override startup
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-09-18 03:34:44 +00:00
*/
2007-10-22 05:52:20 +00:00
function startup () {
}
2007-09-18 03:34:44 +00:00
/**
* Execution method always used for tasks
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-09-18 03:34:44 +00:00
*/
function execute () {
2007-05-13 23:20:47 +00:00
$this -> out ( '' );
$this -> out ( '' );
2007-09-18 03:34:44 +00:00
$this -> out ( __ ( 'Extracting...' , true ));
2007-05-13 23:20:47 +00:00
$this -> hr ();
2007-09-18 03:34:44 +00:00
$this -> out ( __ ( 'Path: ' , true ) . $this -> path );
$this -> out ( __ ( 'Output Directory: ' , true ) . $this -> __output );
2007-05-13 23:20:47 +00:00
$this -> hr ();
2007-03-29 16:53:10 +00:00
$response = '' ;
$filename = '' ;
2007-06-20 06:15:35 +00:00
while ( $response == '' ) {
2007-09-18 03:34:44 +00:00
$response = $this -> in ( __ ( 'Would you like to merge all translations into one file?' , true ), array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( strtolower ( $response ) == 'n' ) {
2007-03-29 16:53:10 +00:00
$this -> __oneFile = false ;
} else {
2007-06-20 06:15:35 +00:00
while ( $filename == '' ) {
2007-09-18 03:34:44 +00:00
$filename = $this -> in ( __ ( 'What should we name this file?' , true ), null , $this -> __filename );
2007-03-29 16:53:10 +00:00
if ( $filename == '' ) {
2007-09-18 03:34:44 +00:00
$this -> out ( __ ( 'The filesname you supplied was empty. Please try again.' , true ));
2007-03-29 16:53:10 +00:00
}
}
$this -> __filename = $filename ;
}
}
$this -> __extractTokens ();
}
2007-10-22 05:52:20 +00:00
/**
* Show help options
*
* @ access public
*/
2007-03-29 16:53:10 +00:00
function help () {
2007-09-18 03:34:44 +00:00
$this -> out ( __ ( 'CakePHP Language String Extraction:' , true ));
2007-05-13 23:20:47 +00:00
$this -> hr ();
2007-09-18 03:34:44 +00:00
$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 ));
2007-05-13 23:20:47 +00:00
$this -> out ( '' );
2007-09-18 03:34:44 +00:00
$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 ));
2007-05-13 23:20:47 +00:00
$this -> out ( '' );
2007-03-29 16:53:10 +00:00
}
2007-10-22 05:52:20 +00:00
/**
* Extract tokens out of all files to be processed
*
* @ access private
*/
2007-06-20 07:51:52 +00:00
function __extractTokens () {
2007-03-29 16:53:10 +00:00
foreach ( $this -> files as $file ) {
$this -> __file = $file ;
2007-09-18 03:34:44 +00:00
$this -> out ( sprintf ( __ ( 'Processing %s...' , true ), $file ));
2007-03-29 16:53:10 +00:00
$code = file_get_contents ( $file );
$this -> __findVersion ( $code , $file );
$allTokens = token_get_all ( $code );
$this -> __tokens = array ();
$lineNumber = 1 ;
2007-06-20 06:15:35 +00:00
foreach ( $allTokens as $token ) {
if (( ! is_array ( $token )) || (( $token [ 0 ] != T_WHITESPACE ) && ( $token [ 0 ] != T_INLINE_HTML ))) {
if ( is_array ( $token )) {
2007-03-29 16:53:10 +00:00
$token [] = $lineNumber ;
}
$this -> __tokens [] = $token ;
}
2007-06-20 06:15:35 +00:00
if ( is_array ( $token )) {
2007-03-29 16:53:10 +00:00
$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 ();
2007-05-13 23:20:47 +00:00
$this -> out ( 'Done.' );
2007-03-29 16:53:10 +00:00
}
/**
* Will parse __ (), __c () functions
*
2007-10-22 05:52:20 +00:00
* @ param string $functionName Function name that indicates translatable string ( e . g : '__' )
* @ access public
2007-03-29 16:53:10 +00:00
*/
2007-10-22 05:52:20 +00:00
function basic ( $functionName = '__' ) {
2007-03-29 16:53:10 +00:00
$count = 0 ;
$tokenCount = count ( $this -> __tokens );
2007-06-20 06:15:35 +00:00
while (( $tokenCount - $count ) > 3 ) {
2007-03-29 16:53:10 +00:00
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 ;
2007-10-22 05:52:20 +00:00
if (( $type == T_STRING ) && ( $string == $functionName ) && ( $parenthesis == '(' )) {
2007-03-29 16:53:10 +00:00
2007-09-18 03:34:44 +00:00
if ( in_array ( $right , array ( ')' , ',' ))
2007-03-29 16:53:10 +00:00
&& ( 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 {
2007-10-22 05:52:20 +00:00
$this -> __markerError ( $this -> __file , $line , $functionName , $count );
2007-03-29 16:53:10 +00:00
}
}
$count ++ ;
}
}
/**
* Will parse __d (), __dc (), __n (), __dn (), __dcn ()
*
2007-10-22 05:52:20 +00:00
* @ param string $functionName Function name that indicates translatable string ( e . g : '__' )
2007-10-22 16:11:12 +00:00
* @ param integer $shift Number of parameters to shift to find translateable string
2007-10-22 16:54:36 +00:00
* @ param boolean $plural Set to true if function supports plural format , false otherwise
2007-10-22 05:52:20 +00:00
* @ access public
2007-03-29 16:53:10 +00:00
*/
2007-10-22 05:52:20 +00:00
function extended ( $functionName = '__d' , $shift = 0 , $plural = false ) {
2007-03-29 16:53:10 +00:00
$count = 0 ;
$tokenCount = count ( $this -> __tokens );
2007-06-20 06:15:35 +00:00
while (( $tokenCount - $count ) > 7 ) {
2007-03-29 16:53:10 +00:00
list ( $countToken , $firstParenthesis ) = array ( $this -> __tokens [ $count ], $this -> __tokens [ $count + 1 ]);
2007-06-20 06:15:35 +00:00
if ( ! is_array ( $countToken )) {
2007-03-29 16:53:10 +00:00
$count ++ ;
continue ;
}
list ( $type , $string , $line ) = $countToken ;
2007-10-22 05:52:20 +00:00
if (( $type == T_STRING ) && ( $string == $functionName ) && ( $firstParenthesis == '(' )) {
2007-03-29 16:53:10 +00:00
$position = $count ;
$depth = 0 ;
2007-06-20 06:15:35 +00:00
while ( $depth == 0 ) {
2007-09-18 03:34:44 +00:00
if ( $this -> __tokens [ $position ] == '(' ) {
2007-03-29 16:53:10 +00:00
$depth ++ ;
2007-09-18 03:34:44 +00:00
} elseif ( $this -> __tokens [ $position ] == ')' ) {
2007-03-29 16:53:10 +00:00
$depth -- ;
}
$position ++ ;
}
2007-06-20 06:15:35 +00:00
if ( $plural ) {
2007-03-29 16:53:10 +00:00
$end = $position + $shift + 7 ;
2007-06-20 06:15:35 +00:00
if ( $this -> __tokens [ $position + $shift + 5 ] === ')' ) {
2007-03-29 16:53:10 +00:00
$end = $position + $shift + 5 ;
}
2007-06-20 06:15:35 +00:00
if ( empty ( $shift )) {
2007-03-29 16:53:10 +00:00
list ( $singular , $firstComma , $plural , $seoncdComma , $endParenthesis ) = array ( $this -> __tokens [ $position ], $this -> __tokens [ $position + 1 ], $this -> __tokens [ $position + 2 ], $this -> __tokens [ $position + 3 ], $this -> __tokens [ $end ]);
2007-09-18 03:34:44 +00:00
$condition = ( $seoncdComma == ',' );
2007-03-29 16:53:10 +00:00
} else {
list ( $domain , $firstComma , $singular , $seoncdComma , $plural , $comma3 , $endParenthesis ) = array ( $this -> __tokens [ $position ], $this -> __tokens [ $position + 1 ], $this -> __tokens [ $position + 2 ], $this -> __tokens [ $position + 3 ], $this -> __tokens [ $position + 4 ], $this -> __tokens [ $position + 5 ], $this -> __tokens [ $end ]);
2007-09-18 03:34:44 +00:00
$condition = ( $comma3 == ',' );
2007-03-29 16:53:10 +00:00
}
$condition = $condition &&
( is_array ( $singular ) && ( $singular [ 0 ] == T_CONSTANT_ENCAPSED_STRING )) &&
( is_array ( $plural ) && ( $plural [ 0 ] == T_CONSTANT_ENCAPSED_STRING ));
} else {
2007-06-20 06:15:35 +00:00
if ( $this -> __tokens [ $position + $shift + 5 ] === ')' ) {
2007-03-29 16:53:10 +00:00
$comma = $this -> __tokens [ $position + $shift + 3 ];
$end = $position + $shift + 5 ;
} else {
$comma = null ;
$end = $position + $shift + 3 ;
}
list ( $domain , $firstComma , $text , $seoncdComma , $endParenthesis ) = array ( $this -> __tokens [ $position ], $this -> __tokens [ $position + 1 ], $this -> __tokens [ $position + 2 ], $comma , $this -> __tokens [ $end ]);
2007-09-18 03:34:44 +00:00
$condition = ( $seoncdComma == ',' || $seoncdComma === null ) &&
2007-03-29 16:53:10 +00:00
( is_array ( $domain ) && ( $domain [ 0 ] == T_CONSTANT_ENCAPSED_STRING )) &&
( is_array ( $text ) && ( $text [ 0 ] == T_CONSTANT_ENCAPSED_STRING ));
}
2007-09-18 03:34:44 +00:00
if (( $endParenthesis == ')' ) && $condition ) {
2007-06-20 06:15:35 +00:00
if ( $this -> __oneFile === true ) {
if ( $plural ) {
2007-03-29 16:53:10 +00:00
$this -> __strings [ $this -> __formatString ( $singular [ 1 ]) . " \0 " . $this -> __formatString ( $plural [ 1 ])][ $this -> __file ][] = $line ;
} else {
$this -> __strings [ $this -> __formatString ( $text [ 1 ])][ $this -> __file ][] = $line ;
}
} else {
2007-06-20 06:15:35 +00:00
if ( $plural ) {
2007-03-29 16:53:10 +00:00
$this -> __strings [ $this -> __file ][ $this -> __formatString ( $singular [ 1 ]) . " \0 " . $this -> __formatString ( $plural [ 1 ])][] = $line ;
} else {
$this -> __strings [ $this -> __file ][ $this -> __formatString ( $text [ 1 ])][] = $line ;
}
}
} else {
2007-10-22 05:52:20 +00:00
$this -> __markerError ( $this -> __file , $line , $functionName , $count );
2007-03-29 16:53:10 +00:00
}
}
$count ++ ;
}
}
2007-10-22 05:52:20 +00:00
/**
* Build the translate template file contents out of obtained strings
*
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __buildFiles () {
$output = '' ;
2007-06-20 06:15:35 +00:00
foreach ( $this -> __strings as $str => $fileInfo ) {
2007-03-29 16:53:10 +00:00
$occured = $fileList = array ();
2007-06-20 06:15:35 +00:00
if ( $this -> __oneFile === true ) {
foreach ( $fileInfo as $file => $lines ) {
2007-09-18 03:34:44 +00:00
$occured [] = " $file : " . join ( ';' , $lines );
2007-03-29 16:53:10 +00:00
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> __fileVersions [ $file ])) {
2007-03-29 16:53:10 +00:00
$fileList [] = $this -> __fileVersions [ $file ];
}
}
$occurances = join ( " \n #: " , $occured );
$occurances = str_replace ( $this -> path , '' , $occurances );
$output = " #: $occurances\n " ;
$filename = $this -> __filename ;
2007-06-20 06:15:35 +00:00
if ( strpos ( $str , " \0 " ) === false ) {
2007-03-29 16:53:10 +00:00
$output .= " msgid \" $str\ " \n " ;
$output .= " msgstr \" \" \n " ;
} else {
list ( $singular , $plural ) = explode ( " \0 " , $str );
$output .= " msgid \" $singular\ " \n " ;
$output .= " msgid_plural \" $plural\ " \n " ;
$output .= " msgstr[0] \" \" \n " ;
$output .= " msgstr[1] \" \" \n " ;
}
$output .= " \n " ;
} else {
2007-06-20 06:15:35 +00:00
foreach ( $fileInfo as $file => $lines ) {
2007-03-29 16:53:10 +00:00
$filename = $str ;
2007-09-18 03:34:44 +00:00
$occured = array ( " $str : " . join ( ';' , $lines ));
2007-03-29 16:53:10 +00:00
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> __fileVersions [ $str ])) {
2007-03-29 16:53:10 +00:00
$fileList [] = $this -> __fileVersions [ $str ];
}
$occurances = join ( " \n #: " , $occured );
$occurances = str_replace ( $this -> path , '' , $occurances );
$output .= " #: $occurances\n " ;
2007-06-20 06:15:35 +00:00
if ( strpos ( $file , " \0 " ) === false ) {
2007-03-29 16:53:10 +00:00
$output .= " msgid \" $file\ " \n " ;
$output .= " msgstr \" \" \n " ;
} else {
list ( $singular , $plural ) = explode ( " \0 " , $file );
$output .= " msgid \" $singular\ " \n " ;
$output .= " msgid_plural \" $plural\ " \n " ;
$output .= " msgstr[0] \" \" \n " ;
$output .= " msgstr[1] \" \" \n " ;
}
$output .= " \n " ;
}
}
$this -> __store ( $filename , $output , $fileList );
}
}
2007-10-22 05:52:20 +00:00
/**
* Prepare a file to be stored
*
* @ param string $file Filename
* @ param string $input What to store
* @ param array $fileList File list
2007-10-22 16:11:12 +00:00
* @ param integer $get Set to 1 to get files to store , false to set
2007-10-22 05:52:20 +00:00
* @ return mixed If $get == 1 , files to store , otherwise void
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __store ( $file = 0 , $input = 0 , $fileList = array (), $get = 0 ) {
static $storage = array ();
2007-06-20 06:15:35 +00:00
if ( ! $get ) {
if ( isset ( $storage [ $file ])) {
2007-03-29 16:53:10 +00:00
$storage [ $file ][ 1 ] = array_unique ( array_merge ( $storage [ $file ][ 1 ], $fileList ));
$storage [ $file ][] = $input ;
} else {
$storage [ $file ] = array ();
$storage [ $file ][ 0 ] = $this -> __writeHeader ();
$storage [ $file ][ 1 ] = $fileList ;
$storage [ $file ][ 2 ] = $input ;
}
} else {
return $storage ;
}
}
2007-10-22 05:52:20 +00:00
/**
* Write the files that need to be stored
*
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __writeFiles () {
$output = $this -> __store ( 0 , 0 , array (), 1 );
$output = $this -> __mergeFiles ( $output );
2007-06-20 06:15:35 +00:00
foreach ( $output as $file => $content ) {
2007-03-29 16:53:10 +00:00
$tmp = str_replace ( array ( $this -> path , '.php' , '.ctp' , '.thtml' , '.inc' , '.tpl' ), '' , $file );
$tmp = str_replace ( DS , '.' , $tmp );
$file = str_replace ( '.' , '-' , $tmp ) . '.pot' ;
$fileList = $content [ 1 ];
unset ( $content [ 1 ]);
$fileList = str_replace ( array ( $this -> path ), '' , $fileList );
2007-06-20 06:15:35 +00:00
if ( count ( $fileList ) > 1 ) {
2007-03-29 16:53:10 +00:00
$fileList = " Generated from files: \n # " . join ( " \n # " , $fileList );
2007-06-20 06:15:35 +00:00
} elseif ( count ( $fileList ) == 1 ) {
2007-09-18 03:34:44 +00:00
$fileList = 'Generated from file: ' . join ( '' , $fileList );
2007-03-29 16:53:10 +00:00
} else {
2007-09-18 03:34:44 +00:00
$fileList = 'No version information was available in the source files.' ;
2007-03-29 16:53:10 +00:00
}
2007-10-20 05:55:37 +00:00
if ( is_file ( $this -> __output . $file )) {
$response = '' ;
while ( $response == '' ) {
$response = $this -> in ( " \n \n Error: " . $file . ' already exists in this location. Overwrite?' , array ( 'y' , 'n' , 'q' ), 'n' );
if ( strtoupper ( $response ) === 'Q' ) {
$this -> out ( 'Extract Aborted' );
exit ();
} elseif ( strtoupper ( $response ) === 'N' ) {
$response = '' ;
while ( $response == '' ) {
$response = $this -> in ( " What would you like to name this file? \n Example: new_ " . $file , null , " new_ " . $file );
$file = $response ;
}
}
}
}
2007-03-29 16:53:10 +00:00
$fp = fopen ( $this -> __output . $file , 'w' );
2007-09-18 03:34:44 +00:00
fwrite ( $fp , str_replace ( '--VERSIONS--' , $fileList , join ( '' , $content )));
2007-03-29 16:53:10 +00:00
fclose ( $fp );
}
}
2007-10-22 05:52:20 +00:00
/**
* Merge output files
*
* @ param array $output Output to merge
* @ return array Merged output
* @ access private
*/
2007-06-20 07:51:52 +00:00
function __mergeFiles ( $output ) {
2007-06-20 06:15:35 +00:00
foreach ( $output as $file => $content ) {
if ( count ( $content ) <= 1 && $file != $this -> __filename ) {
2007-03-29 16:53:10 +00:00
@ $output [ $this -> __filename ][ 1 ] = array_unique ( array_merge ( $output [ $this -> __filename ][ 1 ], $content [ 1 ]));
2007-01-01 22:03:15 +00:00
2007-06-20 06:15:35 +00:00
if ( ! isset ( $output [ $this -> __filename ][ 0 ])) {
2007-03-29 16:53:10 +00:00
$output [ $this -> __filename ][ 0 ] = $content [ 0 ];
}
unset ( $content [ 0 ]);
unset ( $content [ 1 ]);
2007-06-20 06:15:35 +00:00
foreach ( $content as $msgid ) {
2007-03-29 16:53:10 +00:00
$output [ $this -> __filename ][] = $msgid ;
}
unset ( $output [ $file ]);
}
}
return $output ;
}
2007-10-22 05:52:20 +00:00
/**
* Build the translation template header
*
* @ return string Translation template header
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __writeHeader () {
$output = " # LANGUAGE translation of CakePHP Application \n " ;
$output .= " # Copyright YEAR NAME <EMAIL@ADDRESS> \n " ;
$output .= " # --VERSIONS-- \n " ;
$output .= " # \n " ;
$output .= " #, fuzzy \n " ;
$output .= " msgid \" \" \n " ;
$output .= " msgstr \" \" \n " ;
$output .= " \" Project-Id-Version: PROJECT VERSION \\ n \" \n " ;
$output .= " \" POT-Creation-Date: " . date ( " Y-m-d H:iO " ) . " \\ n \" \n " ;
$output .= " \" PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ \\ n \" \n " ;
$output .= " \" Last-Translator: NAME <EMAIL@ADDRESS> \\ n \" \n " ;
$output .= " \" Language-Team: LANGUAGE <EMAIL@ADDRESS> \\ n \" \n " ;
$output .= " \" MIME-Version: 1.0 \\ n \" \n " ;
$output .= " \" Content-Type: text/plain; charset=utf-8 \\ n \" \n " ;
$output .= " \" Content-Transfer-Encoding: 8bit \\ n \" \n " ;
$output .= " \" Plural-Forms: nplurals=INTEGER; plural=EXPRESSION; \\ n \" \n \n " ;
return $output ;
}
2007-10-22 05:52:20 +00:00
/**
* Find the version number of a file looking for SVN commands
*
* @ param string $code Source code of file
* @ param string $file File
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __findVersion ( $code , $file ) {
2007-03-29 17:01:07 +00:00
$header = '$Id' . ':' ;
2007-05-13 23:20:47 +00:00
if ( preg_match ( '/\\' . $header . ' [\\w.]* ([\\d]*)/' , $code , $versionInfo )) {
2007-03-29 16:53:10 +00:00
$version = str_replace ( ROOT , '' , 'Revision: ' . $versionInfo [ 1 ] . ' ' . $file );
$this -> __fileVersions [ $file ] = $version ;
}
}
2007-10-22 05:52:20 +00:00
/**
* Format a string to be added as a translateable string
*
* @ param string $string String to format
* @ return string Formatted string
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __formatString ( $string ) {
2007-05-13 23:20:47 +00:00
$quote = substr ( $string , 0 , 1 );
2007-03-29 16:53:10 +00:00
$string = substr ( $string , 1 , - 1 );
2007-06-20 06:15:35 +00:00
if ( $quote == '"' ) {
2007-03-29 16:53:10 +00:00
$string = stripcslashes ( $string );
} else {
$string = strtr ( $string , array ( " \\ ' " => " ' " , " \\ \\ " => " \\ " ));
}
return addcslashes ( $string , " \0 .. \37 \\ \" " );
}
2007-10-22 05:52:20 +00:00
/**
* Indicate an invalid marker on a processed file
*
* @ param string $file File where invalid marker resides
2007-10-22 16:11:12 +00:00
* @ param integer $line Line number
2007-10-22 05:52:20 +00:00
* @ param string $marker Marker found
2007-10-22 16:11:12 +00:00
* @ param integer $count Count
2007-10-22 05:52:20 +00:00
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __markerError ( $file , $line , $marker , $count ) {
2007-05-13 23:20:47 +00:00
$this -> out ( " Invalid marker content in $file : $line\n * $marker ( " , true );
2007-03-29 16:53:10 +00:00
$count += 2 ;
$tokenCount = count ( $this -> __tokens );
$parenthesis = 1 ;
2007-06-20 06:15:35 +00:00
while ((( $tokenCount - $count ) > 0 ) && $parenthesis ) {
if ( is_array ( $this -> __tokens [ $count ])) {
2007-05-13 23:20:47 +00:00
$this -> out ( $this -> __tokens [ $count ][ 1 ], false );
2007-03-29 16:53:10 +00:00
} else {
2007-05-13 23:20:47 +00:00
$this -> out ( $this -> __tokens [ $count ], false );
2007-09-18 03:34:44 +00:00
if ( $this -> __tokens [ $count ] == '(' ) {
2007-03-29 16:53:10 +00:00
$parenthesis ++ ;
}
2007-09-18 03:34:44 +00:00
if ( $this -> __tokens [ $count ] == ')' ) {
2007-03-29 16:53:10 +00:00
$parenthesis -- ;
}
}
$count ++ ;
}
2007-05-13 23:20:47 +00:00
$this -> out ( " \n " , true );
2007-03-29 16:53:10 +00:00
}
2007-10-22 05:52:20 +00:00
/**
* Search the specified path for files that may contain translateable strings
*
* @ param string $path Path ( or set to null to use current )
* @ return array Files
* @ access private
*/
2007-03-29 16:53:10 +00:00
function __searchDirectory ( $path = null ) {
2007-06-20 07:51:52 +00:00
if ( $path === null ) {
2007-05-13 23:20:47 +00:00
$path = $this -> path . DS ;
2007-03-29 16:53:10 +00:00
}
$files = glob ( " $path *. { php,ctp,thtml,inc,tpl} " , GLOB_BRACE );
$dirs = glob ( " $path * " , GLOB_ONLYDIR );
2007-06-20 06:15:35 +00:00
foreach ( $dirs as $dir ) {
if ( ! preg_match ( " !(^|.+/)(CVS|.svn) $ ! " , $dir )) {
2007-05-13 23:20:47 +00:00
$files = array_merge ( $files , $this -> __searchDirectory ( " $dir " . DS ));
2007-06-20 06:15:35 +00:00
if (( $id = array_search ( $dir . DS . 'extract.php' , $files )) !== FALSE ) {
2007-03-29 16:53:10 +00:00
unset ( $files [ $id ]);
}
}
}
return $files ;
}
2007-01-01 22:03:15 +00:00
}
?>