PHP code example of topaz2 / translator

1. Go to this page and download the library: Download topaz2/translator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

topaz2 / translator example snippets




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

$browser = new Browser();

$clientId = '[YOUR-CLIENT-ID]';
$clientSecret = '[YOUR-CLIENT-SECRET]';

$accessTokenProvider = new AccessTokenProvider($browser, $clientId, $clientSecret);

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



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

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

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

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

$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();
}

$text = 'This is a test';

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

// $detectedLanguage will be 'en'

$text = 'My name is Matthias';

$spoken = $translator->speak($text, 'en', 'audio/mp3', 'MaxQuality');

// $spoken will be the raw MP3 data, which you can save for instance as a file