Twitter.translate Example of How YQL Can Solve a Problem

At the Open Hack Day in Brazil earlier this month, I was stuck with a dilemma. We offered to help people with their hacks and asked them to use Twitter for their questions. But most of the questions were actually posed in Portuguese rather than English, French, or German. I don't speak Portuguese, so I was stuck.

We were showcasing the Yahoo! Query Language (YQL) as the main ingredient of awesomeness for hacks built in 24 hours, so I thought I would use it to solve this issue. The twitter.translate table provides exactly what I needed: It searches Twitter for a specific word and adds a translation to the results. Here is an example of the effect in action:

Twitter translations embedded in a twitter stream

How it works

I used the Twitter Search API in conjunction with the Google Translation API. Both get embedded in an execute block of the twitter.translate table:

var out = '';
// make the query to Twitter
var tweets = y.query('select * from json where
// loop through the results
var rows = tweets;
for each (var result in rows.results){
// replace backslashes
var text = result.text.toString();
var text = text.replace(/\/,'');
// call the Google API
var trans = y.query('select * from google.translate where q="'+
text+'" and target="'+language+'"');
// replace backslashes
var trans = trans.results.translatedText.toString();
var trans = trans.replace(/\/,'');
// add a translatedText element to each result
result.trans +=
// return to YQL
response.object = rows;

This adds the translatedText element to each result of the Twitter search. I can then use, for example, PHP to display the results:

// the YQL statement
$yql = 'select * from twitter.translate where '.
'search="brhackday" and language="en" and amount=30';
echo '<pre><code>'.$yql.'</code></pre>';
// call the YQL endpoint via cURL
echo '<p><a href="'.
'alltableswithkeys">Try this in the console</a></p>';
$url = ''.
$all = json_decode(get($url));
// for debugging
echo '<!-- ';
echo '-->';
// loop over results, if they are available and show them as an
// unordered list
echo '<ul>';
foreach($all->query->results->results as $r){
echo '<li><div><img src="'.$r->profile_image_url.'" alt="">'.
'<p><strong>English:</strong> '.$r->translatedText.'</p>'.
echo '</ul>';
function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return $output;

Add a form and some validation code, and you have a translating search interface for Twitter.

Have a go at execute blocks in your own tables. It is amazing how many things you can make the YQL server do for you.

Chris Heilmann @codepo8
Yahoo! Developer Network