Table of Contents generated with DocToc
Observation: This repository is not finished yet, therefore don't use it in your project yet!
This is an easy and simple yet effective model translation laravel package.
Enable today your model classes for multiple languages!
use panopla\TranslatableTrait;
class User extends Model {
use TranslatableTrait;
$translatable = ['bio'];
}
___
Language::sessionLanguage('en');
$user->bio = "My English bio";
Language::sessionLanguage('pt');
$user->bio = "Usando Português agora para a minha bio";
Add this following line to your providers
list
'providers' => [
panopla\Translatable\TranslatableServiceProvider::class,
]
And this for the aliases
section
'aliases' => [
'Language' => panopla\Translatable\Facades\Language::class,
]
Supposing you have a class named Product
, and you desire to make its text fields translatable.
For the sake of simplicity, let us assume that we only need a name and price for our model.
Schema::create('product', function (Blueprint $table) {
$table->increments('id');
$table->decimal('price');
$table->timestamps();
});
We are making usage of the TranslatableInterface
php interface here in case you'd like
to program to an interface
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use panopla\Translatable\TranslatableInterface;
use panopla\Translatable\TranslatableTrait;
class Product extends Model implements TranslatableInterface
{
use TranslatableTrait;
protected $translatable = ['name'];
}
Additionally, you will create a class with the same named suffixed with Text
on its name.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductText extends Model
{
protected $fillable = ['name'];
}
public function up()
{
Schema::create('product_text', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::drop('product_text');
}