PHP code example of slivka-b / laravel-translatable
1. Go to this page and download the library: Download slivka-b/laravel-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/ */
slivka-b / laravel-translatable example snippets
$book = Book::create(['title' => 'Book about giraffes']);
// Storing translations
app()->setLocale('es');
$book->title = 'Libro sobre jirafas';
$book->save();
// Reading translations
app()->setLocale('es');
echo $book->title; // 'Libro sobre jirafas'
app()->setLocale('en');
echo $book->title; // 'Book about giraffes'
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nevadskiy\Translatable\HasTranslations;
class Post extends Model
{
use HasTranslations;
}
/**
* The attributes that can be translatable.
*
* @var array
*/
protected $translatable = [
'title',
'description',
];
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nevadskiy\Translatable\HasTranslations;
class Post extends Model
{
use HasTranslations;
protected $translatable = [
'title',
'description',
];
}
$book = Book::where('title', 'Book about birds')->first();
app()->setLocale('ru');
$book->update(['title' => 'Книга о птицах']);
echo $book->title; // 'Книга о птицах'
app()->setLocale('en');
echo $book->title; // 'Book about birds'
$book = Book::where('title', 'Book about dolphins')->first();
$book->translate('title', 'Книга о дельфинах', 'ru');
echo $book->getTranslation('title', 'ru'); // 'Книга о дельфинах'
app()->setLocale('de');
Book::create(...); // This will persist model as usual with the default application locale.
// In a controller
app()->setLocale('ru');
$books = Book::paginate(20);
// In a view
@foreach ($books as $book)
{{ $book->title }} // Shows a title in the current locale OR in the default locale if a translation is missing.
@endforeach
class Book extends Model
{
// ...
public function getTitleAttribute()
{
return Str::ucfirst($this->attributes['title']);
}
}
$book = Book::create(['title' => 'book about birds']);
$book->translate('title', 'книга о птицах', 'ru');
// Using attribute with the current locale
app()->setLocale('ru');
echo $book->title; // 'Книга о птицах'
// Using getTranslate method
echo $book->getTranslation('title', 'ru'); // 'Книга о птицах'
class Book extends Model
{
public function setDescriptionAttribute($description)
{
$this->attributes['description'] = Str::substr($description, 0, 10);
}
}
$book = Book::create(['description' => 'Very long description']);
$book->translate('description', 'Очень длинное описание', 'ru');
// Using attribute with the current locale
app()->setLocale('ru');
echo $book->description; // 'Очень длин'
// Using getTranslation method
echo $book->getTranslation('description', 'ru'); // 'Очень длин'
$books = Book::withoutTranslations()->get();
$books = Book::whereTranslatable('title', 'Книга о жирафах')->get();
class Post extends Model
{
use HasTranslations;
protected $translatable = ['title'];
public function autoLoadTranslations()
{
return false;
}
public function autoSaveTranslations()
{
return false;
}
}
use Nevadskiy\Translatable\Translatable;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app[Translatable::class]->disableAutoLoading();
$this->app[Translatable::class]->disableAutoSaving();
}
}
// app/Http/Middleware/SetLocaleMiddleware.php
public function handle($request, Closure $next)
{
// Setting the current locale from cookie
app()->setLocale($request->cookie('locale'));
}