function xlate_hook_recvsms_intercept($sms_datetime, $sms_sender, $message, $sms_receiver) { global $core_config; $msg = explode(" ", $message); $ret = array(); if (count($msg) > 1) { $keyword = trim($msg[0]); if (substr($keyword, 0, 1) == '@') { $xlate = substr($keyword, 1); $xlate = explode('2', $xlate); $xlate_from = $xlate[0]; $xlate_to = $xlate[1]; if ($xlate_from && $xlate_to && strlen($xlate_from) == 2 && strlen($xlate_to) == 2) { for ($i = 1; $i < count($msg); $i++) { $words .= $msg[$i] . " "; } $words = trim($words); // contact google $lib = $core_config['apps_path']['plug'] . '/feature/xlate/lib/GoogleTranslate'; // load JSON.php for PHP version lower than 5.2.x require_once $lib . '/JSON.php'; require_once $lib . '/googleTranslate.class.php'; if ($gt = new GoogleTranslateWrapper()) { /* Translate */ $xlate_words = $gt->translate($words, $xlate_to, $xlate_from); // incoming sms is handled $ret['hooked'] = true; /* Was translation successful */ $sms_datetime = core_display_datetime($sms_datetime); if ($gt->isSuccess()) { $reply = '@' . $xlate_from . '2' . $xlate_to . ' ' . $words . ' => ' . $xlate_words; logger_print("success dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " w:" . $words . " from:" . $xlate_from . " to:" . $xlate_to . " xlate:" . $xlate_words, 2, "xlate"); } else { $reply = '@' . $xlate_from . '2' . $xlate_to . ' ' . _("unable to translate") . ': ' . $words; logger_print("failed dt:" . $sms_datetime . " s:" . $sms_sender . " r:" . $sms_receiver . " w:" . $words . " from:" . $xlate_from . " to:" . $xlate_to, 2, "xlate"); } // detect reply message, set unicode if not ASCII $unicode = core_detect_unicode($reply); // send reply SMS using admin account // should add a web menu in xlate.php to choose which account will be used to send reply SMS // usualy we inspect the result of sendsms, but not this time logger_print("send reply encoding:" . $encoding, 2, "xlate"); $reply = addslashes($reply); list($ok, $to, $smslog_id, $queue) = sendsms_helper('admin', $sms_sender, $reply, 'text', $unicode); // do not forget to tell parent that this SMS has been hooked $ret['hooked'] = true; } else { // unable to load the class, set incoming sms unhandled $ret['hooked'] = false; logger_print("class not exists or fail to load", 2, "xlate"); } } } } return $ret; }
<?php require_once 'googleTranslate.class.php'; $gt = new GoogleTranslateWrapper(); /* Translate text from one language to another */ $test = 'hello'; /* language detection */ print_r($gt->detectLanguage($test)); /* Translate */ echo $gt->translate($test, "fr", "en"); /* Was translation successful */ echo $gt->isSuccess();