aw-studio/laravel-deeplable

2,594 6
Install
composer require aw-studio/laravel-deeplable
Latest Version:v0.9.5
PHP:^8.0|^8.1|^8.2
Laravel:8 – 12
License:MIT
Last Updated:Apr 23, 2025
Links: GitHub  ·  Packagist
Maintainer: jannescb

Laravel Deeplable

A package for translating Laravel Models using DeepL's API. It contains a translator for the astrotomic/laravel-translatable composer package. Translators for other usecases can be written easily.

Installation

Install the package via composer:

composer require aw-studio/laravel-deeplable

and publish the config file:

php artisan vendor:publish --tag=deeplable --force

Setup

After publishing the config/deeplable.php you must enter your DeepL-Secret in order to use their API. You may also configure all Models you want to translate when you run the deeplable:run command.

Usage

You can translate your translatable models to all languages that are set in your config/translatable.php as long as they are available on DeepL. In order to translate your models to a target language the default translation (fallback_locale) must be available. You can use the Deeplable trait to easily translate your model to a target language.

use AwStudio\Deeplable\Traits\Deeplable;

class Post extends Model
{
    use Deeplable;
}

Imagine you have stored the default language ('en') and want to auto-generate the german translation for all translated attributes:

$post = Post::first();

$post->translateTo('de');
$post->translateAttributeTo('de', 'title');
$post->translateAttributesTo('de', ['title', 'text']);

If you want to translate all Models to all locales, simply run the deeplable artisan command:

php artisan deeplable:run

You may also set an argument if you want to translate a specific language:

php artisan deeplable:run fr

Translators

Translators have the purpose to bind a translatable attribute to a model. A build in example is the AstrotomicTranslator that updates models that are translated by the astrotomic/laravel-translatable package.

Creating A Translator

A translator must extend the AwStudio\Deeplable\Translators\BaseTranslator class which has 2 abstract methods:

class AstrotomicTranslator
{
    protected function translateAttribute(Model $model, $attribute, $locale, $translation)
    {
        $model->translateOrNew($locale)->setAttribute($attribute, $translation);
    }

    public function getTranslatedAttributes(Model $model, $locale)
    {
        return array_keys($model->getTranslationsArray()[$locale] ?? []);
    }
}
Registering The Translator

A translator may be registered in a Service Provider like this:

use AwStudio\Deeplable\Translators\Resolver;
use Astrotomic\Translatable\Contracts\Translatable;

public function register()
{
    Translator::register(Translatable::class, function () {
            return new AstrotomicTranslator($this->app['deeplable.api']);
    });
}
Using The Translator

The translator can then be used like this:

use AwStudio\Deeplable\Facades\Translator;
use Astrotomic\Translatable\Contracts\Translatable;

Translator::get(Translatable::class)->translate($post, 'de', 'en'); // Translates the whole model from en to de
Translator::get(Translatable::class)->translateAttributes($post, ['title', 'text'], 'de', 'en'); // Translates attributes title and text from en to de

Translation Strategy

Given the case that different models need different translators, a translation strategy can be specified in a service provider like this:

use AwStudio\Deeplable\Facades\Translator;
use AwStudio\Deeplable\Translators\Resolver;
use Astrotomic\Translatable\Contracts\Translatable;

public function register()
{
    Translator::strategy(function(Model $model) {
        if($model instanceof Translatable) {
            return Translatable::class;
        }
        // something else...
    });
}

The correct translator can now be received like this:

Translator::for($post)->translate(...);

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

641,106,524 9,700
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

502,645,077 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

418,291,778 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

399,241,428 4,660

Version History

Version Released PHP Laravel License
v0.9.5 ^8.0|^8.1|^8.2 ^8.0|^9.0|^10.0|^11.0|^12.0 MIT
v0.9.4 ^8.0|^8.1|^8.2 ^8.0|^9.0|^10.0|^11.0 MIT
v0.9.3 ^8.0 ^8.0|^9.0 MIT
v0.9.2 ^8.0 ^8 MIT
v0.9.1 ^8.0 ^8 MIT