Created
January 2, 2012 14:35
-
-
Save ryo88c/1550905 to your computer and use it in GitHub Desktop.
Generated translation resources from the Smarty tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/php -qn | |
<?php | |
/** | |
* Generated translation resources from the Smarty tags | |
* Tag: <!--t "STRING"--> | |
* | |
* @author Ryo HAYASHI <ryo88c at gmail.com> | |
*/ | |
class tpl2php{ | |
/** @var string Target base directory */ | |
private $_baseDir = '../views'; | |
/** Permit extensions */ | |
private $_extensions = array('tpl'); | |
/** @var array Translate strings */ | |
private $_translates = array(); | |
/** | |
* Export translate strings | |
* | |
* @param string $file Target file | |
* @return void | |
*/ | |
private function exportTranslateStrings($file){ | |
$content = file_get_contents($file); | |
if (empty($content)) { | |
return; | |
} | |
if(preg_match_all('/<!--t "([^"]+)" "([^"]+)"-->/', $content, $matches, PREG_SET_ORDER)){ | |
foreach($matches as $match){ | |
$this->_translates[] = sprintf("__('%s', '%s');", $match[1], $match[2]); | |
} | |
} | |
if(preg_match_all('/<!--t "([^"]+)"-->/', $content, $matches, PREG_SET_ORDER)){ | |
foreach($matches as $match){ | |
$this->_translates[] = sprintf("__('%s');", $match[1]); | |
} | |
} | |
} | |
/** | |
* Recursive dig for directory | |
* | |
* @param string $dir Base dir | |
* @return void | |
*/ | |
private function recursiveDig4Dir($dir){ | |
$d = dir($dir); | |
while(false !== ($entry = $d->read())){ | |
if($entry == '.' || $entry == '..'){ | |
continue; | |
} | |
$entry = $dir.'/'.$entry; | |
if(is_dir($entry)){ | |
$this->recursiveDig4Dir($entry); | |
}else{ | |
$pi = pathinfo($entry); | |
if(is_readable($entry) && isset($pi['extension']) && in_array($pi['extension'], $this->_extensions)){ | |
$this->exportTranslateStrings($entry); | |
} | |
} | |
} | |
$d->close(); | |
} | |
/** | |
* Constructor | |
*/ | |
function __construct(){ | |
$this->recursiveDig4Dir($this->_baseDir); | |
file_put_contents('../Language/tpl.php', sprintf("<?php\n// Genelated: %s\n%s", date('Y-m-d H:i:s'), implode("\n", $this->_translates))); | |
} | |
} | |
new tpl2php; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment