gabrieljmj / translator
Translator to strings using APIs or other things
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2025-01-16 03:45:34 UTC
README
![Gitter](https://badges.gitter.im/Join Chat.svg)
Translator to texts using Web Services or other things that can do this action. ##Install ###Composer
{ "require": { "gabrieljmj/translator": "dev-master" } }
##Needs
- cURL extension to do the web services requests.
##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.
- Go to API key request form.
- After create, go to My keys.
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')