Fix bug in translator tool

This commit is contained in:
Laurent Destailleur 2010-08-27 06:46:57 +00:00
parent f28b3f8aba
commit cb33cdd6a2

View File

@ -14,10 +14,12 @@
class langAutoParser { class langAutoParser {
private $translatedFiles = array(); private $translatedFiles = array();
private $destLang = string; private $destLang = '';
private $refLang = string; private $refLang = '';
private $langDir = string; private $langDir = '';
private $limittofile = string; private $limittofile = '';
private $time;
private $time_end;
private $outputpagecode = 'UTF-8'; private $outputpagecode = 'UTF-8';
//private $outputpagecode = 'ISO-8859-1'; //private $outputpagecode = 'ISO-8859-1';
const DIR_SEPARATOR = '/'; const DIR_SEPARATOR = '/';
@ -46,42 +48,77 @@ class langAutoParser {
if ($this->limittofile && $this->limittofile != $file) continue; if ($this->limittofile && $this->limittofile != $file) continue;
$counter++; $counter++;
$fileContent = null; $fileContent = null;
$this->translatedFiles = array();
$refPath = $this->langDir.$this->refLang.self::DIR_SEPARATOR.$file; $refPath = $this->langDir.$this->refLang.self::DIR_SEPARATOR.$file;
$destPath = $this->langDir.$this->destLang.self::DIR_SEPARATOR.$file; $fileContent = file($refPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$fileContent = file($refPath,FILE_IGNORE_NEW_LINES | print "Processing file " . $file . ", with ".sizeof($fileContent)." lines<br>\n";
FILE_SKIP_EMPTY_LINES);
print "Processing file " . $file . ", found ".sizeof($fileContent)." records<br>\n"; // Define target dirs
// Check destination file presence $targetlangs=array($this->destLang);
if ( ! file_exists( $destPath ) ){ if ($this->destLang == 'all')
// No file presente generate file {
echo "File not found: " . $file . "<br>\n"; $targetlangs=array();
echo "Generating file " . $file . "<br>\n";
$this->createTranslationFile($destPath); // If we must process all languages
} $arraytmp=dol_dir_list($this->langDir,'directories',0);
// Translate lines foreach($arraytmp as $dirtmp)
$fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES |
FILE_SKIP_EMPTY_LINES);
$newlines=0;
foreach($fileContent as $line){
$key = $this->getLineKey($line);
$value = $this->getLineValue($line);
if ($key && $value)
{ {
$newlines+=$this->translateFileLine($fileContentDest,$file,$key,$value); if ($dirtmp['name'] === $this->refLang) continue; // We discard source language
$tmppart=explode('_',$dirtmp['name']);
if (preg_match('/^en/i',$dirtmp['name'])) continue; // We discard en_* languages
if (preg_match('/^fr/i',$dirtmp['name'])) continue; // We discard fr_* languages
if (preg_match('/^es/i',$dirtmp['name'])) continue; // We discard es_* languages
if (preg_match('/ca_ES/i',$dirtmp['name'])) continue; // We discard es_CA language
if (preg_match('/pt_BR/i',$dirtmp['name'])) continue; // We discard pt_BR language
if (preg_match('/^\./i',$dirtmp['name'])) continue; // We discard files .*
if (preg_match('/^CVS/i',$dirtmp['name'])) continue; // We discard CVS
$targetlangs[]=$dirtmp['name'];
} }
//var_dump($targetlangs);
} }
$this->updateTranslationFile($destPath,$file); // Process translation of source file for each target languages
echo "New translated lines: " . $newlines . "<br>\n"; foreach($targetlangs as $mydestLang)
#if ($counter ==3) die('fim'); {
$this->translatedFiles = array();
$destPath = $this->langDir.$mydestLang.self::DIR_SEPARATOR.$file;
// Check destination file presence
if ( ! file_exists( $destPath ) ){
// No file present, we generate file
echo "File not found: " . $destPath . ". We generate it.<br>\n";
$this->createTranslationFile($destPath,$mydestLang);
}
else
{
echo "Updating file: " . $destPath . "<br>\n";
}
// Translate lines
$fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$newlines=0;
foreach($fileContent as $line){
$key = $this->getLineKey($line);
$value = $this->getLineValue($line);
if ($key && $value)
{
$newlines+=$this->translateFileLine($fileContentDest,$file,$key,$value,$mydestLang);
}
}
$this->updateTranslationFile($destPath,$file);
echo "New translated lines: " . $newlines . "<br>\n";
#if ($counter ==3) die('fim');
}
} }
} }
private function updateTranslationFile($destPath,$file){ private function updateTranslationFile($destPath,$file)
{
$this->time_end = date('Y-m-d H:i:s');
if (count($this->translatedFiles[$file])>0){ if (count($this->translatedFiles[$file])>0)
{
$fp = fopen($destPath, 'a'); $fp = fopen($destPath, 'a');
fwrite($fp, "\r\n"); fwrite($fp, "\r\n");
fwrite($fp, "\r\n"); fwrite($fp, "\r\n");
@ -90,16 +127,16 @@ FILE_SKIP_EMPTY_LINES);
foreach( $this->translatedFiles[$file] as $line) { foreach( $this->translatedFiles[$file] as $line) {
fwrite($fp, $line . "\r\n"); fwrite($fp, $line . "\r\n");
} }
fwrite($fp, "// STOP - Lines generated via autotranslator.php tool (".$this->time.").\r\n"); fwrite($fp, "// STOP - Lines generated via autotranslator.php tool (".$this->time_end.").\r\n");
fclose($fp); fclose($fp);
} }
return; return;
} }
private function createTranslationFile($path){ private function createTranslationFile($path,$mydestlang){
$fp = fopen($path, 'w+'); $fp = fopen($path, 'w+');
fwrite($fp, "/*\r\n"); fwrite($fp, "/*\r\n");
fwrite($fp, " * Language code: {$this->destLang}\r\n"); fwrite($fp, " * Language code: {$mydestlang}\r\n");
fwrite($fp, " * Automatic generated via autotranslator.php tool\r\n"); fwrite($fp, " * Automatic generated via autotranslator.php tool\r\n");
fwrite($fp, " * Generation date " . $this->time. "\r\n"); fwrite($fp, " * Generation date " . $this->time. "\r\n");
fwrite($fp, " */\r\n"); fwrite($fp, " */\r\n");
@ -108,15 +145,17 @@ FILE_SKIP_EMPTY_LINES);
} }
/** /**
* Put in array translation of a key * Put in array translatedFiles[$file], line of a new tranlated pair
* *
* @param unknown_type $content Existing content of dest file * @param $content Existing content of dest file
* @param unknown_type $file File name translated (xxxx.lang) * @param $file Target file name translated (xxxx.lang)
* @param unknown_type $key Key to translate * @param $key Key to translate
* @param unknown_type $value Existing key in source file * @param $value Existing value in source file
* @return int 0=Nothing translated, 1=Record translated * @param string Language code (ie: fr_FR)
* @return int 0=Nothing translated, 1=Record translated
*/ */
private function translateFileLine($content,$file,$key,$value){ private function translateFileLine($content,$file,$key,$value,$mydestLang)
{
//print "key =".$key."\n"; //print "key =".$key."\n";
foreach( $content as $line ) { foreach( $content as $line ) {
@ -125,16 +164,24 @@ FILE_SKIP_EMPTY_LINES);
// If translated return // If translated return
//print "destKey=".$destKey."\n"; //print "destKey=".$destKey."\n";
if ( trim($destKey) == trim($key) ) if ( trim($destKey) == trim($key) )
{ // Found already existing translation { // Found already existing translation (key already exits in dest file)
return 0; return 0;
} }
} }
// If not translated then translate
if ($this->outputpagecode == 'UTF-8') $val=$this->translateTexts(array($value),substr($this->refLang,0,2),substr($this->destLang,0,2));
else $val=utf8_decode($this->translateTexts(array($value),substr($this->refLang,0,2),substr($this->destLang,0,2)));
if ($key == 'CHARSET') $val=$this->outputpagecode; if ($key == 'CHARSET') $val=$this->outputpagecode;
else if (preg_match('/^Format/',$key)) $val=$value;
else if ($value=='-') $val=$value;
else
{
// If not translated then translate
if ($this->outputpagecode == 'UTF-8') $val=$this->translateTexts(array($value),substr($this->refLang,0,2),substr($mydestLang,0,2));
else $val=utf8_decode($this->translateTexts(array($value),substr($this->refLang,0,2),substr($mydestLang,0,2)));
}
$val=trim($val);
if (empty($val)) return 0;
$this->translatedFiles[$file][] = $key . '=' . $val ; $this->translatedFiles[$file][] = $key . '=' . $val ;
return 1; return 1;
@ -162,8 +209,15 @@ FILE_SKIP_EMPTY_LINES);
return $files; return $files;
} }
private function translateTexts($src_texts = array(), $src_lang, /**
$dest_lang){ * Return translation of a value
*
* @param $src_texts Array with one value
* @param $src_lang
* @param $dest_lang
* @return string Value translated
*/
private function translateTexts($src_texts = array(), $src_lang, $dest_lang){
$tmp=explode('_',$src_lang); $tmp=explode('_',$src_lang);
if ($tmp[0] == $tmp[1]) $src_lang=$tmp[0]; if ($tmp[0] == $tmp[1]) $src_lang=$tmp[0];
@ -175,9 +229,9 @@ $dest_lang){
$lang_pair = $src_lang.'|'.$dest_lang; $lang_pair = $src_lang.'|'.$dest_lang;
$src_texts_query = ""; $src_texts_query = "";
foreach ($src_texts as $src_text){ $src_text_to_translate=preg_replace('/%s/','SSSSS',join('',$src_texts));
$src_texts_query .= "&q=".urlencode($src_text);
} $src_texts_query .= "&q=".urlencode($src_text_to_translate);
$url = $url =
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0".$src_texts_query."&langpair=".urlencode($lang_pair); "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0".$src_texts_query."&langpair=".urlencode($lang_pair);
@ -185,6 +239,8 @@ $dest_lang){
// sendRequest // sendRequest
// note how referer is set manually // note how referer is set manually
//print "Url to translate: ".$url."\n";
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@ -196,24 +252,16 @@ $dest_lang){
$json = json_decode($body, true); $json = json_decode($body, true);
if ($json['responseStatus'] != 200){ if ($json['responseStatus'] != 200){
print "Error: ".$json['responseStatus']." ".$url."\n";
return false; return false;
} }
$results = $json['responseData']; $rep=$json['responseData']['translatedText'];
$rep=preg_replace('/SSSSS/','%s',$rep);
$return_array = array(); //print "OK ".join('',$src_texts).' => '.$rep."\n";
foreach ($results as $result){ return $rep;
if ($result['responseStatus'] == 200){
$return_array[] = $result['responseData']['translatedText'];
} else {
$return_array[] = false;
}
}
//return translated text
#return $return_array;
return $json['responseData']['translatedText'];
} }
} }