Translating with Google API

Google provides an AJAX API for translating your text into any language you need.

Since Google does not provide us with their whole language database and algorithms,but somewhere in their AJAX script they are requesting data from their server. Using the location that the script requests the data, we can request the data ourselves.

Belove is a small example using google translation API :-

<?php

function google_translate_api( $your_text, $destination_lang , $source_lang ) {

$your_text = urlencode( $your_text );
$destination_lang = urlencode( $destination_lang );
$source_lang = urlencode( $source_lang );

$trans = @file_get_contents( “http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$your_text}&langpair={$source_lang}|{$destination_lang}” );
$json = json_decode( $trans, true );

if( $json[‘responseStatus’] != ‘200’ )
return false; else return $json[‘responseData’][‘translatedText’];
}
$sourec_text = “Good morning,have a nice day,everything is good here”;
$val = google_translate_api($sourec_text,’ru’,’en’);
echo “<pre>”;
print_r(“<h2>English Text:</h2>”.$sourec_text);
echo “<br>”;
echo “<br>”;
print_r(“<h2>Translated Russian Text:</h2>”.$val);
echo “</pre>”;
?>

Output:

English Text: Good morning,have a nice day,everything is good here
Translated Russian Text:Доброе утро, имеют хороший день, все здесь хорошо
Advertisements

I like to hear from you about this !!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s