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
Merged pull request #1029
Reflow some comments
Add comments, add end of file newlines, fix php 8.5 compilation
Benchmark Xdebug performance
Merged pull request #1051
PHP 8.6: printf() is now optimised out if it only uses %s and %d (and…
PHP 8.6: The object IDs of objects changed in tests
PHP 8.6: ZSTR_INIT_LITERAL() no longer takes non-literals, so use zen…
PHP 8.6: WRONG_PARAM_COUNT has been removed in favour of zend_wrong_p…
PHP 8.6: zval_dtor() has been deprecated in favour of zval_ptr_dtor_n…
Update test for version constraints, as well as the error messages
I walked 7.4km in 1h38m15s
I just watched an interesting and lovely documentary on BBC Four: The Magical World of Moss.
It's on the iPlayer for another month:
https://www.bbc.co.uk/programmes/m001hqthI walked 8.2km in 1h26m34s
Merge branch 'xdebug_3_5'
Document to use PIE instead of PECL in the README
I walked 9.2km in 1h45m44s
I walked 10.1km in 1h40m23s
If we must show off a little…
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nyc


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