2017 © Pedro Peláez
 

lib translatable

Translate Model

image

anacreation/translatable

Translate Model

  • Wednesday, June 20, 2018
  • by Xavier Au
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Overview

This add translation to Eloquent Model, (*1)

Installation

composer require anacreation/translatable

Eloquent model that use the, (*2)


namespace App; use Anacreation\Translatable\traits\TranslatableTrait; use Illuminate\Database\Eloquent\Model; class Product extends Model { use TranslatableTrait; }

can use the following APIs, (*3)

Usage

Create and update translations

createModelWithTranslations( array $attributes = [], array $content): Model

the $content has a predefine format, (*4)

$content = [
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
        "attribute_3" => "value 3",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 4",
        "attribute_2" => "value 5",
        "attribute_3" => "value 6",
    ]
]

$newModel = Model::createModelWithTranslations($attributes, $content);

it create a model instance and save the content, (*5)

updateTranslations(array $content): void

No matter for create new language transaltion or update existing language. Simple call this funciton. The $content structure is same as above., (*6)

Retrieve translation

if you have a model with translation as above., (*7)

then you can, (*8)

$mode->attribute_1;

this will automatically fetch the translation base on your current locale setting., (*9)

app()->getLocale();

Fallback

Then defalut fallback is set to false, no fallback if content is null., (*10)

The fall back system is very simple. if you have set the config fallback_locale and set the eloquent model fallback to true, (*11)

$model->fallback = true;

then if the translation for particular attribute is null. It will try to get the translation for fallback locale., (*12)

Retrieve all translation

$translation_array = $model->translatables;

this will return a array as above. The translation array has structure as below:, (*13)

[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
        "attribute_2" => "value 4",
    ]
]

Delete Translation

deleteTranslatableAttribute( string $key, string $locale = null ): void

This will delete specfic attribute for all or speficied transaltion. If the original translation as below:, (*14)

[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
        "attribute_2" => "value 4",
    ]
]

We call, (*15)

    $model->deleteTranslatableAttribute("attribute_2");

the translation will become, (*16)

[
    "language_code_1"=>[
        "attribute_1" => "value 1",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
    ]
]

if we call, (*17)

    $model->deleteTranslatableAttribute("attribute_2", "language_code_2");

the result will as below, (*18)

[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
    ]
]

deleteTranslatableWithLocale(string $locale ): void

This will remove all translation with specified locale. if we call, (*19)

    $model->deleteTranslatableWithLocale("language_code_2");

the result will as below, (*20)

[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ]
]

The Versions

20/06 2018

dev-develop

dev-develop

Translate Model

  Sources   Download

MIT

The Requires

 

by Avatar Xavier Au