yamanhacioglu/laravel-auto-translate

Automatic translation package for Laravel models using DeepL

dev-main 2024-10-11 23:42 UTC

This package is auto-updated.

Last update: 2024-12-12 00:01:11 UTC


README

Automatic translation package for Laravel models using DeepL API.

{{ THIS PACKAGE STILL IN TEST PROCESS. }}

Installation

composer require northlab/laravel-auto-translate

Configuration

  1. Publish the configuration and migrations:
php artisan vendor:publish --provider="NorthLab\AutoTranslate\AutoTranslateServiceProvider"
  1. Run the migrations
php artisan migrate
  1. Add your DeepL API key to your new model (Deepl):

Usage

  1. Implement the Translatable interface and use the HasAutoTranslations trait in your model:
use NorthLab\AutoTranslate\Contracts\Translatable;
use NorthLab\AutoTranslate\Traits\HasAutoTranslations;

For Laravel 11

#[ObservedBy([TranslatableModelObserver::class])]
class Post extends Model implements Translatable
{
    use HasAutoTranslations;

    protected array $translatable = ['title', 'content'];

    protected string $sourceLanguage = 'Column containing source language'

    public function getTranslatableAttributes(): array
    {
        return $this->translatable;
    }

    public function getSourceLanguageAttribute(): string
    {
        return $this->source_language;
    }

}

For Laravel 10 ...

class Post extends Model implements Translatable
{
    use HasAutoTranslations;

    protected array $translatable = ['title', 'content'];

    protected string $sourceLanguage = 'Column containing source language'

    public function getTranslatableAttributes(): array
    {
        return $this->translatable;
    }

    public function getSourceLanguageAttribute(): string
    {
        return $this->source_language;
    }

    protected static function boot()
    {
       Post::observe(TranslatableModelObserver::class);
    }
}
  1. The translations will be automatically processed when the model is saved.

  2. Skip translation:

$post->withoutAutoTranslation()->save();
  1. Updates

It will be constantly updated, taking into account its dependencies and features that can be added.

License

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