darvis / laravel-google-translate
Translate text, HTML and Eloquent models in Laravel with the Google Cloud Translation API, with one row per locale linked through a pid column.
Package info
github.com/ArvidDeJong/laravel-google-translate
pkg:composer/darvis/laravel-google-translate
Requires
- php: ^8.2
- illuminate/database: ^11.0|^12.0|^13.0
- illuminate/http: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-21 11:32:10 UTC
README
Translate text, HTML and Eloquent models in Laravel with the Google Cloud Translation API (v2, with an API key). A translated model is stored as a new row in the same table, with pid pointing at the source row. The package does not translate your lang/ files, and it does not cache or queue by itself.
Features
- Text, HTML and batches - HTML keeps its tags, a batch is one request
- Translatable models - a trait for models with one row per locale, linked through a
pidcolumn - Nothing to catch - a failed API call is logged and returns
null, or[]for a batch; it never throws - No double costs for models - an existing translation is returned, and only empty fields are filled
- Testable - every call goes through Laravel's HTTP client, so
Http::fake()replaces Google - Safe with your key - sent in a header, never in a URL that can end up in a log
- Laravel Boost - guideline and skill included, so an AI assistant in your app knows the API
Requirements
- PHP 8.2 or higher
- Laravel 11, 12 or 13 (Laravel 13 itself needs PHP 8.3)
- A Google Cloud Translation API key, from a Google Cloud project with billing enabled
Installation
composer require darvis/laravel-google-translate
GOOGLE_TRANSLATE_API_KEY=your-api-key GOOGLE_TRANSLATE_SOURCE_LOCALE=nl GOOGLE_TRANSLATE_TARGET_LOCALES=en,de,fr
The installation page explains where the key comes from and how to check that it works.
Quick start
use Darvis\LaravelGoogleTranslate\GoogleTranslateService; $translator = app(GoogleTranslateService::class); $translator->translate('Hallo wereld', 'en'); // for example "Hello world" $translator->translateHtml('<p>Hallo</p>', 'en'); // for example "<p>Hello</p>" $translator->translateBatch(['Hallo', 'Wereld'], 'en'); // for example ["Hello", "World"]
For a model, add a locale and a nullable pid column to the table and the trait to the model:
use Darvis\LaravelGoogleTranslate\Traits\HasGoogleTranslate; use Illuminate\Database\Eloquent\Model; class Page extends Model { use HasGoogleTranslate; protected $fillable = ['pid', 'locale', 'title', 'content', 'slug']; protected $translatableFields = ['title', 'content']; protected $htmlFields = ['content']; } $english = $page->createTranslation('en', ['slug' => 'about-us']); // a new row, or null
Documentation
The full documentation lives on the documentation site:
- Installation: the package, the API key, the settings and a check that it works
- Quick start: a translatable model from migration to translated row
- Concepts: the table layout, failed calls, the API key and the costs
- API reference: every public method
- CMS integration: a Livewire screen with missing translations and a translate button
- Testing: test your code without calling Google
- Troubleshooting:
nullcomes back, and the log lines that say why - FAQ: short answers
Laravel Boost
The package ships a Laravel Boost guideline and a skill. Run php artisan boost:install, or php artisan boost:update --discover in a project that already uses Boost.
Testing
The package's own suite never calls Google:
composer test # Pest composer lint # Pint, check only; composer format fixes composer analyse # Larastan
Changelog
See CHANGELOG.
Contributing
See CONTRIBUTING.
Security
Please report a vulnerability privately, as described in SECURITY, not in the issue tracker.
License
The MIT License (MIT). See LICENSE.