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.

489 lines
14 KiB

9 years ago
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Check translation of module Game
*
* @author
* @version $Id: translate.php,v 1.10 2012/07/25 23:07:43 bdaloukas Exp $
* @package game
**/
require( "../../config.php");
require( 'locallib.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir="ltr" lang="el" xml:lang="el" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Μάθημα: game23</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$context = game_get_context_course_instance( $COURSE->id);
9 years ago
if (!has_capability('mod/game:viewreports', $context)) {
error( get_string( 'only_teachers', 'game'));
9 years ago
}
$langname = array();
$langname['ca'] = 'Català (ca)';
$langname['de'] = 'Deutsch (de)';
$langname['el'] = 'Ελληνικά (el)';
$langname['en'] = 'English (en)';
$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['pt_br'] = 'Português - Brasil (pt_br)';
$langname['ro'] = 'Română (ro)';
$langname['ru'] = 'Русский (ru)';
$langname['sq'] = 'Shqip (sq)';
$langmane['sr_cr'] = 'Српски (sr_cr)';
$langname['uk'] = 'Українська (uk)';
$langname['zh_cn'] = '简体中文 (zh_cn)';
ksort( $langname);
9 years ago
$a = read_dir( $CFG->dirroot.'/mod/game', 'php');
$strings = array();
$files = array();
9 years ago
foreach ($a as $file) {
$files[] = $file;
9 years ago
}
sort( $files);
9 years ago
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:manageoverrides'] = '/db/access.php * game:manageoverrides';
$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';
$strings[ 'game:addinstance'] = '/db/access.php * game:viewreports';
$strings[ 'pluginname'] = 'index.php * pluginname';
$strings[ 'pluginadministration'] = 'index.php * pluginadministration';
$strings[ 'convertfrom'] = 'locallib.php * convertfrom';
$strings[ 'convertto'] = 'locallib.php * convertto';
$strings[ 'helpbookquiz'] = 'index.php * helpbookquiz';
$strings[ 'helphangman'] = 'index.php * helphangman';
$strings[ 'helpcross'] = 'index.php * helpcross';
$strings[ 'helpcryptex'] = 'index.php * helpcryptex';
$strings[ 'helpbookquiz'] = 'index.php * helpbookquiz';
$strings[ 'helpsudoku'] = 'index.php * helpsudoku';
$strings[ 'helphiddenpicture'] = 'index.php * helphiddenpicture';
$strings[ 'helpsnakes'] = 'index.php * helpsnakes';
$strings[ 'helpmillionaire'] = 'index.php * helpmillionaire';
$en = readlangfile( 'en', $header);
unset( $en[ 'convertfrom']);
unset( $en[ 'convertto']);
$langs = array_keys( $langname);
$sum = array();
$destdir = game_export_createtempdir();
$auntranslated = array();
9 years ago
foreach ($langs as $lang) {
if ($lang != 'en') {
computediff( $en, $lang, $strings, $langname, $sum, $destdir, $untranslated);
$auntranslated[ $lang] = $untranslated;
}
}
9 years ago
$filenotranslation = 'game_lang_no_translation.zip';
$filezip = game_create_zip( $destdir, $COURSE->id, $filenotranslation);
remove_dir( $destdir);
9 years ago
$ilang = 0;
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>";
9 years ago
foreach ($sum as $s) {
echo '<tr><td>'.(++$ilang).'</td>'.$s."\r\n";
}
echo "</table>";
echo "<hr><br><font color=\"red\">Words that is not translated yet in each language are in $filezip</font></b>";
9 years ago
// Find missing strings on en/game.php.
$not = array();
$prevfile = '';
9 years ago
foreach ($strings as $info) {
$pos = strpos( $info, '*');
9 years ago
$name = substr( $info, $pos + 2);
$file = substr( $info, 0, $pos - 1);
if (substr( $file, 0, 1) == '/') {
$file = substr( $file, 1);
9 years ago
}
if ($file != $prevfile) {
$prevfile = $file;
}
9 years ago
if (!array_key_exists( $name, $en)) {
$not[ $info] = $info;
9 years ago
}
}
9 years ago
$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']);
unset( $not[ '/hangman/play.php * hangman_correct_\'.$ter']);
9 years ago
if (count( $not)) {
echo "<br><br>Missing strings on en/game.php<br>";
9 years ago
}
foreach ($not as $key => $value) {
$pos = strpos( $value, '*');
$file = trim( substr( $value, 0, $pos));
9 years ago
$key = trim( substr( $value, $pos + 1));
if ($key == 'convertfrom' or $key == 'convertto') {
continue;
9 years ago
}
if (substr( $file, 0, 1) == '/') {
$file = substr( $file, 1);
9 years ago
}
if ($file != $oldfile) {
echo "<br>//$file<br>\r\n";
9 years ago
$fileold = $file;
}
echo '$'."string[ '$key'] = \"\";<br>";
}
9 years ago
// Finds translations to en that are not used now.
$ret = '';
9 years ago
foreach ($en as $key => $value) {
if (!array_key_exists( $key, $strings)) {
$ret .= "$key = $value<br>";
9 years ago
}
}
9 years ago
if ($ret != '') {
echo '<hr><b><center>Translations that are not used</center></b><br>'.$ret;
9 years ago
}
9 years ago
// Creates the zip files of translations.
$destdir = game_export_createtempdir();
sort( $strings);
9 years ago
foreach ($langname as $lang => $name) {
$stringslang = readlangfile( $lang, $header);
if ($stringslang === false) {
continue;
}
$ret = '';
9 years ago
foreach ($stringslang as $key => $value) {
if (!array_key_exists( $key, $en)) {
if ($key != 'convertfrom' and $key != 'convertto') {
$ret .= '<br>'.$key."\r\n";
9 years ago
}
}
}
9 years ago
if ($ret != '') {
echo '<hr><b><center>Unused translation for lang '.$lang.'</center></b><br>'.substr( $ret, 4)."\r\n";
9 years ago
}
$ret = $header;
9 years ago
foreach ($strings as $info) {
$pos = strpos( $info, '*');
9 years ago
$name = substr( $info, $pos + 2);
$file = substr( $info, 0, $pos - 1);
if (substr( $file, 0, 1) == '/') {
$file = substr( $file, 1);
9 years ago
}
if ($file != $prevfile) {
$prevfile = $file;
$ret .= "\r\n//".$file."\r\n";
}
9 years ago
if (array_key_exists( $name, $stringslang)) {
$ret .= '$string'."[ '$name'] = ".$stringslang[ $name]."\r\n";
}
}
9 years ago
if ($lang != 'en') {
$untranslated = $auntranslated[ $lang];
9 years ago
if ($untranslated != '') {
$ret .= "\r\n//Untranslated\r\n".$untranslated;
9 years ago
}
}
mkdir( $destdir.'/'.$lang);
$file = $destdir.'/'.$lang.'/game.php';
file_put_contents( $file, $ret);
}
9 years ago
$filesorted = 'game_lang_sorted.zip';
$filezip = game_create_zip( $destdir, $COURSE->id, $filesorted);
remove_dir( $destdir);
echo "<br><hr><b><font color=\"red\">The sorted translation files is $destdir/$filesorted</font></b>";
asort( $en);
$sprev = '';
$keyprev = '';
$ret = '';
9 years ago
foreach ($en as $key => $s) {
if ($s == $sprev) {
$ret .= "<tr><td>$key</td><td>$keyprev</td><td>$s</td></tr>\r\n";
9 years ago
}
$sprev = $s;
$keyprev = $key;
}
9 years ago
if ($ret != '') {
echo '<br><center><b>Same translations<center></b><br>';
echo '<table border=1><tr><td><b>Word1</td><td><b>Word2</td><td><b>Translation</td></tr>'.$ret.'</table>';
}
function getlangfile( $lang) {
global $CFG;
if ($lang == 'en') {
return $CFG->dirroot.'/mod/game/lang/'.$lang.'/game.php';
} else {
return $CFG->dataroot.'/lang/'.$lang.'/game.php';
}
}
function readlangfile( $lang, &$header) {
$file = getlangfile( $lang);
if (!is_file($file)) {
return false;
}
$a = array();
$lines = file( $file);
$header = '';
$endofheader = false;
9 years ago
foreach ($lines as $line) {
if ($endofheader == false) {
if (strpos( $line, '//') === false) {
$endofheader = true;
9 years ago
} else {
$header .= $line;
9 years ago
}
}
9 years ago
if (splitlangdefinition($line, $name, $trans)) {
$a[ $name] = $trans;
9 years ago
}
}
9 years ago
return $a;
}
9 years ago
function splitlangdefinition($line, &$name, &$trans) {
$pos1 = strpos( $line, '=');
9 years ago
if ($pos1 == 0) {
return false;
9 years ago
}
$pos2 = strpos( $line, '//');
9 years ago
if ($pos2 != 0 or substr( $line, 0, 2) == '//') {
if ($pos2 < $pos1) {
return false; // Commented line.
}
}
9 years ago
$name = trim(substr( $line, 0, $pos1 - 1));
$trans = trim(substr( $line, $pos1 + 1));
$pos = strpos( $name, '\'');
9 years ago
if ($pos) {
$name = substr( $name, $pos + 1);
$pos = strrpos( $name, '\'');
$name = substr( $name, 0, $pos);
}
return true;
}
9 years ago
function readsourcecode( $file, &$strings) {
global $CFG;
$lines = file( $file);
9 years ago
foreach ($lines as $line) {
parseline( $strings, $line, $file);
}
return $strings;
}
9 years ago
function parseline( &$strings, $line, $filename) {
global $CFG;
9 years ago
$filename = substr( $filename, strlen( $CFG->dirroot.'/mod/game/'));
9 years ago
if (strpos($filename, '/')) {
$filename = '/'.$filename;
9 years ago
}
$pos0 = 0;
9 years ago
for (;;) {
$pos = strpos( $line, 'get_string', $pos0);
9 years ago
if ($pos == false) {
$pos = strpos( $line, 'print_string', $pos0);
9 years ago
}
if ($pos === false) {
break;
9 years ago
}
$pos1 = strpos( $line, '(', $pos);
$pos2 = strpos( $line, ',', $pos);
$pos3 = strpos( $line, ')', $pos);
9 years ago
if ($pos1 == 0 or $pos2 == 0 or $pos3 == 0) {
$pos0 = $pos + 1;
continue;
}
9 years ago
$name = gets( substr( $line, $pos1 + 1, $pos2 - $pos1 - 1));
$file = gets( substr( $line, $pos2 + 1, $pos3 - $pos2 - 1));
9 years ago
if ($file == 'game') {
if (!array_key_exists( $name, $strings)) {
$strings[ $name] = $filename.' * '.$name;
9 years ago
}
} else {
$pos4 = strpos($file, '\'');
9 years ago
if ($pos4) {
$file = substr( $file, 0, $pos4);
9 years ago
}
$pos4 = strpos($file, '"');
9 years ago
if ($pos4) {
$file = substr( $file, 0, $pos4);
9 years ago
}
9 years ago
if ($file == 'game') {
if (!array_key_exists( $name, $strings)) {
$strings[ $name] = $filename.' * '.$name;
9 years ago
}
}
}
9 years ago
$pos0 = $pos + 1;
}
}
9 years ago
function gets( $s) {
$s = trim( $s);
9 years ago
if (substr( $s, 0, 1) == '"') {
$s = substr( $s, 1, -1);
9 years ago
}
if (substr( $s, 0, 1) == '\'') {
$s = substr( $s, 1, -1);
9 years ago
}
return $s;
}
function read_dir($dir, $ext) {
9 years ago
if ($ext != '') {
$ext = '.' .$ext;
9 years ago
}
$len = strlen( $ext);
$a = array( $dir);
$ret = array();
9 years ago
while (count( $a)) {
$dir = array_pop( $a);
9 years ago
if (strpos( $dir, '/lang/') != 0) {
continue;
9 years ago
}
$d = dir($dir);
while (false !== ($entry = $d->read())) {
9 years ago
if ($entry != '.' && $entry != '..') {
$entry = $dir.'/'.$entry;
9 years ago
if (is_dir($entry)) {
$a[] = $entry;
} else {
9 years ago
if ($len == 0) {
$ret[] = $entry;
9 years ago
} else if (substr( $entry, -$len) == $ext) {
$ret[] = $entry;
9 years ago
}
}
}
}
$d->close();
9 years ago
}
9 years ago
return $ret;
}
9 years ago
function computediff( $en, $lang, $strings, $langname, &$sum, $outdir, &$untranslated) {
global $CFG;
$untranslated = '';
9 years ago
$counten = count($en);
$trans = readlangfile( $lang, $header);
if ($trans != false) {
foreach ($trans as $s => $key) {
unset( $en[ $s]);
}
}
$file = getlangfile( $lang);
if (file_exists( $file)) {
$lines = file( $file);
} else {
$lines = array();
}
$count = 0;
$s = '';
9 years ago
foreach ($lines as $line) {
$s .= $line;
9 years ago
if (++$count >= 3) {
break;
9 years ago
}
}
9 years ago
$a = array();
9 years ago
foreach ($en as $name => $t) {
if (array_key_exists( $name, $strings)) {
$file = $strings[ $name];
9 years ago
} else {
$file = '';
9 years ago
}
$t = strip_tags( $t);
$a[ $file.' * '.$name] = '$'."string[ '$name'] = $t\r\n";
}
ksort( $a);
9 years ago
if (array_key_exists( $lang, $langname)) {
$langprint = $langname[ $lang];
9 years ago
} else {
$langprint = $lang;
9 years ago
}
9 years ago
$sum[] = "<td>$langprint</td><td><center>".count($a)."</td><td><center>".
round(100 * ($counten - count($a)) / $counten, 0)." %</td>";
$prevfile = '';
foreach ($a as $key => $line) {
$pos = strpos( $key, '*');
9 years ago
$file = trim( substr( $key, 0, $pos - 1));
if (substr( $file, 0, 1) == '/') {
$file = substr( $file, 1);
9 years ago
}
if ($file != $prevfile) {
$s .= "\r\n//$file\r\n";
9 years ago
$prevfile = $file;
}
$s .= $line;
9 years ago
$untranslated .= "//$prevfile ".$line;
}
$file = $outdir.'/'.$lang.'.php';
file_put_contents( $file, $s);
}