Мәтінда Google Translate арқылы аудару
-
gmyrzamuratov
- Алгоритм / 19 қараша 2018, 15:09
- 822
Бір кездері үлкен массивтегі мәтінді аудару керек болды.
Google Translate қызметін қолдандық. Егер керек болса php-мен жазылған код төменде:
Google Translate қызметін қолдандық. Егер керек болса php-мен жазылған код төменде:
urlencode($source), ‘tl’ => urlencode($target), ‘q’ => urlencode($text) ); if((strlen($fields[‘q’])/2)>=5000){
echo «Maximum number of characters exceeded: 5000»;
throw new \Exception(«Maximum number of characters exceeded: 5000»);
}
// URL-ify the data for the POST
$fields_string = «»;
foreach ($fields as $key => $value) {
$fields_string .= $key . ‘=’ . $value . ‘&’;
}
rtrim($fields_string, ‘&’);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, ‘UTF-8’);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, ‘AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1’);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
function getSentencesFromJSON($json) {
$sentencesArray = json_decode($json, true);
$sentences = «»;
foreach ($sentencesArray[«sentences»] as $s) {
$sentences .= isset($s[«trans»]) ? $s[«trans»] :»;
}
return $sentences;
}
?>
-
0