Stars: 162
Forks: 49
Pull Requests: 18
Issues: 0
Watchers: 14
Last Updated: 2023-09-01 19:08:58
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
License: MIT License
Languages: PHP
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
composer require joggapp/laravel-google-translate
The package will automatically register itself.
We have documented how to setup the project and get the necessary configurations from the Google Cloud Platform console in a step by step detailed manner over here.
You can publish the config file using the following command:
php artisan vendor:publish --provider="JoggApp\GoogleTranslate\GoogleTranslateServiceProvider"
This will create the package's config file called googletranslate.php
in the config
directory. These are the contents of the published config file:
return [
/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
|----------------------------------------------------------------------------------------------------
*/
'default_target_translation' => 'en',
/*
|-------------------------------------------------------------------------------
| Path to the json file containing the authentication credentials.
|
| The process to get this file is documented in a step by step detailed manner
| over here:
| https://github.com/JoggApp/laravel-google-translate/blob/master/google.md
|-------------------------------------------------------------------------------
*/
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
];
After setting up the config file values you are all set to use the following methods 😄
Detecting the language. You can pass both, a single string or an array of multiple strings to it:
GoogleTranslate::detectLanguage('Hello world'): array
GoogleTranslate::detectLanguage(['Hello world', 'Laravel is the best']);
// Returns multi-dimensional array containing result set for all the array elements.
translate
method accepts a third optional argument which can be the code of the language you want the string to be translated in. You can specify the default option in the config file:// GoogleTranslate::translate($input, $from = null, $to = null, $format = 'text'): array
GoogleTranslate::translate('Hello world'): array
GoogleTranslate::translate(['Hello world', 'Laravel is the best']);
// Returns multi-dimensional array containing result set for all the array elements.
GoogleTranslate::getAvailableTranslationsFor('en'): array
GoogleTranslate::unlessLanguageIs('en', string $text);
GoogleTranslate::justTranslate(string $text): string
GoogleTranslate::unlessLanguageIs('en', string $text, string $format);
@translate
that comes with the package to make its use more neat in the view files. It accepts an optional second argument which is the language code you want the string to be translated in. You can specify the default option in the config file.@translate('Hello World')
You can run the tests with:
vendor/bin/phpunit
Please see the CHANGELOG for more information about what has changed recently.
If you discover any security related issues, please email them to [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see the License File for more information.