pollora / pollingo
A PHP package for translating strings using AI
Requires
- php: ^8.2|^8.3
- nyholm/psr7: ^1.8
- openai-php/client: ^0.8.0
- patrickschur/language-detection: ^5.0
- symfony/http-client: ^6.0|^7.0
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- laravel/pint: ^1.18.1
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0
- peckphp/peck: ^0.1.1
- pestphp/pest: ^3.5.1
- pestphp/pest-plugin-type-coverage: ^3.1
- phpstan/phpstan: ^1.12.7
- rector/rector: ^1.2.8
- symfony/var-dumper: ^7.1.6
README
๐ Pollingo
A framework-agnostic PHP package for translating groups of strings using OpenAI.
๐ Features
- โจ Fluent API for submitting groups of strings to translate
- ๐ค Simple API for translating single strings
- ๐ค OpenAI-powered smart, contextual translations
- ๐ Support for custom translation providers
- ๐ Global and per-string context support
- ๐ค Support for all ISO 639-1 language codes
- ๐ฆ Framework-agnostic with Laravel integration
๐ฅ Installation
Standalone Installation
composer require pollora/pollingo
Laravel Installation
The package will automatically register itself if you're using Laravel's package discovery. After installation, you can publish the configuration file:
php artisan vendor:publish --tag=pollingo-config
๐ง Configuration
Standalone Configuration
Set your OpenAI API key in your environment:
OPENAI_API_KEY=your-api-key
Laravel Configuration
In your .env
file:
OPENAI_API_KEY=your-api-key OPENAI_MODEL=gpt-4
You can also customize the configuration in config/pollingo.php
after publishing it.
๐ Basic Usage
Single String Translation
use Pollora\Pollingo\Pollingo; // Simple translation $translation = Pollingo::make('your-openai-api-key') ->from('en') ->to('fr') ->text('Welcome to our application') ->translate(); // Translation with context $translation = Pollingo::make('your-openai-api-key') ->from('en') ->to('fr') ->text('Welcome to our platform!') ->context('Used in the subject of a welcome email.') ->translate();
Group Translation
use Pollora\Pollingo\Pollingo; $translations = Pollingo::make('your-openai-api-key') ->from('en') ->to('fr') ->group('messages', [ 'welcome' => 'Welcome to our application', 'goodbye' => 'Goodbye!', ]) ->translate();
Laravel Usage
Using the Facade:
use Pollora\Pollingo\Facades\Pollingo; $translations = Pollingo::from('en') ->to('fr') ->group('messages', [ 'welcome' => 'Welcome to our application', 'goodbye' => 'Goodbye!', ]) ->translate();
Using Dependency Injection:
use Pollora\Pollingo\Pollingo; class TranslationController { public function __construct( private readonly Pollingo $pollingo ) {} public function translate() { return $this->pollingo ->from('en') ->to('fr') ->group('messages', [ 'welcome' => 'Welcome to our application', ]) ->translate(); } }
๐ Language Support
Pollingo supports all ISO 639-1 language codes. Here are some common examples:
- ๐บ๐ธ English:
en
- ๐ซ๐ท French:
fr
- ๐ช๐ธ Spanish:
es
- ๐ฉ๐ช German:
de
- ๐ฎ๐น Italian:
it
- ๐ฏ๐ต Japanese:
ja
- ๐จ๐ณ Chinese:
zh
- ๐ท๐บ Russian:
ru
Example with language codes:
$pollingo ->from('en') // Source language (English) ->to('fr') // Target language (French) ->translate();
๐งฉ Advanced Features
Groups and Context
You can organize your translations into logical groups and provide context:
$pollingo->group('emails', [ 'welcome' => [ 'text' => 'Welcome to our platform!', 'context' => 'Used in the subject of a welcome email.' ], 'greeting' => [ 'text' => 'Hi {name}', 'context' => 'Personal greeting with user name placeholder' ] ]);
Custom Translators
While OpenAI is the default translation provider, you can implement your own translator by implementing the Translator
interface:
use Pollora\Pollingo\Contracts\Translator; class MyCustomTranslator implements Translator { public function translate( array $groups, string $targetLanguage, ?string $sourceLanguage = null, ?string $globalContext = null ): array { // Your custom translation logic here } }
You can then use your custom translator in two ways:
- Using the fluent
withTranslator()
method:
$pollingo = Pollingo::make() ->withTranslator(new MyCustomTranslator()) ->from('en') ->to('fr') ->translate();
- Using the
make()
method'stranslator
parameter:
$pollingo = Pollingo::make(translator: new MyCustomTranslator());
This is particularly useful when you want to:
- Use a different translation service (DeepL, Google Translate, etc.)
- Implement custom translation logic
- Mock translations for testing
- Cache translations
- Implement fallback mechanisms
Global Context
You can provide global context that applies to all translations:
$pollingo ->withGlobalContext('This is for a professional business application') ->group('auth', [ 'login' => 'Log in', 'register' => 'Create an account', ]) ->translate();
Multiple Groups
You can translate multiple groups in a single request:
$translations = $pollingo ->from('en') ->to('fr') ->group('ui', [ 'save' => 'Save', 'cancel' => 'Cancel', ]) ->group('messages', [ 'success' => 'Operation completed successfully', 'error' => 'An error occurred', ]) ->translate();
๐ ๏ธ Laravel Integration Features
Configuration
The package provides a configuration file that can be customized:
// config/pollingo.php return [ 'openai' => [ 'api_key' => env('OPENAI_API_KEY'), 'model' => env('OPENAI_MODEL', 'gpt-4'), ], ];
Service Container
The package registers both the main service and the translator interface in Laravel's service container:
use Pollora\Pollingo\Contracts\Translator; class TranslationService { public function __construct( private readonly Translator $translator ) {} }
๐งช Testing
composer test
๐ค Contributing
Please see CONTRIBUTING.md for details.
๐ License
The MIT License (MIT). Please see License File for more information.