All Projects → gabrieljmj → Translator

gabrieljmj / Translator

Licence: other
Text translator with web services

Programming Languages

PHP
23972 projects - #3 most used programming language

Translator

![Gitter](https://badges.gitter.im/Join Chat.svg) Total Downloads Latest Unstable Version License Scrutinizer Code Quality

Translator to texts using Web Services or other things that can do this action. ##Install ###Composer

{
  "require": {
    "gabrieljmj/translator": "dev-master"
  }
}

##Needs

##Support ###Google Translate - Not tested To use the Google Translate, it is necessary an API Key. To create one, follow this steps:

  • Go to the Google Developers Console.
  • Select a project, or create a new one.
  • In the sidebar on the left, select APIs & auth. In the list of APIs, make sure the status is ON for the Google Translate API.
  • In the sidebar on the left, select Credentials.

How to instance: new \Translator\Service\GoogleTranslate(\Translator\Http\RequestInterface $request, string $apiKey)

###Yandex Translate - Tested To use, also is necessary an API Key.

How to instance: new \Translator\Service\YandexTranslate(\Translator\Http\RequestInterface $request, string $apiKey)

##How to use Examples with Google Translate ###Getting accepted languages of a web service Use the method getAcceptedLangs(). It will return an array with all languages accepted by web service that you are using.

###Translating

use Translator\Service\GoogleTranslate;
use Translator\Http\CurlRequest;

$text = 'Hi! How are you?';
$apiKey = 'YOUR_API_KEY';

$translator = new GoogleTranslate(new CurlRequest(), $apiKey);
$translatedText = $translator->translate('en', 'pt', $text);
$translatedText->getNewText();//'Oi! Como vai você?'
$translatedText->getOriginalText();//'Hi! How are you?'
$translatedText->getOriginalLang();//en
$translatedText->getNewLang();//pt

With an array:

$texts = array('Hi!', 'How are you?');

$translatedText = $translator->translate('en', 'pt', $texts);
$translatedText->getNewText();//Array('Oi!', 'Como vai você?')
$translatedText->getOriginalText();//Array('Hi!', 'How are you?')
$translatedText->getOriginalLang();//en
$translatedText->getNewLang();//pt

###Detecting language

$detectedText = $translator->detect($text);
$detectedText->getLang();//en
$detectedText->getText()//Hi! How are you?
$detectedText->getDetectedTextWithLang();//Array('Hi! How are you?' => 'en')

With an array:

$texts = array('Hi!', 'Olá!');

$detectedText = $translator->detect($texts);
$detectedText->getLang();//Array('en', 'pt')
$detectedText->getText();//Array('Hi!', 'Olá!')
$detectedText->getDetectedTextWithLang();//Array('Hi!' => 'en', 'Olá!' => 'pt')
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].