Polyglot provides a simple way to access a multitude of cloud machine translation services. It is designed to be easily extensible, allowing you to add new drivers as needed.
Currently supported drivers:
- Stichoza Google Translate PHP Package
- Google Cloud Translation API (default to v2)
- Amazon Translate
- OpenAI
You can install the package via composer, and it will automatically register itself.
composer require plank/polyglot
Afterward, you can publish the config file with:
php artisan vendor:publish --tag="polyglot-config"
If you simply need to translate a piece of text, this package provides a few convenient methods to do so.
-
Using the macro on laravel's
Str
class or via the helper functionstr()
> Str::translate('Hello!', 'fr'); = "Bonjour!" > str('hi')->translate('fr'); = "Salut"
-
Using the facade, you can access any of your configured translators and translate text between languages
Polyglot::translator('google')->from('en')->to('fr')->translate('Hello!'); Polyglot::translator('amazon')->translateTo('Hello!', target: 'fr', source: 'en');
-
Using the stack translator, you can chain multiple services as fallbacks in case one or more of them fail
Polyglot::stack('google', 'amazon')->from('en')->to('fr')->translate('Hello!'); Polyglot::stack('google', 'amazon')->translateTo('Hello!', 'fr');
A stack can also be configured as a translator in your
/config/polyglot.php
.'translators' => [ 'stack' => [ 'driver' => 'stack', 'translators' => ['amazon', 'google', 'gpt'], 'retries' => 3, 'sleep' => 100, ], ... ]
> Polyglot::translator('stack')->from('auto')->to('it')->translate('Hello!'); = "Ciao!"
Translator::translateBatch
- Translate an array of strings, set target language withto
methodTranslator::translateBatchTo
- Translate an array of strings to a specific languageAbstractTranslator::to
,from
,format
- Fluent API for setting translator optionsAbstractTranslator::languages
- Get a list of supported languages, if locale is passed it will return the supported languages for that localesendTranslateRequest
- Some drivers allow you to access the underlying response from the translation service
You can extend polyglot and provide your own translator drivers. To do so, follow these steps:
-
Create a new class that extends
Plank\Polyglot\Contracts\AbstractTranslator
and implement the missing methods specified by thePlank\Polyglot\Contracts\Translator
interface. -
Then, in a service provider, call the
Plank\Polyglot\Polyglot::extend
method to register your new driver.Polyglot::extend('azure', static function (Container $app, array $config) { $key = $config['key']; return new AzureTranslator($key); });
-
Now you can add a new translator with the driver you just created by adding it to the
config/polyglot.php
file:return [ 'default' => env('POLYGLOT_DEFAULT', 'bing'), 'translators' => [ 'bing' => [ 'driver' => 'azure', 'key' => env('BING_TRANSLATE_API_KEY'), ], ... ], ];
-
And finally use it in your code:
Polyglot::translator('bing')->from('en')->to('fr')->translate('Hello!');
composer test
The MIT License (MIT). Please see License File for more information.
If you discover a security vulnerability within siren, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.
Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users. We're committed to innovation, inclusivity, and sustainability in the digital space. Learn more about our mission to improve the web.