Made changes to output

This commit is contained in:
Saqib Razzaq 2016-10-14 18:47:23 +05:00
parent 4d175e5cda
commit 4595b19744

View file

@ -1,12 +1,18 @@
<?php
/**
* File : ajaxLang
* Description : Handles language translation in Languages area of admin section
*/
require('../includes/config.inc.php');
/**
* Creates initial logs file to store translation progress related data
* @param : { string } { $langName } { iso lang code }
* @param : { integer } { $total } { total number of phrases that are to be translated }
*
* @return : { string / boolean } { full path to log file else false }
* * @return : { string / boolean } { full path to log file else false }
* @since : 8th October, 2016 ClipBucket 2.8.1
* @author : Saqib Razzaq
*/
@ -40,7 +46,7 @@ require('../includes/config.inc.php');
* @author : Saqib Razzaq
*/
function translate_phrase($phrase, $phrase_code, $to) {
function translate_phrase($phrase, $phrase_code, $to, $total, $current) {
global $MrsTranslator;
/**
* There is no point in starting with empty phrase
@ -58,13 +64,14 @@ require('../includes/config.inc.php');
if (!empty($translation)) {
# build default translation log path
$file = FILES_DIR.'/translation_'.$to.'.log';
$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($file)) {
if (file_exists($transFile)) {
# read data of file to ease up appending
$existingData = file_get_contents($file);
$existingData = file_get_contents($transFile);
# Make data readable by converting in Json
$data = json_decode($existingData,true);
@ -81,8 +88,20 @@ require('../includes/config.inc.php');
}
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($file, $data)) {
if (!file_put_contents($transFile, $data)) {
return false;
}
}
@ -94,22 +113,34 @@ require('../includes/config.inc.php');
}
}
// When there is data in _POST, run the process
if(isset($_POST['selectFieldValue'])) {
sleep(2);
#sleep(2);
$output = array();
$iso_code = $_POST['selectFieldValue'];
$language_detact = $_POST['langDetect'];
$phrase = $_POST['phrase'];
$phrase_code = $_POST['phrase_code'];
$translation = translate_phrase($phrase,$phrase_code,$iso_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;
echo json_encode($output);
} else {
# unable to translate, throw error
$output['status'] = 'error';
return false;
}