PHP code example of spletna-postaja / translatable

1. Go to this page and download the library: Download spletna-postaja/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/ */

    

spletna-postaja / translatable example snippets


use Laraplus\Data\Translatable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Translatable;
}

Post::first();
$post->title; // title in the current locale

Post::translateInto('de')->first();
$post->title; // title in 'de' locale

Post::translateInto('de')->withFallback('en')->first();
$post->title; // title in 'de' if available, otherwise in 'en'

Post::where('body', 'LIKE', '%Laravel%')->orderBy('title', 'desc');

Post::onlyTranslated()->all()

'providers' => [
    // Other providers
    Laraplus\Data\TranslatableServiceProvider::class,
],

TranslatableConfig::currentLocaleGetter(function() {
    // Return the current locale of the application
});

TranslatableConfig::fallbackLocaleGetter(function() {
    // Return the fallback locale of the application
});

Schema::create('posts', function(Blueprint $table)
{
    $table->increments('id');
    $table->datetime('published_at');
    $table->timestamps();
});

Schema::create('posts_i18n', function(Blueprint $table)
{
    $table->integer('post_id')->unsigned();
    $table->string('locale', 6);
    $table->string('title');
    $table->string('body');
    
    $table->primary(['post_id', 'locale']);
});

use Laraplus\Data\Translatable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Translatable;
}

class Post extends Model
{
    use Translatable;
    
    protected $withFallback = false;
    
    protected $onlyTranslated = true;
}

Post::where('active', 1)->orderBy('title')->get();

Post::onlyTranslated()->get();

Post::withoutFallback()->get();

Post::withUntranslated()->withFallback()->get();
Post::withUntranslated()->withFallback('de')->get();

Post::translateInto('de')->get();

Post::withoutTranslations()->get();

Post::where('title', 'LIKE', '%Laravel%')->orWhere('description', 'LIKE', '%Laravel%')->get();

Post::orderBy('title')->get();

Post::create([
    'title'        => 'My title',
    'published_at' => Carbon::now(),
]);

Post::createInLocale('de', [
    'title'        => 'Title in DE',
    'published_at' => Carbon::now(),
]);

Post::create([
    'published_at' => Carbon::now()
], [
    'en' => ['title' => 'Title in EN'],
    'de' => ['title' => 'Title in DE'],
]);

Post::forceCreate([/*attributes*/], [/*translations*/]);
Post::forceCreateInLocale($locale, [/*attributes*/]);

$user = User::first();

$user->title = 'New title';
$user->save();

$user = User::first();

$user->saveTranslation('en', [
    'title' => 'Title in EN'
]);

$user->saveTranslation('de', [
    'title' => 'Title in DE'
]);

User::where('published_at', '>', Carbon::now())->update(['title' => 'New title']);

User::where('published_at', '>', Carbon::now())->translateInto('de')->update(['title' => 'New title']);

$user = User::first();

$user->delete();

User::where('published_at', '>', Carbon::now())->delete();

$user = $user->first();

foreach ($user->translations as $translation) {
    echo "Title in {$translation->locale}: {$translation->title}";
}

$user = $user->first();

$user->translate('en')->title; // Title in EN
$user->translate('de')->title; // Title in DE

User::withAllTranslations()->get();