You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
12 KiB

<?php // $Id: translate.php,v 1.1.2.6 2012/01/16 23:12:48 bdaloukas Exp $
/**
* Check translation of module Game
*
* @author
* @version $Id: translate.php,v 1.1.2.6 2012/01/16 23:12:48 bdaloukas Exp $
* @package game
**/
require( "../../config.php");
require( 'locallib.php');
?>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
if( !isteacher( $COURSE->id, $USER->id)){
error( get_string( 'only_teachers', 'game'));
}
$langname = array();
$langname['ca'] = 'Català (ca)';
$langname['de'] = 'Deutsch (de)';
$langname['el'] = 'Ελληνικά (el)';
$langname['en'] = 'English (de)';
$langname['es'] = 'Español - Internacional (es)';
$langname['eu'] = 'Euskara (eu)';
$langname['fr'] = 'Français (fr)';
$langname['he'] = 'ית (he';
$langname['hr'] = 'Hrvatski (hr)';
$langname['it'] = 'Italiano (it)';
$langname['lt'] = 'Lietuviškai (lt)';
$langname['nl'] = 'Nederlands (nl)';
$langname['no'] = 'Norsk - bokmål (no)';
$langname['pl'] = 'Polski (pl)';
$langname['ru'] = 'Русский (ru)';
$langname['sq'] = 'Shqip (sq)';
$langname['uk'] = 'Українська (uk)';
$langname['pt_br'] = 'Português - Brasil (pt_br)';
$langname['zh_cn'] = '简体中文 (zh_cn)';
$a = read_dir( $CFG->dirroot.'/mod/game','php');
$en = readlangfile( 'en_utf8', $header);
$strings = array();
$files = array();
foreach( $a as $file)
$files[] = $file;
$files[] = $CFG->dirroot.'/mod/game/mod.html';
sort( $files);
foreach( $files as $file)
{
readsourcecode( $file, $strings);
}
$strings[ 'game:attempt'] = '/db/access.php * game:attempt';
$strings[ 'game:deleteattempts'] = '/db/access.php * game:deleteattempts';
$strings[ 'game:grade'] = '/db/access.php * game:grade';
$strings[ 'game:manage'] = '/db/access.php * game:manage';
$strings[ 'game:preview'] = '/db/access.php * game:preview';
$strings[ 'game:reviewmyattempts'] = '/db/access.php * game:reviewmyattempts';
$strings[ 'game:view'] = '/db/access.php * game:view';
$strings[ 'game:viewreports'] = '/db/access.php * game:viewreports';
$langs = computelangs();
sort( $langs);
$sum = array();
$destdir = game_export_createtempdir();
foreach( $langs as $lang)
{
if($lang != 'en_utf8' and $lang != 'CVS' and $lang != 'ru_uft8')
{
computediff( $en, $lang, $strings, $langname, $sum, $destdir, $untranslated);
$auntranslated[ $lang] = $untranslated;
}
}
$file_no_translation = 'game_lang_no_translation.zip';
$filezip=game_create_zip( $destdir, $COURSE->id, $file_no_translation);
remove_dir( $destdir);
echo '<table border=1>';
echo "<tr><td><b>Counter</td><td><b>Language</td><td><b>Missing words</td><td><b>Percent completed</td></tr>";
$ilang=0;
foreach($sum as $s)
echo '<tr><td>'.(++$ilang).'</td>'.$s."\r\n";
echo "</table>";
echo "<br><a href=\"{$CFG->wwwroot}/file.php/1/export/$file_no_translation\">Words that is not translated yet in each language</a>";
//Find missing strings on en/game.php
$not = array();
$prevfile = '';
foreach( $strings as $info)
{
$pos = strpos( $info, '*');
$name = substr( $info, $pos+2);
$file = substr( $info, 0, $pos-1);
if( substr( $file, 0, 1) == '/')
$file = substr( $file, 1);
if( $file != $prevfile)
{
$prevfile = $file;
}
if( !array_key_exists( $name, $en))
$not[ $info] = $info;
}
$oldfile='';
unset( $not[ 'tabs.php * $report']);
unset( $not[ 'mod_form.php * game_\'.$gamekin']);
unset( $not[ 'mod.html * game_\'.$form->gamekin']);
unset( $not[ '/report/overview/report.php * fullname\')."\t".get_string(\'startedon']);
if( count( $not))
echo "<br><br>Missing strings on en_utf8/game.php<br>";
foreach( $not as $key => $value)
{
$pos = strpos( $value, '*');
$file = trim( substr( $value, 0, $pos));
$key = trim( substr( $value, $pos+1));
if( substr( $file, 0, 1) == '/')
$file = substr( $file, 1);
if( $file != $oldfile)
{
echo "<br>//$file<br>";
$fileold=$file;
}
echo '$'."string[ '$key'] = \"\";<br>";
}
//Finds translations to en that are not used now
$ret = '';
foreach( $en as $key => $value)
{
if( !array_key_exists( $key, $strings))
$ret .= "$key = $value<br>";
}
if( $ret != '')
echo '<hr><b><center>Translations that are not used</center></b><br>'.$ret;
//Creates the zip files of translations
$destdir = game_export_createtempdir();
$strings_en = readlangfile( 'en_utf8', $header);
sort( $strings);
foreach( $langname as $lang => $name)
{
$strings_lang = readlangfile( $lang.'_utf8', $header);
$ret = '';
foreach( $strings_lang as $key => $value)
{
if( !array_key_exists( $key, $strings_en))
{
$ret .= '<br>'.$key."\r\n";
}
}
if( $ret != '')
echo '<hr><b><center>Unused translation for lang '.$lang.'</center></b><br>'.substr( $ret, 4)."\r\n";
$ret = $header;
foreach( $strings as $info)
{
$pos = strpos( $info, '*');
$name = substr( $info, $pos+2);
$file = substr( $info, 0, $pos-1);
if( substr( $file, 0, 1) == '/')
$file = substr( $file, 1);
if( $file != $prevfile)
{
$prevfile = $file;
$ret .= "\r\n//".$file."\r\n";
}
if( array_key_exists( $name, $strings_lang))
$ret .= '$string'."[ '$name'] = ".$strings_lang[ $name]."\r\n";
}
if( $lang != 'en')
{
$untranslated = $auntranslated[ $lang.'_utf8'];
if( $untranslated != '')
$ret .= "\r\n//Untranslated\r\n".$untranslated;
}
mkdir( $destdir.'/'.$lang.'_utf8');
$file = $destdir.'/'.$lang.'_utf8/game.php';
file_put_contents( $file, $ret);
}
$file_sorted = 'game_lang_sorted.zip';
$filezip=game_create_zip( $destdir, $COURSE->id, $file_sorted);
remove_dir( $destdir);
echo "<br><a href=\"{$CFG->wwwroot}/file.php/1/export/$file_sorted\">Sorted translation files</a>";
asort( $en);
$sprev = '';
$keyprev = '';
$ret = '';
foreach( $en as $key => $s)
{
if( $s == $sprev)
$ret .= "<tr><td>$key</td><td>$keyprev</td><td>$s</td></tr>\r\n";
$sprev = $s;
$keyprev = $key;
}
if( $ret != '')
echo '<br><center><b>Same translations<center></b><br><table border=1><tr><td><b>Word1</td><td><b>Word2</td><td><b>Translation</td></tr>'.$ret.'</table>';
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[]="<td>$langprint</td><td><center>".count($a)."</td><td><center>".round(100*($counten-count($a))/$counten,0)." %</td>";
$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);
}