Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

PanoPla/laravel-translatable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents generated with DocToc

laravel-translatable

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!

Examples

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";
    

Setup

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,
]

Using on your classes

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.

Main model migration

Schema::create('product', function (Blueprint $table) {
            $table->increments('id');

            $table->decimal('price');

            $table->timestamps();
        });

Main model class

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'];

}

Text model class

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'];

}

Text model class migration

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');
}

Releases

No releases published

Packages

No packages published

Languages