Laravel package for Google Translate REST API
, (*1)
Package allows to work with Google Translate API, (*2)
Installation
Package can be installed using composer by adding to "require" object, (*3)
"require": {
"pixxet/google-translate": "dev-master"
}
or from console:, (*4)
composer require pixxet/google-translate dev-master
Configuration
You should publish config file to be able to add your Google API key.
To publish config you should do:, (*5)
php artisan vendor:publish --provider="Dedicated\GoogleTranslate\GoogleTranslateProvider"
After config is published, you will have it in config\google-translate.php
of your Laravel project directory, (*6)
You should change only one line:, (*7)
...
/**
* Google key for authentication
*/
'api_key' => 'YOUR-GOOGLE-API-KEY-GOES-HERE',
...
Usage
To translate text with given source language and target language:, (*8)
$translator = new Dedicated\GoogleTranslate\Translator;
$result = $translator->setSourceLang('en')
->setTargetLang('ar')
->translate('Hello World');
dd($result); // "مرحبا بالعالم"
, (*9)
By default language detection is turned on, so you can translate text without specifying source language., (*10)
This will make 2 requests to google API:, (*11)
- First request will go to /detect URL and get source language name
- Second request will make actual translate request and give out result.
$translator = new Dedicated\GoogleTranslate\Translator;
$result = $translator->setTargetLang('ar')
->translate('Hello World');
dd($result); // "مرحبا بالعالم"
You can also use function to only detect text's source language:, (*12)
$result = $translator->detect('Hello World');
dd($result); // "en"
License
This repository code is open-sourced software licensed under the MIT license, (*13)