Easy to use Deep L API for translation in Laravel projects
Install this package via Composer.
composer require waithaw/deeplapitranslate
You can publish the config file using the following command:
php artisan vendor:publish --provider="WaiThaw\DeeplTranslate\DeeplTranslateServiceProvider"
This will create the package's config file called deepltranslate.php in the config directory. The contents of the published config file are as follows.
return [
/*
|-------------------------------------------------------------------------------
| Api Key generated from Deep L account.
|-------------------------------------------------------------------------------
*/
'auth_key' => env('DEEPL_TRANSLATE_API_KEY'),
/*
|-------------------------------------------------------------------------------
| Api Endpoint URL from Deep L account.
|-------------------------------------------------------------------------------
*/
'api_url' => env('DEEPL_TRANSLATE_API_ENDPOINT_URL'),
];
- After setting up the config file values in .env, you can use translation methods.
use WaiThaw\DeeplTranslate\DeeplTranslate;
$deepl = new DeeplTranslate();
// parameters are $text, $source_language, $target_language
$translated_text = $deepl->translate('are you hungry?', 'EN', 'JA'); // Output translated text string
echo $translated_text;
$languages = $deepl->languages();
foreach($languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
For supported Source-languages
$source_languages = $deepl->languages('source');
foreach($source_languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
For supported Target-languages
$target_languages = $deepl->languages('target');
foreach($target_languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
You can monitor the number of traslated characters and maximun limitations
$usage= $deepl->usage();
echo $usage['character_count'].' characters have been used. Maximum number of characters that can be translated in the current billing period are '.$usage['character_limit'];
You can setup a timeout to prevent waiting in case of api unreachable
$deepl->setTimeout(10);
If you discover any security related issues, please email them to waithawoocw@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see the License File for more information.