PHP code example of darvis / laravel-google-translate

1. Go to this page and download the library: Download darvis/laravel-google-translate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

darvis / laravel-google-translate example snippets


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('<p>Hallo <strong>wereld</strong></p>', 'en');
// Result: "<p>Hello <strong>world</strong></p>"

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

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',
    ];
}

$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();

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

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

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

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

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

// 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();

// 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')),
];

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']);
});
bash
php artisan vendor:publish --tag=google-translate-config