Library for making calls to the Microsoft Translator V2 API

v3.2 2020-05-17 09:37 UTC

This package is auto-updated.

Last update: 2021-04-23 16:18:46 UTC


By Matthias Noback (maintained by Alayn Gortazar)

Build Status


Using Composer, add to composer.json:

    "require": {
        "matthiasnoback/microsoft-translator": "dev-master"

Then using the Composer binary:

php composer.phar install


This library uses the Buzz browser to make calls to the Microsoft Translator Text API 3.0.

You need to obtain a Microsoft Azure Cognitive Services subscription Key. This can be used to instantiate the AzureTokenProvider:


use Buzz\Browser;
use MatthiasNoback\MicrosoftOAuth\AzureTokenProvider;
use MatthiasNoback\MicrosoftTranslator\MicrosoftTranslator;

$browser = new Browser();


$accessTokenProvider = new AzureTokenProvider($browser, $azureKey);

$translator = new MicrosoftTranslator($browser, $accessTokenProvider);

Selecting azure token provider url

By default the acesstoken will be retrieved from , a third parameter can be passed to the AzureTokenProvider to choose another source.

// Some code

$accessTokenProvider = new AzureTokenProvider($browser, $azureKey, '');

Optional: enable the access token cache

Each call to the translator service is preceded by a call to Microsoft's OAuth server. Each access token however, may be cached for 10 minutes, so you should also use the built-in AccessTokenCache:


use MatthiasNoback\MicrosoftOAuth\AccessTokenCache;
use Doctrine\Common\Cache\ArrayCache;

$cache = new ArrayCache();
$accessTokenCache = new AccessTokenCache($cache);

The actual cache provider can be anything, as long as it implements the Cache interface from the Doctrine Common library.

Making calls

Translate a string

// Some code

$translatedString = $translator->translate('This is a test', 'nl', 'en');

// $translatedString will be 'Dit is een test', which is Dutch for...

Translate a string and get multiple translations

// Some code

$matches = $translator->getTranslations('This is a test', 'nl', 'en');

foreach ($matches as $match) {
    // $match is an instance of MatthiasNoback\MicrosoftTranslator\ApiCall\TranslationMatch
    $degree = $match->getDegree();
    $translatedText = $match->getTranslatedText();

Detect the language of a string

// Some code

$text = 'This is a test';

$detectedLanguage = $translator->detect($text);

// $detectedLanguage will be 'en'


Take a look at the tests to find out what else you can do with the API.

To fully enable the test suite, you need to copy phpunit.xml.dist to phpunit.xml and replace the placeholder values with their real values (i.e. client id, client secret and a location for storing spoken text files).

Build Status

Related projects

There is a MicrosoftTranslatorBundle which makes the Microsoft translator available in a Symfony2 project.

There is also a MicrosoftTranslatorServiceProvider which registers the Microsoft translator and related services to a Silex application.

Differences with V2 library

  • Removed speak method: Speak API has been removed from the Microsoft Translator Text API on v3.0.
  • Removed getTranslations method: GetTranslations functionality has been removed from the Microsoft Translator Text API on v3.0.
  • Removed Datamarket token compatibility: Azure datamarket is not longer available so this has been removed from v3.x


  • Make CachedClient work (now bypasses any SendRequest call)
  • There are some more calls to be implemented, and also some more tests to be added.