autotranslator.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file dev/translation/langAutoParser.class.php
  19. * \ingroup dev
  20. * \brief This file is an example for a command line script
  21. */
  22. /**
  23. * Class to parse language files and translate them
  24. * This is a language automatic translator parser for Dolibarr
  25. */
  26. class autoTranslator
  27. {
  28. private $_translatedFiles = array();
  29. private $_destlang = '';
  30. private $_refLang = '';
  31. private $_langDir = '';
  32. private $_limittofile = '';
  33. private $_time;
  34. private $_time_end;
  35. private $_outputpagecode = 'UTF-8';
  36. private $_apikey;
  37. //private $_outputpagecode = 'ISO-8859-1';
  38. const DIR_SEPARATOR = '/';
  39. function __construct($_destlang,$_refLang,$_langDir,$_limittofile,$_apikey)
  40. {
  41. // Set enviorment variables
  42. $this->_destlang = $_destlang;
  43. $this->_refLang = $_refLang;
  44. $this->_langDir = $_langDir.self::DIR_SEPARATOR;
  45. $this->_time = date('Y-m-d H:i:s');
  46. $this->_limittofile = $_limittofile;
  47. $this->_apikey = $_apikey;
  48. // Translate
  49. //ini_set('default_charset','UTF-8');
  50. ini_set('default_charset',$this->_outputpagecode);
  51. $this->parse_refLangTranslationFiles();
  52. }
  53. /**
  54. * Parse file
  55. *
  56. * @return void
  57. */
  58. private function parse_refLangTranslationFiles()
  59. {
  60. $files = $this->getTranslationFilesArray($this->_refLang);
  61. $counter = 1;
  62. foreach($files as $file)
  63. {
  64. if ($this->_limittofile && $this->_limittofile != $file) continue;
  65. $counter++;
  66. $fileContent = null;
  67. $refPath = $this->_langDir.$this->_refLang.self::DIR_SEPARATOR.$file;
  68. $fileContent = file($refPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
  69. print "Processing file " . $file . ", with ".count($fileContent)." lines<br>\n";
  70. // Define target dirs
  71. $targetlangs=array($this->_destlang);
  72. if ($this->_destlang == 'all')
  73. {
  74. $targetlangs=array();
  75. // If we must process all languages
  76. $arraytmp=dol_dir_list($this->_langDir,'directories',0);
  77. foreach($arraytmp as $dirtmp)
  78. {
  79. if ($dirtmp['name'] === $this->_refLang) continue; // We discard source language
  80. $tmppart=explode('_',$dirtmp['name']);
  81. if (preg_match('/^en/i',$dirtmp['name'])) continue; // We discard en_* languages
  82. if (preg_match('/^fr/i',$dirtmp['name'])) continue; // We discard fr_* languages
  83. if (preg_match('/^es/i',$dirtmp['name'])) continue; // We discard es_* languages
  84. if (preg_match('/ca_ES/i',$dirtmp['name'])) continue; // We discard es_CA language
  85. if (preg_match('/pt_BR/i',$dirtmp['name'])) continue; // We discard pt_BR language
  86. if (preg_match('/nl_BE/i',$dirtmp['name'])) continue; // We discard nl_BE language
  87. if (preg_match('/^\./i',$dirtmp['name'])) continue; // We discard files .*
  88. if (preg_match('/^CVS/i',$dirtmp['name'])) continue; // We discard CVS
  89. $targetlangs[]=$dirtmp['name'];
  90. }
  91. //var_dump($targetlangs);
  92. }
  93. // Process translation of source file for each target languages
  94. foreach($targetlangs as $my_destlang)
  95. {
  96. $this->_translatedFiles = array();
  97. $destPath = $this->_langDir.$my_destlang.self::DIR_SEPARATOR.$file;
  98. // Check destination file presence
  99. if (! file_exists($destPath))
  100. {
  101. // No file present, we generate file
  102. echo "File not found: " . $destPath . ". We generate it.<br>\n";
  103. $this->createTranslationFile($destPath,$my_destlang);
  104. }
  105. else
  106. {
  107. echo "Updating file: " . $destPath . "<br>\n";
  108. }
  109. // Translate lines
  110. $fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
  111. $newlines=0;
  112. foreach($fileContent as $line){
  113. $key = $this->getLineKey($line);
  114. $value = $this->getLineValue($line);
  115. if ($key && $value)
  116. {
  117. $newlines+=$this->translateFileLine($fileContentDest,$file,$key,$value,$my_destlang);
  118. }
  119. }
  120. $this->updateTranslationFile($destPath,$file,$my_destlang);
  121. echo "New translated lines: " . $newlines . "<br>\n";
  122. //if ($counter ==3) die('fim');
  123. }
  124. }
  125. }
  126. /**
  127. * Update file with new translations
  128. *
  129. * @param string $destPath Target path
  130. * @param string $file File
  131. * @param string $my_destlang Target language code
  132. * @return void
  133. */
  134. private function updateTranslationFile($destPath,$file,$my_destlang)
  135. {
  136. $this->_time_end = date('Y-m-d H:i:s');
  137. if (isset($this->_translatedFiles[$file]) && count($this->_translatedFiles[$file])>0)
  138. {
  139. $fp = fopen($destPath, 'a');
  140. fwrite($fp, "\n");
  141. fwrite($fp, "\n");
  142. fwrite($fp, "// START - Lines generated via autotranslator.php tool (".$this->_time.").\n");
  143. fwrite($fp, "// Reference language: ".$this->_refLang." -> ".$my_destlang."\n");
  144. foreach( $this->_translatedFiles[$file] as $line) {
  145. fwrite($fp, $line . "\n");
  146. }
  147. fwrite($fp, "// STOP - Lines generated via autotranslator.php tool (".$this->_time_end.").\n");
  148. fclose($fp);
  149. }
  150. return;
  151. }
  152. /**
  153. * Create a new translation file
  154. *
  155. * @param string $path Path
  156. * @param string $my_destlang Target language code
  157. * @return void
  158. */
  159. private function createTranslationFile($path,$my_destlang)
  160. {
  161. $fp = fopen($path, 'w+');
  162. fwrite($fp, "/*\n");
  163. fwrite($fp, " * Language code: {$my_destlang}\n");
  164. fwrite($fp, " * Automatic generated via autotranslator.php tool\n");
  165. fwrite($fp, " * Generation date " . $this->_time. "\n");
  166. fwrite($fp, " */\n");
  167. fclose($fp);
  168. return;
  169. }
  170. /**
  171. * Put in array _translatedFiles[$file], line of a new tranlated pair
  172. *
  173. * @param string $content Existing content of dest file
  174. * @param string $file Target file name translated (xxxx.lang)
  175. * @param string $key Key to translate
  176. * @param string $value Existing value in source file
  177. * @param string $my_destlang Language code (ie: fr_FR)
  178. * @return int 0=Nothing translated, 1=Record translated
  179. */
  180. private function translateFileLine($content,$file,$key,$value,$my_destlang)
  181. {
  182. //print "key =".$key."\n";
  183. foreach( $content as $line ) {
  184. $destKey = $this->getLineKey($line);
  185. $destValue = $this->getLineValue($line);
  186. // If translated return
  187. //print "destKey=".$destKey."\n";
  188. if ( trim($destKey) == trim($key) )
  189. { // Found already existing translation (key already exits in dest file)
  190. return 0;
  191. }
  192. }
  193. if ($key == 'CHARSET') $val=$this->_outputpagecode;
  194. else if (preg_match('/^Format/',$key)) $val=$value;
  195. else if ($value=='-') $val=$value;
  196. else
  197. {
  198. // If not translated then translate
  199. if ($this->_outputpagecode == 'UTF-8') $val=$this->translateTexts(array($value),substr($this->_refLang,0,2),substr($my_destlang,0,2));
  200. else $val=utf8_decode($this->translateTexts(array($value),substr($this->_refLang,0,2),substr($my_destlang,0,2)));
  201. }
  202. $val=trim($val);
  203. if (empty($val)) return 0;
  204. $this->_translatedFiles[$file][] = $key . '=' . $val ;
  205. return 1;
  206. }
  207. /**
  208. * getLineKey
  209. *
  210. * @param string $line Line found into file
  211. * @return string Key
  212. */
  213. private function getLineKey($line)
  214. {
  215. $arraykey = explode('=',$line,2);
  216. return trim($arraykey[0]);
  217. }
  218. /**
  219. * getLineValue
  220. *
  221. * @param string $line Line found into file
  222. * @return string Value
  223. */
  224. private function getLineValue($line)
  225. {
  226. $arraykey = explode('=',$line,2);
  227. return trim(isset($arraykey[1])?$arraykey[1]:'');
  228. }
  229. /**
  230. * getTranslationFilesArray
  231. *
  232. * @param string $lang Language code
  233. * @return array Array
  234. */
  235. private function getTranslationFilesArray($lang)
  236. {
  237. $dir = new DirectoryIterator($this->_langDir.$lang);
  238. while($dir->valid()) {
  239. if(!$dir->isDot() && $dir->isFile() && ! preg_match('/^\./',$dir->getFilename())) {
  240. $files[] = $dir->getFilename();
  241. }
  242. $dir->next();
  243. }
  244. return $files;
  245. }
  246. /**
  247. * Return translation of a value
  248. *
  249. * @param array $src_texts Array with one value
  250. * @param string $src_lang Language code source (us, fr, it, ...)
  251. * @param string $dest_lang Language code target (es, de, ...)
  252. * @return string Value translated
  253. */
  254. private function translateTexts($src_texts, $src_lang, $dest_lang)
  255. {
  256. // We want to be sure that src_lang and dest_lang are using 2 chars only
  257. $tmp=explode('_',$src_lang);
  258. if (! empty($tmp[1]) && $tmp[0] == $tmp[1]) $src_lang=$tmp[0];
  259. $tmp=explode('_',$dest_lang);
  260. if (! empty($tmp[1]) && $tmp[0] == $tmp[1]) $dest_lang=$tmp[0];
  261. //setting language pair
  262. $lang_pair = $src_lang.'|'.$dest_lang;
  263. $src_text_to_translate=preg_replace('/%s/','SSSSS',join('',$src_texts));
  264. $src_text_to_translate=preg_replace('/'.preg_quote('\n\n').'/',' NNNNN ',$src_text_to_translate);
  265. // Define GET URL v1
  266. //$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($src_text_to_translate)."&langpair=".urlencode($lang_pair);
  267. // Example: http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Setup%20area&langpair=en_US|fr_FR
  268. // Define GET URL v2
  269. $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);
  270. // Example: https://www.googleapis.com/language/translate/v2?key=_apikey&q=Setup%20area&source=en_US&target=fr_FR
  271. // Send request
  272. //print "Url to translate: ".$url."\n";
  273. if (! function_exists("curl_init"))
  274. {
  275. print "Error, your PHP does not support curl functions.\n";
  276. die();
  277. }
  278. $ch = curl_init();
  279. curl_setopt($ch, CURLOPT_URL, $url);
  280. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  281. curl_setopt($ch, CURLOPT_REFERER, "Mozilla");
  282. $body = curl_exec($ch);
  283. curl_close($ch);
  284. //sleep(1); // This is to avoid to overload server.
  285. // now, process the JSON string
  286. $json = json_decode($body, true);
  287. if ((! empty($json['responseStatus']) && $json['responseStatus'] != 200)
  288. || count($json['data']['translations']) == 0)
  289. {
  290. print "Error: ".$json['responseStatus']." ".$url."\n";
  291. return false;
  292. }
  293. $rep=$json['data']['translations'][0]['translatedText'];
  294. $rep=preg_replace('/SSSSS/i','%s',$rep);
  295. $rep=preg_replace('/NNNNN/i','\n\n',$rep);
  296. $rep=preg_replace('/&#39;/i','\'',$rep);
  297. //print "OK ".join('',$src_texts).' => '.$rep."\n";
  298. return $rep;
  299. }
  300. }