Skip to content

laravel-google-informal-icu-i18n-translate ​

Adds the free Google Translate provider (eugene-erg/google-informal-icu-i18n-translator) to the Laravel translator (eugene-erg/laravel-icu-i18n-translate). Install it and the translator fills missing translations through Google. No API key is needed.

The informal endpoint has no SLA and can change or rate-limit at any time. Use it for development and drafts, and put a reliable provider before it for production.

bash
composer require eugene-erg/laravel-google-informal-icu-i18n-translate
php artisan migrate   # the tables of laravel-icu-i18n-translate

Requires PHP 8.3+, Laravel 13 and ext-intl. Both service providers are discovered automatically.

php
use EugeneErg\IcuI18nTranslator\Translator;

$translator = app(Translator::class);

$translator->translateMessage('Hello, {name}!', ['name' => 'Ivan'], toLocale: 'ru', fromLocale: 'en');
// "Привет, Ivan!": translated once, then read from the database

Files, formats and everything else are described in laravel-icu-i18n-translate and the core translator.

Configuration ​

The endpoint, for a proxy (config/services.php):

php
'google_informal_translate' => [
    'url' => env('GOOGLE_INFORMAL_TRANSLATE_URL', 'https://translate.googleapis.com'),
],

HTTP goes through Guzzle, and the list of Google languages is cached for a day in the default Laravel cache. Bind your own Psr\Http\Client\ClientInterface, PSR-17 factories or Psr\SimpleCache\CacheInterface to replace them.

Google is added at the end of TranslatorInterface::class . '[]'. To put another provider first:

php
$this->app->extend(TranslatorInterface::class . '[]', fn (array $translators) => [new MyProvider(), ...$translators]);

Upgrading to 2.x ​

  • Requires eugene-erg/laravel-icu-i18n-translate 3.x or 4.x and eugene-erg/google-informal-icu-i18n-translator 3.x (named google-informal-icu-i18n-translate up to 2.x). 1.x could not be installed with them.
  • The endpoint is read from config('services.google_informal_translate.url'). 1.x read a config key named GOOGLE_ICU_I18N_TRANSLATE_API_URL, which no config file defines.
  • The service provider is now discovered automatically (1.x declared it outside extra).
  • Guzzle is a dependency instead of an optional default.

Development ​

bash
composer check   # pint, phpstan (level max), phpunit

From the README.md of EugeneErg/laravel-google-informal-icu-i18n-translate · composer require eugene-erg/laravel-google-informal-icu-i18n-translate