Frequently asked questions

What is darvis/laravel-google-translate?

A Laravel package that machine translates text, HTML and Eloquent models with the Google Cloud Translation API v2 and an API key. It has one service class for strings and one trait for models, and it stores a translated model as a new row in the same table with pid pointing at the source row.

How do I translate text with Google Translate in a Laravel application?

Install darvis/laravel-google-translate, set GOOGLE_TRANSLATE_API_KEY in .env and call app(GoogleTranslateService::class)->translate('Hallo wereld', 'en'). You get the translated string back, or null when the text is empty, the key is missing or the API call failed.

How do I translate an Eloquent model into another language in Laravel?

Give the table a locale and a nullable pid column, add the HasGoogleTranslate trait and list the fields in $translatableFields. $page->createTranslation('en') then translates those fields and stores a new row with locale set to en and pid set to the id of the source row.

How does darvis/laravel-google-translate compare to spatie/laravel-translatable or Google’s PHP client?

A package such as spatie/laravel-translatable stores translations that you supply; this package produces the translation with Google and stores it as a row of its own, so every translation has its own slug and status. It does not use Google’s PHP SDK: it calls the v2 REST API through Laravel’s HTTP client, so there are no extra dependencies and Http::fake() works in tests.

Which Laravel and PHP versions does darvis/laravel-google-translate support?

PHP 8.2 and higher with Laravel 11, 12 or 13; Laravel 13 itself needs PHP 8.3. Livewire is not required. CI runs the tests on PHP 8.2 to 8.4 with each of those Laravel versions, on the lowest and the latest dependencies.

What do I need from Google, and what does translating cost with this package?

You need a Google Cloud project with billing enabled, the Cloud Translation API switched on and an API key. The package is free and adds no costs of its own; Google charges for the characters you send, for every target locale again. createTranslation() returns the existing row when the locale already exists and fillMissingTranslations() only sends empty fields, so a model field is not sent twice.

Does darvis/laravel-google-translate throw an exception when a translation fails?

No. A failed API call is written to the log with Log::error(), on a line that starts with Google Translate failed:, and the method returns null, or an empty array for a batch. createTranslation() returns null when nothing could be translated, and fillMissingTranslations() reports the failed fields in its errors list. Check the return value.

Does the package keep the HTML tags in translated content?

translateHtml() and every model field you list in $htmlFields are sent with format set to html, which tells Google to translate the text and keep the tags. The result is still HTML from an external service, so sanitise it the same way you sanitise the source.

Is my Google API key safe with darvis/laravel-google-translate?

The key is sent only to translation.googleapis.com, in the X-goog-api-key header. It is not part of the URL, so it does not end up in the log when a request fails. Restrict the key to the Cloud Translation API, and to the IP addresses of your servers, in the Google Cloud console.

How do I test Laravel code that uses darvis/laravel-google-translate without calling Google?

Set google-translate.api_key to any value, because without a key nothing is sent, and fake the HTTP client with Http::fake(['translation.googleapis.com/*' => Http::response([...])]). Add Http::preventStrayRequests() so a forgotten fake can never reach Google; the package then returns null and logs the blocked request instead of throwing.