Translatable

Translatable

Multilingual Models.

Moamen Eltouny (Raggi) Chun-Sheng, Li (Peter)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Install

Install the latest version using Composer

composer require pharaonic/laravel-translatable
Creating

You can generate your Model & Translatable Model.

Optional --migrations option

php artisan make:translatable Post --migrations

Result :

// Models
App\Models\Post
App\Models\PostTranslation

// Migrations
2021_05_31_121627_create_posts_table.php
2021_05_31_121628_create_post_translations_table.php
Actions

translate (string $locale = null)

Getting/Setting value from/to item has been translated before.

// Getting English Title of Post
echo $post->translate('en')->title;

// Setting English Title of Post
$post->translate('en')->title = 'Something Here';
$post->save();

translateOrNew (string $locale)

Getting/Setting value from/to item has been translated before or creating a new translation.

// Getting English Title of Post
echo $post->translateOrNew('en')->title;

// Setting English Title of Post
$post->translateOrNew('en')->title = 'Something Here';
$post->save();

translateOrFail (string $locale)

Getting/Setting value from/to item has been translated before.

The item must be translated before or the page will return 404 Error.

// Getting English Title of Post
echo $post->translateOrFail('en')->title;

// Setting English Title of Post
$post->translateOrFail('en')->title = 'Something Here';
$post->save();

translateOrDefault (string $locale = null)

Getting/Setting value from/to item has been translated before with locale.

If the locale doesn't exist then will use the default locale from Config.

// Getting English Title of Post
echo $post->translateOrDefault('en')->title;

// Setting English Title of Post
$post->translateOrDefault('en')->title = 'Something Here';
$post->save();

hasTranslation (string $locale = null)

Checking the locale/default has been inserted as a translation before or not.

$translated = $post->hasTranslation('en');
// Returns Boolean
Attributes

locales

dump($post->locales);
// Returns Array

Results :

array:1 [▼
  0 => "en",
  1 => "ar"
]
Scopes

translated (string $locale = null)

Getting translated items in the $locale.

Post::translated('en')->get();
// Returns Illuminate\Database\Eloquent\Collection

notTranslated (string $locale = null)

Getting NOT translated items in the $locale.

Post::notTranslated('fr')->get();
// Returns Illuminate\Database\Eloquent\Collection

translatedSorting (string $locale, string $field, string $method = 'asc')

Getting Translated items with Sorting depends on Locale, Field name and sorting method (asc, desc).

Post::translatedSorting('en', 'title', 'asc')->get();
// Returns Illuminate\Database\Eloquent\Collection

whereTranslation (string $field, $value, string $locale = null)

orWhereTranslation (string $field, $value, string $locale = null)

Filtering translated bepends on locale with a specific value.

Post::translated('en')->whereTranslation('type', 'package')->get();
// Returns Illuminate\Database\Eloquent\Collection

whereTranslationLike (string $field, $value, string $locale = null)

orWhereTranslationLike (string $field, $value, string $locale = null)

Filtering translated bepends on locale with %value%.

Post::translated('en')->whereTranslationLike('content', 'Moamen Eltouny')->get();
// Returns Illuminate\Database\Eloquent\Collection
Relationships

translations

dump($post->translations);
// Returns Illuminate\Database\Eloquent\Collection

Results :

Illuminate\Database\Eloquent\Collection {#1262 ▼
  #items: array:1 [▼
    "en" => App\Models\PostTranslation {#1293 ▶}
  ]
}