Skip to content

Instantly share code, notes, and snippets.

@kamiel79
Created November 29, 2014 00:27
Show Gist options
  • Save kamiel79/1f958accbb8ac3f5366c to your computer and use it in GitHub Desktop.
Save kamiel79/1f958accbb8ac3f5366c to your computer and use it in GitHub Desktop.
Yandex over AJAX
/* Code should be self-explanatory
1) Change the css classes .yandex, .entry-content
2) Change lang into your language combination.
3) Pay me $100 for the PHP file :) lol, it's right here.
*/
$('.yandex').click(function(e) {
e.preventDefault();
$(this).html('Translating');
var txt = $('.entry-content').html();
var postData = {
"txt" : $('.entry-content').html(),
"lang" : 'nl-de'
}
$.ajax ({
url: ccb_options.ccb_uri+"/inc/yandex.php",
type: 'POST',
data: postData
})
.done(function(html){
$('.yandex').html('Translate');
$('.entry-content').html( html);
})
.error(function(){alert("error");});
});
<?php
/* Use Yandex.ru translation to translate tags
* Template Name: Yandex
* Since 1.0
*/
class yandex {
private $APIkey = "GET ONEM"; //See http://api.yandex.com/translate/doc/dg/reference/translate.xml
public function translate ($text,$lang) {
$t = urlencode($text);
$url = "https://translate.yandex.net/api/v1.5/tr/translate?key=";
$url .= "{$this->APIkey}&format=html&lang={$lang}&text={$t}";
if ($xml = simplexml_load_file($url)) {
return $xml;
}
else return false;
} // yandex_trans
}
$ya = new yandex();
$txt = $_POST['txt'];
$lang = $_POST['lang'];
$trans = $ya->translate($txt, $lang);
echo $trans->text;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment