Translating Twitter, part 2
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 http://www.bing.com/developers/appids.aspx) to be able to use the APIs. The APIs that I am using are fairly simple though.
For a simple translation, requesting http://api.microsofttranslator.com/V2/Http.svc/Translate?appId=[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:
<?php
$apiBase = 'http://api.microsofttranslator.com/V2/Http.svc/';
$appId = 'yourappid';
$text = urlencode( 'Een hoge boom vangt veel wind' );
$language = (string) simplexml_load_string(
file_get_contents(
"{$apiBase}/Detect?appId={$appId}&text={$text}"
)
);
$inEnglish = (string) simplexml_load_string(
file_get_contents(
"{$apiBase}/Translate?appId={$appId}&text={$text}&to=en"
)
);
var_dump( $language, $inEnglish );
?>
with as output:
string(2) "nl" string(34) "A high tree catches a lot of wind."
Comments
Any reason to use simplexml_load_string(file_get_contents()) instead of just simplexml_load_file() ?
- Derick said:
-
I had to resort to using two requests...
By using the GetTranslations service, the detected source language and the translation can be found with one request. Admittedly, it's a little bit messy (needing a POST request) but it saves waiting around for two request/response cycles.
An example, which appears to work, based on the code in your article can be found at https://gist.github.com/1000264 .
@Ilia: No reason, it's just that I debugged with file_get_contents() first.
@Peter: I'll update the article again... I'd missed this API.
Life Line
After my PHP 8.5 in Leeds last night, it's now time to head to Rotterdam to give the same talk there tonight!
It's cold out here, but atleast the snow is now gone.
📷 Avenue Gardens
🚩 Princess Road, London Borough of Brent, United Kingdom
📷 Leafy Entrance
🚩 East Heath Road, London Borough of Camden, United Kingdom
📷 From Green to Yellow.
🚩 St John's Wood Road, City of Westminster, United Kingdom
The Secret Maps exhibition at the British Library is well worth a visit!
We went last Sunday and it still runs to January.
📷 Mirror
🚩 The Terrace, London Borough of Richmond upon Thames, United Kingdom
📷 Green, Red, Orange, and Yellow
🚩 Mortlake High Street, London Borough of Richmond upon Thames, United Kingdom
📷 Leaf
🚩 Lonsdale Road, London Borough of Richmond upon Thames, United Kingdom
I walked 10.6km in 1h47m32s
Merged pull request #1048
Fixed issue #2386: Crashes when running context_get in an exception t…
It's PHP 8.5 release week !
I'm giving two talks on what's new in it, first on Wednesday evening in Leeds: https://www.meetup.com/leedsphp/events/311677834/
And then on Thursday evening in Rotterdam: https://eventy.io/events/q8lmw0v4Will I see you there?
@robinince Loving the new series of the Infinite Monkey Cage so far! #bbc
I walked 1.7km in 18m51s
Updated a clothes shop and a restaurant; Deleted a dentist; Confirmed an estate_agent shop, a dentist, and 2 other objects
I walked 3.4km in 39m19s
I walked 5.7km in 57m30s
📷 Low Thames
🚩 Ranelagh Gardens, London Borough of Hammersmith and Fulham, United Kingdom
I walked 3.3km in 31m40s











Shortlink
This article has a short URL available: https://drck.me/bing-translate-8nq