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.


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.

$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');


Download Microsoft Bing PHP Wrapper
Downloads : 5606 / 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:


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.

$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');

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.


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.

$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.


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.

$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
/* Enable the cache */

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.

$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.

$gt = new BingTranslateWrapper('YOUR_BING_APPID_HERE');
$languages_supported = $gt->LanguagesSupported();

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.

$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");

13 Responses

  1. Marcin says:

    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 says:

    just what I was looking for – thanks!

  3. Draco says:

    This is awesome
    Thanks for the tutorial :D

  4. Leon says:

    @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 says:

    Can this auto detect languages?

  6. Adrian says:

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

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

  7. domger says:

    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 says:

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

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


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

  9. Dipen says:

    how can i create the appId???

  10. Aditya says:

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

  11. tresloukadu says:

    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:

  12. Umar says:

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

  13. mohan kumar says:

    i am unable to get the service,how can i get the solution its very urgent