Microsoft Bing Translation PHP wrapper


Microsoft Language translation is an interesting service. Not only can you do language translation, you can also detect the language of a particular text. The given class provides a PHP wrapper which will help developers translate text from one language to another in a easy manner. The library also supports caching, helping you keep your translations fast and simple.

appId

You need a Bing AppId to use the translation class, which you can procure form the Microsoft site. Once you get your AppId you need to inititlise the class with the AppId as below.

require_once('./BingTranslateLib/BingTranslate.class.php');
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');

Installation

Download Microsoft Bing PHP Wrapper
Downloads : 4635 / File size : 11 kB

Check usage policies and pricing here.

After you have downloaded the library, add a reference to the translator class as shown below:

require_once('./BingTranslateLib/BingTranslate.class.php');

The Translation class used cURL to access the Microsoft Bing translation service, so you need to have that installed. A quick way to check if the class is working correctly is to use the ‘selfTest’ method of the class.

require_once('./BingTranslateLib/BingTranslate.class.php');
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
$gt->selfTest();

Which will print ‘Translation test Ok.’ if everything is working fine. If cURL is not installed on your system, check your php.ini settings, you should have the following line in your php.ini file.

extension=php_curl.dll

Translating a text string

Once you are sure that the class is working properly, you can use the following simple example to translate a piece of text from one language to another.

require_once('./BingTranslateLib/BingTranslate.class.php');
 
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
 
/* Text to translate */
$text = 'hello world';
 
/* Translate from "English" to "French" */
$translated_text = $gt->translate($text, "en", "fr");
 
echo $translated_text;
 
/* This should print 'Salut tout le monde' */

The first parameter of the ‘translate()’ method takes the text to translate, the second and third parameters take the target language to translate from and the language to translate to, respectively.

Caching

The class also has the facility to cache the results, so if you post a text for translation and it is already in the cache the cached results will be returned instead of going out to Bing for translation, this helps decrease translation time. The cache can be enabled or disabled as below. The cache is disabled by default.

require_once('./BingTranslateLib/BingTranslate.class.php');
 
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
 
/* Enable the cache */
$gt->cacheEnabled(true);

Make sure that the ‘cache’ directory is there in the directory where your php file is located. If you need to locate the ‘cache’ directory somewhere different than you can change it in the ‘BingTranslate.class.php’ file. Locate the following line in the file, and then change the directory path to the one you want.

private $_cache_directory = './cache/';

Language Names

The ‘LanguageNames()’ method Retrieves friendly names for the languages passed in as the parameter languageCodes, and localized using the passed locale language.

require_once('./BingTranslateLib/BingTranslate.class.php');
 
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
 
/* Return the language names supported in Chinese, 
   make sure you set the correct headers if you are echoing 
   it to the browser. 
 */
 
$names = $gt->LanguageNames("zh-CHS");
 
header('Content-Type: text/html; charset=utf-8'); 
echo $names;

Languages Supported

The ‘LanguagesSupported()’ method obtains a list of language codes representing languages that are supported by the Translation Service. translate() can translate between any two of these languages.

require_once('./BingTranslateLib/BingTranslate.class.php');
 
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
$languages_supported = $gt->LanguagesSupported();
print_r($languages_supported);

Converting translation to audio

The ‘Speak()’ method allows you to save your tralslation as an audio to a wav file, speaking the passed-in text in the desired language. Remember, when you echo the audio to the browser you set the correct headers as shown below.

require_once('./BingTranslateLib/BingTranslate.class.php');
 
$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
 
/* Speak the text given in French */
$audio = $gt->Speak("If you're designing a web application", "fr");
 
header('Content-Type: audio/wav'); 
echo $audio;

If you need to save the audio to a file instead, you can supply a filename, where the audio will be saved. The audio will not be directed to the browser.

/* Speak the text in French, and save it to a file called 'audio.wav' */
$audio = $gt->Speak("What a wonderful world", "fr", "audio.wav");

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

12 Responses

1

Marcin

September 19th, 2011 at 6:36 am

The translation service is only available as 120 days trial. It would be nice of you to include this information in the beginning.

2

simon

October 1st, 2011 at 11:07 am

just what I was looking for – thanks!

3

Draco

October 12th, 2011 at 5:26 am

This is awesome
Thanks for the tutorial :D

4

Leon

October 31st, 2011 at 7:21 am

@Sameer – Many thanks for this nice time saving wrapper :)

@Marcin – The way I read it, is that a free subscription gives you 2,000 transactions a month. If someone sees this differently please fill me in??

5

Adrian

November 5th, 2011 at 11:57 pm

Can this auto detect languages?

6

Adrian

November 6th, 2011 at 12:05 am

I figured out, I did not see the detectLanguage function.

$text_lang = $gt->detectLanguage($text);

7

domger

November 6th, 2011 at 3:53 am

since there is a limitation on the string sent via curl/get, it would be nicer to send via post in order to translate a bigger text including more html and so on. Do you have experience on or suggestions to this ?

8

Daniele

November 27th, 2011 at 10:40 am

The cache is not working properly when multiple languages are used. The fix is :

$string_signature = md5($this->_text);

to

$string_signature = md5($this->_text . $this->_toLang . $this->_fromLang);

9

Dipen

November 13th, 2012 at 10:56 pm

how can i create the appId???

10

Aditya

January 19th, 2013 at 8:37 pm

Hi , Thanks for this great script , any info how many search query bing allow per day ?

11

tresloukadu

January 20th, 2013 at 10:53 am

Good, but it does not work more in this way. Please if you are visiting this post check this fresh class which works pretty good: http://code.google.com/p/micrsoft-translator-php-wrapper/

12

Umar

November 6th, 2014 at 11:10 am

Sir how can i detect language by simple click please tell me the code

Your thoughts

Sign up for fresh content in your email