picolab/translator

Simple translating class with translation providers

0.8 2016-07-28 13:54 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:47:44 UTC


README

Simple translator class with some translate providers

Usage

 $translator = new Picolab\Translator\Translator();
 
 // set up your translating provider from available providers
 $translator->setProvider($translateProvider);
 
 // translate:
 $translation = $translator->from('en')->to('ru')->translate('Some other language');
 
 // You can output the results immediately with echo
 echo $translation;
 // output:
 // Другой язык
 
 $outputResponse = $translation->getResponse();
 //output: 
 /*
 Array
 (
     [code] => 200
     [to] => ru
     [from] => en
     [text] => Array
         (
             [0] => Другой язык
         )
 
 )
 */

// You can use language autodetect feature, if provider is supporting it:
$translation = $translator->to('ru')->translate('Some other language');
// output $translation->getResponse():
/*
  Array
  (
      [code] => 200
      [to] => ru
      [from] => en
      [text] => Array
          (
              [0] => Другой язык
          ) 
  )
  */
 
 // You can even use arrays for translating multiple texts
 $translation = $translator->to('ru')->translate(['Some other language', 'Some other value']);
 // $translation->getResponse():
 /*
   Array
   (
       [code] => 200
       [to] => ru
       [from] => en
       [text] => Array
           (
               [0] => Другой язык
               [1] => другое значение
           )   
   )
   */
// p.s. in this case  echo $translation  will output only first array item   

Providers use Guzzle Http client, if you have specific guzzle client configuration, you can set up with setGuzzleInstance function

 $translator = new Picolab\Translator\Translator();
 
 $translator->setGuzzleInstance($yourGuzzleClientInstance);
 ...
 

Available providers

Microsoft Translator service provider

dev docs: https://msdn.microsoft.com/en-us/library/dd576287.aspx

 $translateProvider = new Picolab\Translator\Providers\BingProvider([
     'client_id' => 'client id',
     'client_secret' => 'client secret',
 ]);
    

Yandex provider

dev docs: https://translate.yandex.com/developers

$translateProvider = new Picolab\Translator\Providers\YandexProvider([
     'key' => 'your api key',
 ]);

Tilde Machine Translation provider

dev docs: http://www.tilde.com/mt/tools/api

Due to the fact that available languages is already defined in the MT system, you do not need to specify them there, but provider class will output system source and target language

$translateProvider = new Picolab\Translator\Providers\LetsMTProvider([
    'client_id' => 'client ID',
    'systemID' => 'System ID',
 ]);
 
$translator = new Picolab\Translator\Translator();
$translator->setProvider($translateProvider);

// Due to the fact that available languages is already defined in the MT system, 
// you do not need to specify them there
// Translation system EN-LV
$translation = $translator->translate('Some other language');
// output first translation 
echo $translation;

Google Translate API provider

dev docs: https://cloud.google.com/translate/docs/

$translateProvider = new Picolab\Translator\Providers\GoogleProvider([
    'api_key' => 'your api key'
]);
 

License: MIT