Created
November 29, 2014 00:27
-
-
Save kamiel79/1f958accbb8ac3f5366c to your computer and use it in GitHub Desktop.
Yandex over AJAX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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");}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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