gabrielesbaiz / google-translate-toolkit
A lightweight helper package to handle translations using Google Translate API.
Fund package maintenance!
Gabriele Sbaiz
Requires
- php: ^8.0
- google/cloud-translate: ^1.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
README
A lightweight helper package to handle translations using Google Translate API.
Original code from JoggApp/laravel-google-translate
Features
- ✅ Basic translation
- ✅ Advanced translation
- ✅ Detect language
- ✅ Batch detect language
- ✅ Batch detect language
- ✅ Batch translation
- ✅ Get available translations for a given text
Installation
You can install the package via composer:
composer require gabrielesbaiz/google-translate-toolkit
You can publish the config file with:
php artisan vendor:publish --tag="google-translate-toolkit-config"
This is the contents of the published config file:
return [ /* * The ISO 639-1 code of the default source language. */ 'default_source_translation' => 'en', /* * The ISO 639-1 code of the language in lowercase to which * the text will be translated to by default. */ 'default_target_translation' => 'en', /* * Api Key generated within Google Cloud Dashboard. */ 'api_key' => env('GOOGLE_TRANSLATE_API_KEY'), ];
Usage
$googleTranslateToolkit = new Gabrielesbaiz\GoogleTranslateToolkit(); echo $googleTranslateToolkit->translate('Hello world');
Using facade:
use GoogleTranslateToolkit; GoogleTranslateToolkit::translate('Hello world');
Available Methods
GoogleTranslateToolkit::detectLanguage(string $text): array
- Detect the language.GoogleTranslateToolkit::detectLanguage(array $texts): array
- Detect the language.GoogleTranslateToolkit::translate(string $text): array
- Translate the given text.GoogleTranslateToolkit::translate(array $texts): array
- Translate the given text.GoogleTranslateToolkit::justTranslate(string $text)): string
- Translate the given text.GoogleTranslateToolkit::getAvailableTranslationsFor('en'): array
- Get all the available translations from 'Google Translation' for a particular language by passing its language code.GoogleTranslateToolkit::unlessLanguageIs('en', string $text): array
- Translate unless the language is same as the first argument. It accepts an optional third argument which is the language code you want the string to be translated in. You can specify the default option in the config file. If the languages are same, the input string is returned as it is, else an array is returned containing the translation results.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.