Skip to content

Instantly share code, notes, and snippets.

@griga
Last active January 12, 2022 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griga/89e2dc4b040841bf8469 to your computer and use it in GitHub Desktop.
Save griga/89e2dc4b040841bf8469 to your computer and use it in GitHub Desktop.
console google translation php script
<?php
/**
* @author grigach@gmail.com
*
* @usage translate.php <word> [-a] [-s]
*
* -a - print all translations
* -s - print translation synonyms
*
* @example
* /c/admin/scripts/common
$ php translate.php origin -a -s
origin -> имя существительное
происхождение [origin, descent, birth, genesis, background, lineage]
начало [start, starting, beginning, outbreak, origin, onset]
источник [source, spring, origin, fountain, fount, wellspring]
возникновение [emergence, origin, beginning, genesis, uprising, nascency]
первоисточник [primary source, original, origin, fountainhead]
первопричина [prime cause, origin, source, principle]
корень дерева [origin]
*/
function get_with_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
$response = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
} else {
curl_close($ch);
return $response;
}
}
/**
* cleanup google api response (json could not be parsed without this step)
*/
function cleanup_response($response)
{
$response = preg_replace('/,+/', ',', $response);
$response = preg_replace('/\[,/', '[', $response);
return $response;
}
/**
* output transformations: cp866 is used for windows cyrillic command line
* (don't forget to set console font to Lucida Console or similar)
*/
function print_line($line)
{
echo iconv("utf-8", "cp866", $line) . PHP_EOL;
}
$term = $argv[1];
$all = in_array('-a', $argv);
$syn = in_array('-s', $argv);
$api_url = 'http://translate.google.com/translate_a/single';
$params = [
"client" => "t",
"sl" => "en", // source lang
"tl" => "ru", // translate to lang
"hl" => "ru", // messages lang
"dt" => "bd", // get translation with synonymous worlds
"q" => $term,
];
$translations = json_decode(cleanup_response(get_with_curl($api_url . '?' . http_build_query($params))));
echo PHP_EOL;
if (isset($translations[0][0][0], $translations[0][0][2])) {
print_line($term . ' -> ' . $translations[0][0][0]);
$translations = $all ? $translations[0][0][2] : [$translations[0][0][2][0]];
foreach ($translations as $translation) {
if ($syn) {
print_line($translation[0] . ' [' . implode(', ', $translation[1]) . ']');
} else {
print_line($translation[0]);
}
}
} else {
print_line("Ничего не найдено");
}
@gumerfox
Copy link

Я только начал изучать php. Я установил этот скрипт на сайт, но перевод не работает. Как правильно прописать источники, пути?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment