Fix: error if no api key
This commit is contained in:
parent
6cf68c12d3
commit
a0138493fc
@ -27,34 +27,34 @@
|
|||||||
*/
|
*/
|
||||||
class autoTranslator
|
class autoTranslator
|
||||||
{
|
{
|
||||||
private $translatedFiles = array();
|
private $_translatedFiles = array();
|
||||||
private $destLang = '';
|
private $_destlang = '';
|
||||||
private $refLang = '';
|
private $_refLang = '';
|
||||||
private $langDir = '';
|
private $_langDir = '';
|
||||||
private $limittofile = '';
|
private $_limittofile = '';
|
||||||
private $time;
|
private $_time;
|
||||||
private $time_end;
|
private $_time_end;
|
||||||
private $outputpagecode = 'UTF-8';
|
private $_outputpagecode = 'UTF-8';
|
||||||
private $apikey;
|
private $_apikey;
|
||||||
//private $outputpagecode = 'ISO-8859-1';
|
//private $_outputpagecode = 'ISO-8859-1';
|
||||||
const DIR_SEPARATOR = '/';
|
const DIR_SEPARATOR = '/';
|
||||||
|
|
||||||
|
|
||||||
function __construct($destLang,$refLang,$langDir,$limittofile,$apikey)
|
function __construct($_destlang,$_refLang,$_langDir,$_limittofile,$_apikey)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Set enviorment variables
|
// Set enviorment variables
|
||||||
$this->destLang = $destLang;
|
$this->_destlang = $_destlang;
|
||||||
$this->refLang = $refLang;
|
$this->_refLang = $_refLang;
|
||||||
$this->langDir = $langDir.self::DIR_SEPARATOR;
|
$this->_langDir = $_langDir.self::DIR_SEPARATOR;
|
||||||
$this->time = date('Y-m-d H:i:s');
|
$this->_time = date('Y-m-d H:i:s');
|
||||||
$this->limittofile = $limittofile;
|
$this->_limittofile = $_limittofile;
|
||||||
$this->apikey = $apikey;
|
$this->_apikey = $_apikey;
|
||||||
|
|
||||||
// Translate
|
// Translate
|
||||||
//ini_set('default_charset','UTF-8');
|
//ini_set('default_charset','UTF-8');
|
||||||
ini_set('default_charset',$this->outputpagecode);
|
ini_set('default_charset',$this->_outputpagecode);
|
||||||
$this->parseRefLangTranslationFiles();
|
$this->parse_refLangTranslationFiles();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,31 +63,31 @@ class autoTranslator
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function parseRefLangTranslationFiles()
|
private function parse_refLangTranslationFiles()
|
||||||
{
|
{
|
||||||
|
|
||||||
$files = $this->getTranslationFilesArray($this->refLang);
|
$files = $this->getTranslationFilesArray($this->_refLang);
|
||||||
$counter = 1;
|
$counter = 1;
|
||||||
foreach($files as $file)
|
foreach($files as $file)
|
||||||
{
|
{
|
||||||
if ($this->limittofile && $this->limittofile != $file) continue;
|
if ($this->_limittofile && $this->_limittofile != $file) continue;
|
||||||
$counter++;
|
$counter++;
|
||||||
$fileContent = null;
|
$fileContent = null;
|
||||||
$refPath = $this->langDir.$this->refLang.self::DIR_SEPARATOR.$file;
|
$refPath = $this->_langDir.$this->_refLang.self::DIR_SEPARATOR.$file;
|
||||||
$fileContent = file($refPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
|
$fileContent = file($refPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
|
||||||
print "Processing file " . $file . ", with ".count($fileContent)." lines<br>\n";
|
print "Processing file " . $file . ", with ".count($fileContent)." lines<br>\n";
|
||||||
|
|
||||||
// Define target dirs
|
// Define target dirs
|
||||||
$targetlangs=array($this->destLang);
|
$targetlangs=array($this->_destlang);
|
||||||
if ($this->destLang == 'all')
|
if ($this->_destlang == 'all')
|
||||||
{
|
{
|
||||||
$targetlangs=array();
|
$targetlangs=array();
|
||||||
|
|
||||||
// If we must process all languages
|
// If we must process all languages
|
||||||
$arraytmp=dol_dir_list($this->langDir,'directories',0);
|
$arraytmp=dol_dir_list($this->_langDir,'directories',0);
|
||||||
foreach($arraytmp as $dirtmp)
|
foreach($arraytmp as $dirtmp)
|
||||||
{
|
{
|
||||||
if ($dirtmp['name'] === $this->refLang) continue; // We discard source language
|
if ($dirtmp['name'] === $this->_refLang) continue; // We discard source language
|
||||||
$tmppart=explode('_',$dirtmp['name']);
|
$tmppart=explode('_',$dirtmp['name']);
|
||||||
if (preg_match('/^en/i',$dirtmp['name'])) continue; // We discard en_* languages
|
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('/^fr/i',$dirtmp['name'])) continue; // We discard fr_* languages
|
||||||
@ -103,17 +103,17 @@ class autoTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process translation of source file for each target languages
|
// Process translation of source file for each target languages
|
||||||
foreach($targetlangs as $mydestLang)
|
foreach($targetlangs as $my_destlang)
|
||||||
{
|
{
|
||||||
$this->translatedFiles = array();
|
$this->_translatedFiles = array();
|
||||||
|
|
||||||
$destPath = $this->langDir.$mydestLang.self::DIR_SEPARATOR.$file;
|
$destPath = $this->_langDir.$my_destlang.self::DIR_SEPARATOR.$file;
|
||||||
// Check destination file presence
|
// Check destination file presence
|
||||||
if (! file_exists($destPath))
|
if (! file_exists($destPath))
|
||||||
{
|
{
|
||||||
// No file present, we generate file
|
// No file present, we generate file
|
||||||
echo "File not found: " . $destPath . ". We generate it.<br>\n";
|
echo "File not found: " . $destPath . ". We generate it.<br>\n";
|
||||||
$this->createTranslationFile($destPath,$mydestLang);
|
$this->createTranslationFile($destPath,$my_destlang);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -128,11 +128,11 @@ class autoTranslator
|
|||||||
$value = $this->getLineValue($line);
|
$value = $this->getLineValue($line);
|
||||||
if ($key && $value)
|
if ($key && $value)
|
||||||
{
|
{
|
||||||
$newlines+=$this->translateFileLine($fileContentDest,$file,$key,$value,$mydestLang);
|
$newlines+=$this->translateFileLine($fileContentDest,$file,$key,$value,$my_destlang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->updateTranslationFile($destPath,$file,$mydestLang);
|
$this->updateTranslationFile($destPath,$file,$my_destlang);
|
||||||
echo "New translated lines: " . $newlines . "<br>\n";
|
echo "New translated lines: " . $newlines . "<br>\n";
|
||||||
//if ($counter ==3) die('fim');
|
//if ($counter ==3) die('fim');
|
||||||
}
|
}
|
||||||
@ -144,24 +144,24 @@ class autoTranslator
|
|||||||
*
|
*
|
||||||
* @param string $destPath Target path
|
* @param string $destPath Target path
|
||||||
* @param string $file File
|
* @param string $file File
|
||||||
* @param string $mydestLang Target language code
|
* @param string $my_destlang Target language code
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function updateTranslationFile($destPath,$file,$mydestLang)
|
private function updateTranslationFile($destPath,$file,$my_destlang)
|
||||||
{
|
{
|
||||||
$this->time_end = date('Y-m-d H:i:s');
|
$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");
|
||||||
fwrite($fp, "// START - Lines generated via autotranslator.php tool (".$this->time.").\r\n");
|
fwrite($fp, "// START - Lines generated via autotranslator.php tool (".$this->_time.").\r\n");
|
||||||
fwrite($fp, "// Reference language: ".$this->refLang." -> ".$mydestLang."\r\n");
|
fwrite($fp, "// Reference language: ".$this->_refLang." -> ".$my_destlang."\r\n");
|
||||||
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_end.").\r\n");
|
fwrite($fp, "// STOP - Lines generated via autotranslator.php tool (".$this->_time_end.").\r\n");
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -171,32 +171,32 @@ class autoTranslator
|
|||||||
* Create a new translation file
|
* Create a new translation file
|
||||||
*
|
*
|
||||||
* @param string $path Path
|
* @param string $path Path
|
||||||
* @param string $mydestlang Target language code
|
* @param string $my_destlang Target language code
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function createTranslationFile($path,$mydestlang)
|
private function createTranslationFile($path,$my_destlang)
|
||||||
{
|
{
|
||||||
$fp = fopen($path, 'w+');
|
$fp = fopen($path, 'w+');
|
||||||
fwrite($fp, "/*\r\n");
|
fwrite($fp, "/*\r\n");
|
||||||
fwrite($fp, " * Language code: {$mydestlang}\r\n");
|
fwrite($fp, " * Language code: {$my_destlang}\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");
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put in array translatedFiles[$file], line of a new tranlated pair
|
* Put in array _translatedFiles[$file], line of a new tranlated pair
|
||||||
*
|
*
|
||||||
* @param string $content Existing content of dest file
|
* @param string $content Existing content of dest file
|
||||||
* @param string $file Target file name translated (xxxx.lang)
|
* @param string $file Target file name translated (xxxx.lang)
|
||||||
* @param string $key Key to translate
|
* @param string $key Key to translate
|
||||||
* @param string $value Existing value in source file
|
* @param string $value Existing value in source file
|
||||||
* @param string $mydestLang Language code (ie: fr_FR)
|
* @param string $my_destlang Language code (ie: fr_FR)
|
||||||
* @return int 0=Nothing translated, 1=Record translated
|
* @return int 0=Nothing translated, 1=Record translated
|
||||||
*/
|
*/
|
||||||
private function translateFileLine($content,$file,$key,$value,$mydestLang)
|
private function translateFileLine($content,$file,$key,$value,$my_destlang)
|
||||||
{
|
{
|
||||||
|
|
||||||
//print "key =".$key."\n";
|
//print "key =".$key."\n";
|
||||||
@ -211,21 +211,21 @@ class autoTranslator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key == 'CHARSET') $val=$this->outputpagecode;
|
if ($key == 'CHARSET') $val=$this->_outputpagecode;
|
||||||
else if (preg_match('/^Format/',$key)) $val=$value;
|
else if (preg_match('/^Format/',$key)) $val=$value;
|
||||||
else if ($value=='-') $val=$value;
|
else if ($value=='-') $val=$value;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// If not translated then translate
|
// If not translated then translate
|
||||||
if ($this->outputpagecode == 'UTF-8') $val=$this->translateTexts(array($value),substr($this->refLang,0,2),substr($mydestLang,0,2));
|
if ($this->_outputpagecode == 'UTF-8') $val=$this->translateTexts(array($value),substr($this->_refLang,0,2),substr($my_destlang,0,2));
|
||||||
else $val=utf8_decode($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($my_destlang,0,2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
$val=trim($val);
|
$val=trim($val);
|
||||||
|
|
||||||
if (empty($val)) return 0;
|
if (empty($val)) return 0;
|
||||||
|
|
||||||
$this->translatedFiles[$file][] = $key . '=' . $val ;
|
$this->_translatedFiles[$file][] = $key . '=' . $val ;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ class autoTranslator
|
|||||||
*/
|
*/
|
||||||
private function getTranslationFilesArray($lang)
|
private function getTranslationFilesArray($lang)
|
||||||
{
|
{
|
||||||
$dir = new DirectoryIterator($this->langDir.$lang);
|
$dir = new DirectoryIterator($this->_langDir.$lang);
|
||||||
while($dir->valid()) {
|
while($dir->valid()) {
|
||||||
if(!$dir->isDot() && $dir->isFile() && ! preg_match('/^\./',$dir->getFilename())) {
|
if(!$dir->isDot() && $dir->isFile() && ! preg_match('/^\./',$dir->getFilename())) {
|
||||||
$files[] = $dir->getFilename();
|
$files[] = $dir->getFilename();
|
||||||
@ -297,8 +297,8 @@ class autoTranslator
|
|||||||
//$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($src_text_to_translate)."&langpair=".urlencode($lang_pair);
|
//$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($src_text_to_translate)."&langpair=".urlencode($lang_pair);
|
||||||
// Example: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Setup%20area&langpair=en_US|fr_FR
|
// Example: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Setup%20area&langpair=en_US|fr_FR
|
||||||
// Define GET URL v2
|
// Define GET URL v2
|
||||||
$url = "https://www.googleapis.com/language/translate/v2?key=".$this->apikey."&q=".urlencode($src_text_to_translate)."&source=".urlencode($src_lang)."&target=".urlencode($dest_lang);
|
$url = "https://www.googleapis.com/language/translate/v2?key=".$this->_apikey."&q=".urlencode($src_text_to_translate)."&source=".urlencode($src_lang)."&target=".urlencode($dest_lang);
|
||||||
// Example: https://www.googleapis.com/language/translate/v2?key=APIKEY&q=Setup%20area&source=en_US&target=fr_FR
|
// Example: https://www.googleapis.com/language/translate/v2?key=_apikey&q=Setup%20area&source=en_US&target=fr_FR
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
//print "Url to translate: ".$url."\n";
|
//print "Url to translate: ".$url."\n";
|
||||||
|
|||||||
@ -55,7 +55,7 @@ print "***** ".$script_file." (".$version.") *****\n";
|
|||||||
$dir=DOL_DOCUMENT_ROOT."/langs";
|
$dir=DOL_DOCUMENT_ROOT."/langs";
|
||||||
|
|
||||||
// Check parameters
|
// Check parameters
|
||||||
if (! isset($argv[2])) {
|
if (! isset($argv[3])) {
|
||||||
print "Usage: ".$script_file." lang_code_src lang_code_dest|all APIKEY [langfile.lang]\n";
|
print "Usage: ".$script_file." lang_code_src lang_code_dest|all APIKEY [langfile.lang]\n";
|
||||||
print "Example: ".$script_file." en_US pt_PT 123456\n";
|
print "Example: ".$script_file." en_US pt_PT 123456\n";
|
||||||
print "Rem: lang_code to use can be found on http://www.google.com/language_tools\n";
|
print "Rem: lang_code to use can be found on http://www.google.com/language_tools\n";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user