Skip to content
/ polyglot Public

simple laravel translation package. supports common service providers: google, amazon, openai

License

Notifications You must be signed in to change notification settings

plank/polyglot

Repository files navigation

PHP Version Support PHP Version Support GitHub Workflow Tests Status

Laravel Polyglot

⚠️ Package is under active development. Wait for v1.0.0 for production use. ⚠️

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

Installation

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"

Usage

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 function str()

    > 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!"

Other Methods

  • Translator::translateBatch - Translate an array of strings, set target language with to method
  • Translator::translateBatchTo - Translate an array of strings to a specific language
  • AbstractTranslator::to, from, format - Fluent API for setting translator options
  • AbstractTranslator::languages - Get a list of supported languages, if locale is passed it will return the supported languages for that locale
  • sendTranslateRequest - Some drivers allow you to access the underlying response from the translation service

Custom Drivers

You can extend polyglot and provide your own translator drivers. To do so, follow these steps:

  1. Create a new class that extends Plank\Polyglot\Contracts\AbstractTranslator and implement the missing methods specified by the Plank\Polyglot\Contracts\Translator interface.

  2. 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);
    });
  3. 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'),
            ],
            ...
        ],
    ];
  4. And finally use it in your code:

    Polyglot::translator('bing')->from('en')->to('fr')->translate('Hello!');

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.

Security Vulnerabilities

If you discover a security vulnerability within siren, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.

Check Us Out!

 

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.

About

simple laravel translation package. supports common service providers: google, amazon, openai

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages