PHP code example of aw-studio / laravel-deeplable

1. Go to this page and download the library: Download aw-studio/laravel-deeplable 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/ */

    

aw-studio / laravel-deeplable example snippets


use AwStudio\Deeplable\Traits\Deeplable;

class Post extends Model
{
    use Deeplable;
}

$post = Post::first();

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

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] ?? []);
    }
}

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

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

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...
    });
}

Translator::for($post)->translate(...);
bash
php artisan vendor:publish --tag=deeplable --force
bash
php artisan deeplable:run
bash
php artisan deeplable:run fr