PHP code example of potsky / microsoft-translator-php-sdk

1. Go to this page and download the library: Download potsky/microsoft-translator-php-sdk 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/ */

    

potsky / microsoft-translator-php-sdk example snippets




$msTranslator = new MicrosoftTranslator\Client( $configuration );


ranslator = new \MicrosoftTranslator\Client( array(
	'log_level'         => \MicrosoftTranslator\Logger::LEVEL_DEBUG ,
	'api_client_key'     => 'your-client-key' ,
) );

print_r( $msTranslator->translate( 'chair' , 'de' )->getBody() );

Stuhl

print_r( $msTranslator->translate( 'chair' , 'de' , 'fr' )->getBody() );

Fleisch

print_r( $msTranslator->translateArray( array( 'dog' , 'cat' ) , 'fr' )->getBody() );

Array
(
    [dog] => chien
    [cat] => chat
)

print_r( $msTranslator->TransformText( 'WTF I am not here!' , 'en' )->getBody() );

Array
(
    [ec] => 0
    [em] => OK
    [sentence] => WHAT THE HECK I am not here!
)

print_r( $msTranslator->detect( 'The dog is red' )->getBody() );

en

print_r( $msTranslator->detectArray( array( 'The dog is red' , 'Le chien est rouge' ) )->getBody() );

Array
(
    [The dog is red] => en
    [Le chien est rouge] => fr
)

print_r( $msTranslator->getLanguageNames( 'fr' , array( 'en' , 'jp' , 'fr' ) )->getBody() );

Array
(
    [en] => Anglais
    [jp] =>
    [fr] => Français
)

print_r( $msTranslator->getLanguagesForTranslate()->getBody() );

Array
(
    [0] => ar
    [1] => bs-Latn
    [2] => bg
    [3] => ca
    [4] => zh-CHS
    [5] => zh-CHT
    [6] => hr
    [7] => cs
    [8] => da
    [9] => nl
    ...
)

print_r( $msTranslator->breakSentences( 'The dog is red. The cat is blue. The fish is yellow submarine.' , 'en' )->getBody() );

Array
(
    [0] => The dog is red.
    [1] => The cat is blue.
    [2] => The fish is yellow submarine.
)

try
{
	...

}
catch ( \MicrosoftTranslator\Exception $e )
{
	$error = sprintf( "Error #%s with message : %s" , $e->getCode() , $e->getMessage() );
	$msg   = sprintf( "|    %s    |" , $error );
	$line  = str_repeat( '-' , strlen( $msg ) );
	echo sprintf( "%s\n%s\n%s\n" , $line , $msg , $line );
}

try
{
	...
}
catch ( \MicrosoftTranslator\Exception $f )
{
	if ( $f->getCode() === 500 )
	{
		echo sprintf( "Oups, error #%s : %s." , $f->getCode() , $f->getMessage() );
	}
	else
	{
		echo sprintf( "Error #%s : %s\n" , $f->getCode() , $f->getMessage() );
	}
}

$msTranslator = new \MicrosoftTranslator\Client( $config , null , null , $my_logger );

$msTranslator = new \MicrosoftTranslator\Client( $config , $my_http_manager );

$msTranslator = new \MicrosoftTranslator\Client( $config , null , $my_auth_manager );

$msTranslator = new \MicrosoftTranslator\Client( array(
	'guard_type' => 'MyNameSpace\\MyGuard'
) );

$msTranslator->getAuth()->getGuard()->deleteAllAccessTokens();

$msTranslator->getAuth()->getGuard()->cleanAccessTokens();
json
{
    "otsky/microsoft-translator-php-sdk": "*"
    }
}