2008-10-16 22:42:41 +02:00
< ? php
/**
* @ author Bernat Foj Capell < bernat @ silverstripe . com >
2008-10-17 17:21:33 +02:00
* @ author Ingo Schommer < FIRSTNAME @ silverstripe . com >
2008-10-16 22:42:41 +02:00
* @ package sapphire
* @ subpackage misc
*/
class i18nTextCollector extends Object {
protected $defaultLocale ;
2008-10-17 17:21:33 +02:00
/**
* @ var string $basePath The directory base on which the collector should act .
* Usually the webroot set through { @ link Director :: baseFolder ()} .
* @ todo Fully support changing of basePath through { @ link SSViewer } and { @ link ManifestBuilder }
*/
public $basePath ;
/**
* @ var string $basePath The directory base on which the collector should create new lang folders and files .
* Usually the webroot set through { @ link Director :: baseFolder ()} .
* Can be overwritten for testing or export purposes .
* @ todo Fully support changing of basePath through { @ link SSViewer } and { @ link ManifestBuilder }
*/
public $baseSavePath ;
2008-10-16 22:42:41 +02:00
/**
* @ param $locale
*/
function __construct ( $locale = null ) {
$this -> defaultLocale = ( $locale ) ? $locale : i18n :: default_locale ();
2008-10-17 17:21:33 +02:00
$this -> basePath = Director :: baseFolder ();
$this -> baseSavePath = Director :: baseFolder ();
2008-10-16 22:42:41 +02:00
parent :: __construct ();
}
/**
* This is the main method to build the master string tables with the original strings .
* It will search for existent modules that use the i18n feature , parse the _t () calls
* and write the resultant files in the lang folder of each module .
*
* @ uses DataObject -> collectI18nStatics ()
*/
2008-10-17 17:21:33 +02:00
public function run ( $restrictToModule = null ) {
Debug :: message ( " Collecting text... " , false );
2008-10-16 22:42:41 +02:00
// A master string tables array (one mst per module)
2008-10-17 17:21:33 +02:00
$entitiesByModule = array ();
2008-10-16 22:42:41 +02:00
//Search for and process existent modules, or use the passed one instead
2008-10-17 17:21:33 +02:00
$modules = ( isset ( $restrictToModule )) ? array ( basename ( $restrictToModule )) : scandir ( $this -> basePath );
foreach ( $modules as $module ) {
// Only search for calls in folder with a _config.php file (which means they are modules)
$isValidModuleFolder = (
is_dir ( " $this->basePath / $module " )
&& is_file ( " $this->basePath / $module /_config.php " )
&& substr ( $module , 0 , 1 ) != '.'
);
if ( ! $isValidModuleFolder ) continue ;
// we store the master string tables
$entitiesByModule [ $module ] = $this -> processModule ( $module );
2008-10-16 22:42:41 +02:00
}
// Write the generated master string tables
2008-10-17 17:21:33 +02:00
$this -> writeMasterStringFile ( $entitiesByModule );
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
Debug :: message ( " Done! " , false );
2008-10-16 22:42:41 +02:00
}
/**
* Build the module ' s master string table
*
* @ param string $module Module ' s name
*/
2008-10-17 17:21:33 +02:00
protected function processModule ( $module ) {
$entitiesArr = array ();
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
Debug :: message ( " Processing Module ' { $module } ' " , false );
// Search for calls in code files if these exists
if ( is_dir ( " $this->basePath / $module /code " )) {
$fileList = $this -> getFilesRecursive ( " $this->basePath / $module /code " );
2008-10-17 19:44:14 +02:00
} else if ( $module == 'sapphire' ) {
2008-10-17 17:21:33 +02:00
// sapphire doesn't have the usual module structure, so we'll scan all subfolders
$fileList = $this -> getFilesRecursive ( " $this->basePath / $module " );
}
foreach ( $fileList as $filePath ) {
// exclude ss-templates, they're scanned separately
if ( substr ( $filePath , - 3 ) == 'php' ) {
$content = file_get_contents ( $filePath );
$entitiesArr = array_merge ( $entitiesArr ,( array ) $this -> collectFromCode ( $content , $module ));
2008-10-17 19:44:14 +02:00
$entitiesArr = array_merge ( $entitiesArr , ( array ) $this -> collectFromStatics ( $filePath , $module ));
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
}
// Search for calls in template files if these exists
if ( is_dir ( " $this->basePath / $module /templates " )) {
$fileList = $this -> getFilesRecursive ( " $this->basePath / $module /templates " );
foreach ( $fileList as $index => $filePath ) {
$content = file_get_contents ( $filePath );
// templates use their filename as a namespace
$namespace = basename ( $filePath );
$entitiesArr = array_merge ( $entitiesArr , ( array ) $this -> collectFromTemplate ( $content , $module , $namespace ));
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
}
// sort for easier lookup and comparison with translated files
2008-10-17 19:44:14 +02:00
ksort ( $entitiesArr );
2008-10-17 17:21:33 +02:00
return $entitiesArr ;
2008-10-16 22:42:41 +02:00
}
/**
* Write the master string table of every processed module
*/
2008-10-17 17:21:33 +02:00
protected function writeMasterStringFile ( $entitiesByModule ) {
2008-10-16 22:42:41 +02:00
// Write each module language file
2008-10-17 17:21:33 +02:00
if ( $entitiesByModule ) foreach ( $entitiesByModule as $module => $entities ) {
2008-10-17 19:44:14 +02:00
$php = '' ;
2008-10-16 22:42:41 +02:00
// Create folder for lang files
2008-10-17 17:21:33 +02:00
$langFolder = $this -> baseSavePath . '/' . $module . '/lang' ;
2008-10-17 19:44:14 +02:00
if ( ! file_exists ( $langFolder )) {
2008-10-17 17:21:33 +02:00
Filesystem :: makeFolder ( $langFolder , Filesystem :: $folder_create_mask );
2008-10-17 19:44:14 +02:00
touch ( $langFolder . '/_manifest_exclude' );
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
2008-10-16 22:42:41 +02:00
// Open the English file and write the Master String Table
if ( $fh = fopen ( $langFolder . '/' . $this -> defaultLocale . '.php' , " w " )) {
2008-10-17 17:21:33 +02:00
if ( $entities ) foreach ( $entities as $fullName => $spec ) {
$php .= $this -> langArrayCodeForEntitySpec ( $fullName , $spec );
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
// test for valid PHP syntax by eval'ing it
try {
//eval($php);
} catch ( Exception $e ) {
user_error ( 'i18nTextCollector->writeMasterStringFile(): Invalid PHP language file. Error: ' . $e -> toString (), E_USER_ERROR );
}
fwrite ( $fh , " <?php \n \n global \$ lang; \n \n " . $php . " \n ?> " );
fclose ( $fh );
Debug :: message ( " Created file: $langFolder / " . $this -> defaultLocale . " .php " , false );
2008-10-16 22:42:41 +02:00
} else {
user_error ( " Cannot write language file! Please check permissions of $langFolder / " . $this -> defaultLocale . " .php " , E_USER_ERROR );
}
}
}
/**
* Helper function that searches for potential files to be parsed
*
* @ param string $folder base directory to scan ( will scan recursively )
* @ param array $fileList Array where potential files will be added to
*/
protected function getFilesRecursive ( $folder , & $fileList = null ) {
if ( ! $fileList ) $fileList = array ();
$items = scandir ( $folder );
2008-10-17 19:44:14 +02:00
$isValidFolder = (
! in_array ( '_manifest_exclude' , $items )
&& ! preg_match ( '/\/tests$/' , $folder )
);
if ( $items && $isValidFolder ) foreach ( $items as $item ) {
2008-10-16 22:42:41 +02:00
if ( substr ( $item , 0 , 1 ) == '.' ) continue ;
if ( substr ( $item , - 4 ) == '.php' ) $fileList [ substr ( $item , 0 , - 4 )] = " $folder / $item " ;
else if ( substr ( $item , - 3 ) == '.ss' ) $fileList [ $item ] = " $folder / $item " ;
else if ( is_dir ( " $folder / $item " )) $this -> getFilesRecursive ( " $folder / $item " , $fileList );
}
return $fileList ;
}
2008-10-17 17:21:33 +02:00
public function collectFromCode ( $content , $module ) {
$entitiesArr = array ();
$regexRule = '_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]*,' . # value
'[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment
'\)' ;
while ( ereg ( $regexRule , $content , $regs )) {
$entitiesArr = array_merge ( $entitiesArr , ( array ) $this -> entitySpecFromRegexMatches ( $regs ));
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
// remove parsed content to continue while() loop
2008-10-16 22:42:41 +02:00
$content = str_replace ( $regs [ 0 ], " " , $content );
}
2008-10-17 19:44:14 +02:00
ksort ( $entitiesArr );
2008-10-17 17:21:33 +02:00
return $entitiesArr ;
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
public function collectFromTemplate ( $content , $module , $fileName ) {
$entitiesArr = array ();
2008-10-16 22:42:41 +02:00
// Search for included templates
2008-10-17 17:21:33 +02:00
preg_match_all ( '/<' . '% include +([A-Za-z0-9_]+) +%' . '>/' , $content , $regs , PREG_SET_ORDER );
foreach ( $regs as $reg ) {
$includeName = $reg [ 1 ];
$includeFileName = " { $includeName } .ss " ;
$filePath = SSViewer :: getTemplateFileByType ( $includeName , 'Includes' );
$includeContent = file_get_contents ( $filePath );
// @todo Will get massively confused if you include the includer -> infinite loop
$entitiesArr = array_merge ( $entitiesArr ,( array ) $this -> collectFromTemplate ( $includeContent , $module , $includeFileName ));
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
// @todo respect template tags (<% _t() %> instead of _t())
$regexRule = '_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]]*,' . # value
'[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment (optional)
'\)' ;
while ( ereg ( $regexRule , $content , $regs )) {
$entitiesArr = array_merge ( $entitiesArr ,( array ) $this -> entitySpecFromRegexMatches ( $regs , $fileName ));
// remove parsed content to continue while() loop
$content = str_replace ( $regs [ 0 ], " " , $content );
}
2008-10-17 19:44:14 +02:00
ksort ( $entitiesArr );
2008-10-17 17:21:33 +02:00
return $entitiesArr ;
}
/**
* @ todo Fix regexes so the deletion of quotes , commas and newlines from wrong matches isn ' t necessary
*/
protected function entitySpecFromRegexMatches ( $regs , $_namespace = null ) {
// remove wrapping quotes
$fullName = substr ( $regs [ 1 ], 1 , - 1 );
// split fullname into entity parts
$entityParts = explode ( '.' , $fullName );
if ( count ( $entityParts ) > 1 ) {
// templates don't have a custom namespace
2008-10-16 22:42:41 +02:00
$entity = array_pop ( $entityParts );
2008-10-17 17:21:33 +02:00
// namespace might contain dots, so we explode
$namespace = implode ( '.' , $entityParts );
} else {
$entity = array_pop ( $entityParts );
$namespace = $_namespace ;
}
// remove wrapping quotes
$value = ( $regs [ 2 ]) ? substr ( $regs [ 2 ], 1 , - 1 ) : null ;
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
// only escape quotes when wrapped in double quotes, to make them safe for insertion
// into single-quoted PHP code. If they're wrapped in single quotes, the string should
// be properly escaped already
if ( substr ( $regs [ 2 ], 0 , 1 ) == '"' ) $value = addcslashes ( $value , '\'' );
// remove starting comma and any newlines
$prio = ( $regs [ 5 ]) ? trim ( preg_replace ( '/\n/' , '' , substr ( $regs [ 5 ], 1 ))) : null ;
// remove wrapping quotes
$comment = ( $regs [ 7 ]) ? substr ( $regs [ 7 ], 1 , - 1 ) : null ;
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
return array (
" { $namespace } . { $entity } " => array (
$value ,
$prio ,
$comment
)
);
}
/**
* Input for langArrayCodeForEntitySpec () should be suitable for insertion
* into single - quoted strings , so needs to be escaped already .
*
* @ param string $entity The entity name , e . g . CMSMain . BUTTONSAVE
*/
public function langArrayCodeForEntitySpec ( $entityFullName , $entitySpec ) {
$php = '' ;
$entityParts = explode ( '.' , $entityFullName );
if ( count ( $entityParts ) > 1 ) {
// templates don't have a custom namespace
$entity = array_pop ( $entityParts );
// namespace might contain dots, so we implode back
$namespace = implode ( '.' , $entityParts );
} else {
user_error ( " i18nTextCollector::langArrayCodeForEntitySpec(): Wrong entity format for $entityFullName with values " . var_export ( $entitySpec , true ), E_USER_WARNING );
return false ;
}
$value = $entitySpec [ 0 ];
$prio = ( isset ( $entitySpec [ 1 ])) ? addcslashes ( $entitySpec [ 1 ], '\'' ) : null ;
$comment = ( isset ( $entitySpec [ 2 ])) ? addcslashes ( $entitySpec [ 2 ], '\'' ) : null ;
$php .= '$lang[\'' . $this -> defaultLocale . '\'][\'' . $namespace . '\'][\'' . $entity . '\'] = ' ;
if ( $prio ) {
$php .= " array( \n \t ' " . $value . " ', \n \t " . $prio ;
if ( $comment ) {
$php .= " , \n \t ' " . $comment . '\'' ;
}
$php .= " \n ); " ;
} else {
$php .= '\'' . $value . '\';' ;
2008-10-16 22:42:41 +02:00
}
2008-10-17 17:21:33 +02:00
$php .= " \n " ;
return $php ;
}
protected function collectFromStatics ( $filePath ) {
$entitiesArr = array ();
2008-10-16 22:42:41 +02:00
2008-10-17 17:21:33 +02:00
$classes = ClassInfo :: classes_for_file ( $filePath );
if ( $classes ) foreach ( $classes as $class ) {
if ( class_exists ( $class ) && method_exists ( $class , 'provideI18nStatics' )) {
$obj = singleton ( $class );
$entitiesArr = array_merge ( $entitiesArr ,( array ) $obj -> provideI18nStatics ());
}
}
2008-10-17 19:44:14 +02:00
ksort ( $entitiesArr );
2008-10-17 17:21:33 +02:00
return $entitiesArr ;
}
public function getDefaultLocale () {
return $this -> defaultLocale ;
}
public function setDefaultLocale ( $locale ) {
$this -> defaultLocale = $locale ;
2008-10-16 22:42:41 +02:00
}
}
?>