2017 © Pedro PelĂĄez
 

library laravel-eloquent-multilingualization

Add multilingual support to laravel eloquent models

image

zai/laravel-eloquent-multilingualization

Add multilingual support to laravel eloquent models

  • Monday, September 4, 2017
  • by zaichaopan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

laravel-eloquent-multilingualization

Add multilingual support to your laravel eloquent models in a breeze, (*1)

Installation

Step 1: Install package

Executing the following command to add the package in your composer.json, (*2)

composer require zai/laravel-eloquent-multilingualization

For laravel 5.4, add the service provider to app/config/app.php, (*3)

 Zai\Translate\TranslationServiceProvider::class,

For laravel 5.5, because of package auto-discovery, there is no need to add service provider to app/config/app.php, (*4)

Step 2: Migration

Executing the following commands to add translations table. Only one table is needed for translating any Eloquent models, (*5)

php artisan translations:table

php artisan migrate

Usage

Use Translatable trait in the model you want to translate

Let's say the model is called Article. In the Article model, (*6)

``` php use Zai\Translate\Translatable;, (*7)

use Illuminate\Database\Eloquent\Model;, (*8)

class Article extends Model { use Translatable; }, (*9)


### Define translatables property in the model Take the same Article model as example. We want to translate title and body of an article ``` php protected $translatables = [ 'title', 'body' ];

Add Translation

Using method: addTranslation. It takes an associative array as parameter. Simply put the translation data in the array, including which language it is using key locale., (*10)

// create a new article
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// add translation to the article
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

The method only adds keys that exist in the translatables property of the model. Any other keys will be ignored. If a key in the translatables property is missing in the parameter. It will still be inserted, but the value will become empty string., (*11)

If you submit the translation data in the form. In your ArticleTranslationsController, simply using, (*12)

public function store(Article $article)
{
    $article->addTranslation(request()->all();
}

Remember to include locale input filed in your form, so it can be posted in the request. If addTranslation method is applied to an existing translation, it will update the existing translation with values in the parameter., (*13)

Display translation

Using translation attribute provided by the trait, e.g, $article->translation->title. It will return correct translation based on what the current locale is (the value returned by App::getLocale()), (*14)

For default locale or if the translation of a locale is missing, it will return the values in the model., (*15)

// create a new title
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// for default locale, or no translations existing
$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

// after adding translation
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

// set the locale to fr
App::setLocale('fr');

$article->translation->title; // the output is Bonjour
$article->translation->body;  // the out is laravel est génial!

// for a no existing locale
App::setLocale('zh');

$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

Update translation

Using method: updateTranslation. It takes an associative array as parameter. Simply put the updated translation data in the array, including which language it is., (*16)

``` php // add translation to the article $article->updateTranslation([ 'locale' => 'fr', 'title' => 'updated title', 'body' => 'updated body content here' ]);, (*17)


If you update the translation using form, in your ArticleTranslationsController ```php public function update(Article $article) { $article->updateTranslation(request()->all()); }

If you update to a translation which doesn't exist, it will insert a new translation., (*18)

Delete a translation

Using method deleteTranslation. It takes a string which specifies which language of the translation you want to delete as parameter., (*19)

$article->deleteTranslation('fr');

Delete all translations

Using method deleteTranslations which takes no parmaters, (*20)

$article->deleteTranslations();

Check translations exist

Using method hasTranslations which takes no paramters. The method returns boolean to indicate if the record has translations or not., (*21)

 $article->hasTranslations();

Check translation of a locale exists

Using method hasTranslation. It takes a string as parameter which specifies the locale you are checking. The method returns boolean to indicate if the translation of a specific locale exists or not., (*22)

$article->hasTranslation();

Prevent N+1 problem

To prevent N+1 problem, eager load translations in your model, (*23)

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $translatables = [
        'title',
        'body'
    ];
}

Appending Values To JSON

Add the attribute translation to the appends property on the model, (*24)

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $appends = ['translation'];

    protected $translatables = [
        'title',
        'body'
    ];
}

The Versions

04/09 2017

dev-master

9999999-dev

Add multilingual support to laravel eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zai chao Pan

04/09 2017

v1.0.2

1.0.2.0

Add multilingual support to laravel eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zai chao Pan

04/09 2017

v1.0.1

1.0.1.0

Add multilingual support to laravel eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Zai chao Pan