News Archive
PhpRiot Newsletter
Your Email Address:

More information

Translating Twitter, part 2

Note: This article was originally published at Planet PHP on 31 May 2011.
Planet PHP

Translating Twitter, part 2

London, UK Tuesday, May 31st 2011, 09:04 BST

A while ago I wrote in an article about translating tweets in my client Haunt. For the translating itself I was using the Google Translate API, which has sadly be deprecated. Evil after all I suppose.

I've now rewritten my translation code to use the Bing Translation APIs instead. You need to register an API key (see to be able to use the APIs. The APIs that I am using are fairly simple though.

For a simple translation, requesting[yourappid]&to=en&text=[yourtext] is all you need to do. It will auto-detect the language for you as well.

However, it does not return the detected language, so I had to resort to using two requests in order to reimplement the same functionality that I had before with the Google APIs. I also found that it was easier to use the Http and not the Ajax variant of the API. It requires using SimpleXML to get to the data, but at least you do not have to fight with the BOM (Byte-order mark) and quoting.

The full code looks like:

with as output:

string(2) "nl" string(34) "A high tree catches a lot of wind."