If you don’t care about the story, here is the file, eat your own dog food to install. ( some screen shots here TwitterFox + gTranslate)

—-

After attending Barcamp Bangkok #3 , I have added some new twitter friends all over South East Asia.

In order to read their tweet, which may be in Thai, Vietnamese or whatsoever language I don’t know or that I am not quite familiar with ( such as Japanese … ) , I use the build-in translation function in Tweetdeck / Nambu in MacOS.

Problem arise when I switch to a PC, in which I am used to tweet with Twitterfox. Twitterfox does not yet have embedded translation function. Although Tweetdeck can be installed into Windows, windows itself sucks enough. I don’t what to open one more app and kick start the AIR runtime. Another reason is that Twitterfox is a firefox plug-in, I can use it in Windows, MacOS, Linux and even BSD desktop and keep similar ( err…. ) working experience.

So I decided to h… Hey! I decided to look at the change-log first .

Don’t reinvent the wheel before you hack things, that sucks.

But what I had found is the Naan Studio guys in behind, are shifting their focus on TwitterFon, another piece of software, or an iPhone App, that I use a lot. Peer comments also reveals that TwitterFox is not to be made complicated.

OK, time to hack.

First, most firefox plug-ins are just javascript and XUL files in a zipped archive. Just find out the directory path and unzip files in chrome, most likely you can read and edit the source. For my TwitterFon, the file path is somewhat like:

W:\Document and Settings\%USER%\Application Data\Mozilla\Firefox\Profiles\%your.default%\extensions\twitternotifier@naan.net\chrome

I just use Windows search to find TwitterFox.jar , and that is it.

If you have installed firefox and twitterfox, just want and dare to test my crap hack, just download my file and replace the original one. Needless to say, you don’t back up, you bare your own blame.

For me, I unzip it with 7zip , and check out what can I do.

Obviously, “content\twitternotifier.js” should be the main file for fun.

What I want to do is to

1. Add a new item to the context menu ( right click ) , like the retweet stuff.
2. Use Google translate API ( RESTful part ) to do the job.
3. make the translated result usable for retweet
4. auto detect original language ( google api takes care about it ) and user preferred language ( as set in browser )

It turns out more files are involved, including i18n thingies, for point 1, which I spend most of the time to clean up tonight.

For point 2, just read Google’s document. It is by far easier than what I used to work with ( babelfish… ) 6 years ago. I add this follow code within 30 minutes, most time are wasted for my many many bugs and typos and thus firefox restarts.

( this is added just after the retweet function )

translate: function(target) {
var tweet = target.parentNode.node;
/*I don't care what "target" is, the code in above tells me how to get the "tweet". */
var req = new XMLHttpRequest;
/* Ajax translation kind of stuff */
req.open('GET', GTranslateAPI + tweet.getAttribute('text') ,true);
/* constants have be defined in the header */
req.onreadystatechange = function(){
if(req.readyState == 1)
req.setRequestHeader('Referer', Referrer);
/* google requires developers to state the referrer, although the function would work without it*/
if(req.readyState != 4) return;
result=eval('('+req.responseText+')');
/* json stuff */
r=result.responseData.translatedText;
/* fixit: yeah I am super lazy, even there are some available functions to normalize the result, I do nothing */
t=document.createTextNode(r);
p=document.createElement('description');
p.className = "twitternotifier-message-translate";
/* add some styling, thus the CSS file is also changed */
p.appendChild(t);
tweet.appendChild(p);
/* fixit: this part is definitely sick, may be CSS problem exists */
tweet.setAttribute('text',r);
/* How DIRTY ! :DDD , translate and retweet result is that easy (3) */
};
req.send('');
},

For point 4, just read the setting of the browser is not enough. Google translate does not know ALL languages.
Even worse, it takes only first two letter in locale for most langauge. Chinese is the only exception. The worst thing ture out to be zh-HK, which I am suppose to support even though I don’t like to use, is not supported by Google Translate. Thus this is the REAL Hack, if anything in this post means to be a hack :

( in the header )

var tolang;
switch(navigator.language) {
case 'zh-TW':
case 'zh-HK':
tolang='zh-tw';
break;
default:
tolang=navigator.language.substr(0,2);
}
const GTranslateAPI = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=%7C'
+ tolang + '&q=';
const Referrer = 'http://twitterfox.net/';

And that’s it .

Wait, I have also changed the behavior of Retweet , from
RT: @someone:
to
RT @someone

What is the point for adding two colons ? Funny ? Waste two more precious digit space ?

Moreover, I add a “zh-HK” locale.
Otherwise, HK users ( not me ) will be depreciated to be broken Chinese (破體中文) readers.

( Doh!)

By the way, I know there are many many bugs as you can see in the above code snippets ( at least look at the Fixits ).
I don’t care too much, but I may update if have time.

Hope that the Naan studio guys will read this post, throw away my crap, and implement something really works.

Before that, my dog food might be good enough for you darn lazy geeks.
You can always hack it better. I hope you will notice me after tasting your own shit.

The above code is a damned deviate (dd). Never attribute me if you copy and distribute. Yet I don’t care if you make it even more horrible and throw me some tipjoy to fix it. I will NOT fix it for you. I will fix it perhaps for my own pleasure.

If your firefox is corrupted by my file, please feel free to shout out your foul words to @bencrox.