PHP code example of marksitko / laravel-deep-translatable

1. Go to this page and download the library: Download marksitko/laravel-deep-translatable 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/ */

    

marksitko / laravel-deep-translatable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MarkSitko\DeepTranslatable\UseDeepTranslations;
use MarkSitko\DeepTranslatable\Contracts\Translatable;

class Post extends Model implements Translatable
{
    use UseDeepTranslations;

    protected $fillable = [
        'title',
        'body',
    ];

    protected $translatable = [
        'title',
        'body',
    ];
}

use App\Models\Post;
use MarkSitko\DeepTranslatable\Lang;

$post = Post::create([
    'title' => 'My awesome title!',
    'body' => 'This is the main content',
]);

$post->translateViaDictionary(Lang::DE);

// to swap the post language, 
// simply call your url with an additional query 'lang' parameter.
// https://yousite.test/posts/1?lang=de

// It would return the post translatable keys:

// titel => 'Mein genialer Titel!'
// body => 'Dies ist der Hauptinhalt'

use App\Models\Post;
use MarkSitko\DeepTranslatable\Lang;

Post::first()->translate(Lang::DE);

Post::first()->translations;

Post::first()->updateTranslation('title', LANG::DE, 'Mein großartiger Titel!');

use MarkSitko\DeepTranslatable\DeepL;
use MarkSitko\DeepTranslatable\Lang;

DeepL::translate('Hello World!', Lang::DE); // <- returns 'Hallo Welt!'
DeepL::translate('Los gehts!', Lang::EN, ['source_lang' => Lang::DE]); // <- returns 'Let's go'

class Fields extends Model implements Translatable
{
    use UseDeepTranslations;
    /**
     * Imagine you have stored the following json object in the option column
     * 
     * {
     *  option: {
     *      nested: {
     *          key: {
     *              value: 'I would like to be translated', 
     *          },
     *          other_key: {
     *              value: 'I should not be translated', 
     *          },
     *      },
     *  }
     * }
     * 
     * */

    protected $translatable = [
        'option.nested.key.value', 
    ];
}
 php 
'providers' => [
    //...
    MarkSitko\DeepTranslatable\DeepTranslatableServiceProvider::class,
];
 bash
$ php artisan vendor:publish --provider="MarkSitko\DeepTranslatable\DeepTranslatableServiceProvider"
 bash
$ php artisan migrate