2017 © Pedro Peláez
 

library microsoft-translator

Library for making calls to the Microsoft Translator V2 API

image

matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  • Friday, April 6, 2018
  • by matthiasnoback
  • Repository
  • 4 Watchers
  • 29 Stars
  • 28,445 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 20 Forks
  • 1 Open issues
  • 13 Versions
  • 9 % Grown

The README.md

PHP library for the Microsoft Translator V3 API

By Matthias Noback (maintained by Alayn Gortazar), (*1)

Build Status, (*2)

Installation

Using Composer, add to composer.json:, (*3)

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

Then using the Composer binary:, (*4)

php composer.phar install

Usage

This library uses the Buzz browser to make calls to the Microsoft Translator Text API 3.0., (*5)

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

<?php

use MatthiasNoback\MicrosoftOAuth\AzureTokenProvider;
use MatthiasNoback\MicrosoftTranslator\MicrosoftTranslator;

use Buzz\Browser;
use Buzz\Client\Curl;
use Nyholm\Psr7\Factory\Psr17Factory;


$client = new Curl(new Psr17Factory());
$browser = new Browser($client, new Psr17Factory());

$azureKey = '[YOUR-AZURE-SUBSCRIPTION-KEY]';

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

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

Selecting azure token provider url

By default the acesstoken will be retrieved from https://api.cognitive.microsoft.com/sts/v1.0/issueToken , a third parameter can be passed to the AzureTokenProvider to choose another source., (*7)

<?php
// Some code

$accessTokenProvider = new AzureTokenProvider($browser, $azureKey, 'https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken');

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:, (*8)

<?php

use MatthiasNoback\MicrosoftOAuth\AccessTokenCache;
use Cache\Adapter\PHPArray\ArrayCachePool;

$cache = new ArrayCachePool();
$accessTokenCache = new AccessTokenCache($cache);
$accessTokenProvider->setCache($accessTokenCache);

The actual cache provider can be anything, as long as it implements the CachePoolInterface interface from FIG-PSR6, (*9)

Making calls

Translate a string

<?php
// 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

<?php
// 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

<?php
// Some code

$text = 'This is a test';

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

// $detectedLanguage will be 'en'

Tests

Take a look at the tests to find out what else you can do with the API., (*10)

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)., (*11)

Build Status, (*12)

TODO

  • There are some more calls to be implemented, and also some more tests to be added.

The Versions

06/04 2018

dev-master

9999999-dev http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

06/04 2018

v0.6.1

0.6.1.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

30/01 2017

v0.6.0

0.6.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

18/06 2016

v0.5.0

0.5.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

25/11 2015

v0.4.4

0.4.4.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

26/10 2014

v0.4.3

0.4.3.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

29/01 2014

v0.4.2

0.4.2.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

The Development Requires

translate bing microsoft translator

29/01 2014

v0.4.1

0.4.1.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator

29/01 2014

v0.4.0

0.4.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator

29/01 2014

v0.3.1

0.3.1.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator

29/01 2014

v0.3.0

0.3.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator

16/07 2013

v0.2.0

0.2.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator

16/12 2012

v0.1.0

0.1.0.0 http://github.com/matthiasnoback/microsoft-translator

Library for making calls to the Microsoft Translator V2 API

  Sources   Download

MIT

The Requires

 

translate bing microsoft translator