Code Monkey home page Code Monkey logo

translator's Introduction

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

translator's People

Contributors

picolablv avatar

Stargazers

Ivars avatar  avatar

Watchers

James Cloos avatar  avatar Ivars avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.