PHPackages                             darvis/laravel-google-translate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. darvis/laravel-google-translate

ActiveLibrary[Database &amp; ORM](/categories/database)

darvis/laravel-google-translate
===============================

Google Translate integration for Laravel with support for translatable models

v1.0.1(1mo ago)05[1 PRs](https://github.com/ArvidDeJong/laravel-google-translate/pulls)MITPHPPHP ^8.2CI passing

Since Jan 26Pushed 1mo agoCompare

[ Source](https://github.com/ArvidDeJong/laravel-google-translate)[ Packagist](https://packagist.org/packages/darvis/laravel-google-translate)[ Docs](https://github.com/darvis/laravel-google-translate)[ RSS](/packages/darvis-laravel-google-translate/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (11)Versions (4)Used By (0)

Laravel Google Translate
========================

[](#laravel-google-translate)

[![Latest Version on Packagist](https://camo.githubusercontent.com/546f7d641203cf6127ce5e9471df824574b824a3679d58efe284ffa54843e85c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461727669732f6c61726176656c2d676f6f676c652d7472616e736c6174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/laravel-google-translate)[![PHP Version](https://camo.githubusercontent.com/ee2ab45e4818d6cd4a7db96b26e977dc49143d93d180b66a0b8e1e433c0270a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6461727669732f6c61726176656c2d676f6f676c652d7472616e736c6174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/laravel-google-translate)[![Laravel Version](https://camo.githubusercontent.com/c20259c437ff58cd84098943e59f925da9f087a13ca51664012c943949657a7c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d31312e7825323025374325323031322e7825323025374325323031332e782d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/laravel-google-translate)[![License](https://camo.githubusercontent.com/6d1ea25ab344fb0161e99b5285fccb87f5d7aee09fd00b5e2c53943bc8205b27/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6461727669732f6c61726176656c2d676f6f676c652d7472616e736c6174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/laravel-google-translate)

Google Translate integration for Laravel with support for translatable Eloquent models.

Features
--------

[](#features)

- 🌍 **Simple Translation API** - Translate text and HTML content
- 📦 **Model Trait** - Easy integration with Eloquent models
- 🔄 **Batch Translation** - Translate multiple texts at once
- 📝 **HTML Support** - Preserve HTML tags during translation
- ⚙️ **Configurable** - Customize source and target locales

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11.x, 12.x, or 13.x
- Google Cloud Translation API key

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require darvis/laravel-google-translate
```

Publish the configuration file:

```
php artisan vendor:publish --tag=google-translate-config
```

Add your Google Translate API key to your `.env` file:

```
GOOGLE_TRANSLATE_API_KEY=your-api-key-here
GOOGLE_TRANSLATE_SOURCE_LOCALE=nl
GOOGLE_TRANSLATE_TARGET_LOCALES=en,de,fr
```

Usage
-----

[](#usage)

### Basic Translation

[](#basic-translation)

```
use Darvis\LaravelGoogleTranslate\GoogleTranslateService;

$translator = app(GoogleTranslateService::class);

// Translate text
$translated = $translator->translate('Hallo wereld', 'en');
// Result: "Hello world"

// Translate HTML (preserves tags)
$translated = $translator->translateHtml('Hallo wereld', 'en');
// Result: "Hello world"

// Batch translate
$translations = $translator->translateBatch(['Hallo', 'Wereld'], 'en');
// Result: ['Hello', 'World']
```

### Model Integration

[](#model-integration)

Add the `HasGoogleTranslate` trait to your Eloquent model:

```
use Darvis\LaravelGoogleTranslate\Traits\HasGoogleTranslate;
use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    use HasGoogleTranslate;

    protected $fillable = [
        'pid',
        'locale',
        'title',
        'content',
        'description',
        // ...
    ];

    // Define which fields should be translated
    protected array $translatableFields = [
        'title',
        'content',
        'description',
        'seo_title',
        'seo_description',
    ];

    // Define which fields contain HTML
    protected array $htmlFields = [
        'content',
        'description',
    ];
}
```

### Creating Translations

[](#creating-translations)

```
$page = Page::find(1); // Dutch page

// Create English translation
$englishPage = $page->createTranslation('en', [
    'slug' => $page->slug,
    'active' => true,
]);

// Check if translation exists
if ($page->hasTranslation('en')) {
    $translation = $page->getTranslation('en');
}

// Get all translations
$allTranslations = $page->getAllTranslations();
```

### Filling Missing Translations

[](#filling-missing-translations)

```
$englishPage = Page::where('locale', 'en')->first();

// Fill empty fields from Dutch source
$result = $englishPage->fillMissingTranslations('nl');

// $result = [
//     'translated' => ['title', 'content'],
//     'errors' => [],
// ]
```

### Finding Missing Translations

[](#finding-missing-translations)

```
// Get all Dutch pages without English translation
$missing = Page::getMissingTranslations('en', 'nl');

foreach ($missing as $page) {
    $page->createTranslation('en');
}
```

### Scopes

[](#scopes)

```
// Get only source items (no pid)
$sourcePages = Page::sourceItems()->get();

// Get items in current locale
$localizedPages = Page::localized()->get();

// Get items in specific locale
$dutchPages = Page::localized('nl')->get();
```

Configuration
-------------

[](#configuration)

```
// config/google-translate.php

return [
    // Your Google Cloud Translation API key
    'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),

    // Default source locale
    'source_locale' => env('GOOGLE_TRANSLATE_SOURCE_LOCALE', 'nl'),

    // Target locales (comma-separated in .env)
    'target_locales' => explode(',', env('GOOGLE_TRANSLATE_TARGET_LOCALES', 'en')),
];
```

Database Structure
------------------

[](#database-structure)

Your translatable models should have:

- `locale` column (string) - The language code (e.g., 'nl', 'en')
- `pid` column (nullable integer) - Parent ID pointing to the source item

Example migration:

```
Schema::create('pages', function (Blueprint $table) {
    $table->id();
    $table->foreignId('pid')->nullable()->constrained('pages')->nullOnDelete();
    $table->string('locale', 5)->default('nl');
    $table->string('title');
    $table->text('content')->nullable();
    // ...
    $table->timestamps();

    $table->index(['locale', 'pid']);
});
```

API Reference
-------------

[](#api-reference)

### GoogleTranslateService

[](#googletranslateservice)

MethodDescription`isAvailable()`Check if API key is configured`translate($text, $targetLocale, $sourceLocale)`Translate plain text`translateHtml($html, $targetLocale, $sourceLocale)`Translate HTML content`translateBatch($texts, $targetLocale, $sourceLocale)`Translate multiple texts`translateFields($fields, $targetLocale, $sourceLocale, $htmlFields)`Translate array of fields`getSourceLocale()`Get configured source locale`getTargetLocales()`Get configured target locales### HasGoogleTranslate Trait

[](#hasgoogletranslate-trait)

MethodDescription`hasTranslation($locale)`Check if translation exists`getTranslation($locale)`Get translation for locale`getAllTranslations()`Get all translations including self`createTranslation($locale, $attributes)`Create new translation`fillMissingTranslations($sourceLocale)`Fill empty fields from source`getTranslatableFields()`Get list of translatable fields`getHtmlFields()`Get list of HTML fields### Scopes

[](#scopes-1)

ScopeDescription`sourceItems()`Only items without pid (originals)`localized($locale)`Items in specific locale### Static Methods

[](#static-methods)

MethodDescription`getMissingTranslations($targetLocale, $sourceLocale)`Get items missing translationCMS Integration
---------------

[](#cms-integration)

For a complete guide on building a Translation Check Dashboard for your CMS, see the [CMS Integration Guide](docs/cms-integration.md).

Key features:

- Overview of missing translations per module
- Bulk translation with one click
- Individual item translation
- API status monitoring
- Progress tracking per locale

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

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

Author
------

[](#author)

- **Arvid de Jong** -

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance91

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~62 days

Total

2

Last Release

44d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/24c445b7580e09ff72b8340d1423886148c4c8a249d0a828c98285109e7e5663?d=identicon)[darvis](/maintainers/darvis)

---

Top Contributors

[![ArvidDeJong](https://avatars.githubusercontent.com/u/7394837?v=4)](https://github.com/ArvidDeJong "ArvidDeJong (4 commits)")

---

Tags

laravellocalizationi18ntranslationeloquentmultilingualgoogle-translategoogle cloud

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/darvis-laravel-google-translate/health.svg)

```
[![Health](https://phpackages.com/badges/darvis-laravel-google-translate/health.svg)](https://phpackages.com/packages/darvis-laravel-google-translate)
```

###  Alternatives

[richan-fongdasen/laravel-i18n

Simple route and eloquent localization / translation in Laravel

1296.6k](/packages/richan-fongdasen-laravel-i18n)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

754.3k2](/packages/omaralalwi-lexi-translate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
