Automatic translations without hassle

Submitted by jesseyvanofferen - 5 months ago

Automatically replace column with a translated value.

https://github.com/hostit-online/laravel-translator

<?php

namespace App\Models;

use HostitOnline\LaravelTranslator\Traits\Translatable;

class Product extends Model
{
    use Translatable;

    /** @var array|string[]  */
    public array $translatable = [
        'name'
    ];

    protected $fillable = [
        'name'
    ];
}

$product = Product::create([
    'name' => 'Book'
]);

\HostitOnline\LaravelTranslator\Models\Translations::create([
    'value' => 'Boek',
    'translatable_id' => $product->id,
    'translatable_type' => Product::class,
    'iso_code' => 'nl',
    'translatable_column' => 'name'
]);

\HostitOnline\LaravelTranslator\Models\Translations::create([
    'value' => 'Livre',
    'translatable_id' => $product->id,
    'translatable_type' => Product::class,
    'iso_code' => 'fr',
    'translatable_column' => 'name'
]);

ProductController {

    public function show(Product $product): string
    {
        return $product->name; // Output: Livre
    }
}