Word1 | Word2 | Translation |
'.$ret.'
';
function readlangfile( $lang, &$header)
{
global $CFG;
$file = $CFG->dirroot.'/mod/game/lang/'.$lang.'/game.php';
$a = array();
$lines = file( $file);
$header = '';
$endofheader = false;
foreach( $lines as $line)
{
if( $endofheader == false)
{
if( strpos( $line, '//') === false)
$endofheader = true;
else
$header .= $line;
}
if( splitlangdefinition($line,$name,$trans))
$a[ $name] = $trans;
}
return $a;
}
function splitlangdefinition($line,&$name,&$trans)
{
$pos1 = strpos( $line, '=');
if( $pos1 == 0)
return false;
$pos2 = strpos( $line, '//');
if( $pos2 != 0 or substr( $line, 0, 2) == '//')
{
if( $pos2 < $pos1)
return false; //Commented line
}
$name = trim(substr( $line, 0, $pos1-1));
$trans = trim(substr( $line, $pos1+1));
$pos = strpos( $name, '\'');
if( $pos)
{
$name = substr( $name, $pos+1);
$pos = strrpos( $name, '\'');
$name = substr( $name, 0, $pos);
}
return true;
}
function readsourcecode( $file, &$strings)
{
global $CFG;
$lines = file( $file);
foreach( $lines as $line)
{
parseline( &$strings, $line, $file);
}
return $strings;
}
function parseline( &$strings, $line, $filename)
{
global $CFG;
$filename = substr( $filename, strlen( $CFG->dirroot.'/mod/game/'));
if( strpos($filename, '/'))
$filename = '/'.$filename;
$pos0 = 0;
for(;;)
{
$pos = strpos( $line, 'get_string', $pos0);
if( $pos == false)
$pos = strpos( $line, 'print_string', $pos0);
if( $pos === false)
break;
$pos1 = strpos( $line, '(', $pos);
$pos2 = strpos( $line, ',', $pos);
$pos3 = strpos( $line, ')', $pos);
if( $pos1 == 0 or $pos2 == 0 or $pos3 == 0)
{
$pos0 = $pos+1;
continue;
}
$name = gets( substr( $line, $pos1+1, $pos2-$pos1-1));
$file = gets( substr( $line, $pos2+1, $pos3-$pos2-1));
if( $file == 'game')
{
if( !array_key_exists( $name, $strings))
$strings[ $name] = $filename.' * '.$name;
}else
{
$pos4 = strpos($file, '\'');
if( $pos4)
$file = substr( $file, 0, $pos4);
$pos4 = strpos($file, '"');
if($pos4)
$file = substr( $file, 0, $pos4);
if( $file == 'game')
{
if( !array_key_exists( $name, $strings))
$strings[ $name] = $filename.' * '.$name;
}
}
$pos0 = $pos+1;
}
}
function gets( $s)
{
$s = trim( $s);
if( substr( $s, 0, 1) == '"')
$s = substr( $s, 1, -1);
if( substr( $s, 0, 1) == '\'')
$s = substr( $s, 1, -1);
return $s;
}
function read_dir($dir, $ext) {
if( $ext != '')
$ext = '.' .$ext;
$len = strlen( $ext);
$a = array( $dir);
$ret = array();
while( count( $a)){
$dir = array_pop( $a);
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if($entry!='.' && $entry!='..') {
$entry = $dir.'/'.$entry;
if(is_dir($entry)) {
$a[] = $entry;
} else {
if( $len == 0)
$ret[] = $entry;
else if( substr( $entry, -$len) == $ext)
$ret[] = $entry;
}
}
}
$d->close();
}
return $ret;
}
function computelangs()
{
global $CFG;
$dir = $CFG->dirroot.'/mod/game/lang';
$ret = array();
$d = dir( $dir);
while (false !== ($entry = $d->read())) {
if( $entry != '.' and $entry != '..'){
if(is_dir($dir.'/'.$entry)) {
$ret[] = $entry;
}
}
}
return $ret;
}
function computediff( $en, $lang, $strings, $langname, &$sum, $outdir, &$untranslated)
{
global $CFG;
$untranslated = '';
$counten=count($en);
$trans = readlangfile( $lang, $header);
foreach( $trans as $s => $key)
{
unset( $en[ $s]);
}
unset( $en[ 'convertfrom']);
unset( $en[ 'convertto']);
$file = $CFG->dirroot.'/mod/game/lang/'.$lang.'/game.php';
$lines = file( $file);
$count = 0;
$s = '';
foreach( $lines as $line)
{
$s .= $line;
if( ++$count >= 3)
break;
}
$a = array();
foreach( $en as $name => $t)
{
if( array_key_exists( $name, $strings))
$file = $strings[ $name];
else
$file = '';
$t = strip_tags( $t);
$a[ $file.' * '.$name] = '$'."string[ '$name'] = $t\r\n";
}
ksort( $a);
if( substr( $lang, -5) == '_utf8')
$lang = substr( $lang, 0, -5);
if( array_key_exists( $lang, $langname))
$langprint = $langname[ $lang];
else
$langprint = $lang;
$sum[]="$langprint | ".count($a)." | ".round(100*($counten-count($a))/$counten,0)." % | ";
$prev_file = '';
foreach( $a as $key => $line)
{
$pos = strpos( $key, '*');
$file = trim( substr( $key, 0, $pos-1));
if( substr( $file, 0, 1) == '/')
$file = substr( $file, 1);
if( $file != $prev_file)
{
$s .= "\r\n//$file\r\n";
$prev_file = $file;
}
$s .= $line;
$untranslated .= "//$prev_file ".$line;
}
$file = $outdir.'/'.$lang.'.php';
file_put_contents( $file, $s);
}