translate($phrase,$to,'en',"text/html"); # In case, it failed in translation, lets give it another try if (!$translation) { $translation = $GoogleTranslator->translate($phrase,$to,'en',"text/html"); } if (!empty($translation)) { # build default translation log path $transFile = BASEDIR.'/includes/langs/'.$to.'.lang'; $logFile = FILES_DIR.'/translation_'.$to.'.log'; # check if file exists or not because handling will matter if (file_exists($transFile)) { # read data of file to ease up appending $existingData = file_get_contents($transFile); # Make data readable by converting in Json $data = json_decode($existingData,true); # considering file is empty, lets create an array if (!is_array($data)) { $data = array(); } $data[$phrase_code] = $translation; } else { $data = array(); $data[$phrase_code] = $translation; } if (!empty($data)) { if (file_exists($logFile)) { $langLogData = file_get_contents($logFile); } else { $langLogData = ""; } $progressPercent = ($current/$total)*100; $progressPercent = intval($progressPercent); file_put_contents($logFile, $langLogData."\n"."[DONE] ".$current." / ".$total." [$progressPercent %] Translated phrase : $phrase"); file_put_contents(BASEDIR."/files/percent.lang", $progressPercent."\n"); $data = json_encode($data); if (!file_put_contents($transFile, $data)) { return false; } } return $translation; } else { return false; } } } // When there is data in _POST, run the process if(isset($_POST['selectFieldValue'])) { #sleep(2); global $lang_obj; $output = array(); $iso_code = $_POST['selectFieldValue']; $language_detact = $_POST['langDetect']; $phrase = $_POST['phrase']; $phrase_code = $_POST['phrase_code']; $totalPhrases = $_POST['totalPhrases']; $phraseNum = $_POST['phraseNum']; $translation = translate_phrase($phrase,$phrase_code,$iso_code, $totalPhrases, $phraseNum); if (!empty($translation)) { # translation was success, lets proceed $progressPercent = ($phraseNum/$totalPhrases)*100; $progressPercent = intval($progressPercent); $output['status'] = 'success'; $output['phrase_code'] = $phrase_code; $output['phrase'] = $phrase; $output['translation'] = $translation; $output['progress'] = $progressPercent; if ($phrase == 'aye') { $lang_obj->import_packlang($iso_code,$iso_code,$language_detact); header("refresh: 2;"); } echo json_encode($output); } else { # unable to translate, throw error $output['status'] = 'error'; return false; } }