To use Yandex Translator you need to get a key API
Get the key API:
Supported translator languages
Let's create a new file 1.php
PHP
Let's write the code in the file 1.php
function translate_yandex_text($source_text, $lang='ru-en', & $translated_text, & $error_description)
{
$translated_text = "";
$error_description= "";
// there must be a character between the words %20
$source_text = str_replace(" ", "%20", $source_text);
// your key should be here yandex api
$yt_api_key = "trnsl.1.1.20181108T121523Z.669d96c33aec2412.c32c6acb77efd.....49f91d4904088ad722a89";
$url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?' .
'key=' . $yt_api_key . '&' .
'text=' . $source_text. '&' .
'lang=' . $lang . '&' .
'format=plain&' .
'options=1&p=2';
$curlObject = curl_init();
curl_setopt($curlObject, CURLOPT_URL, $url);
curl_setopt($curlObject, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObject, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, true);
$responseData = curl_exec($curlObject);
curl_close($curlObject);
$result_arr = json_decode($responseData, true); // convert to an array
$translated_text = $result_arr['text'][0];
$error_description= $result_arr['message'];
if ($error_description=="")
return true; // no errors
return false;
}
// translate the text from English into French en-fr
$source_text = "Hello my friend";
if (translate_yandex_text($source_text, 'en-fr', & $translated_text, & $error))
echo $translated_text; // on the screen we will see Bonjour, mon ami
else
echo $error;