This add translation to Eloquent Model
composer require anacreation/translatable
Eloquent model that use the
namespace App;
use Anacreation\Translatable\traits\TranslatableTrait;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use TranslatableTrait;
}
can use the following APIs
the $content
has a predefine format
$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
No matter for create new language transaltion or update existing language. Simple call this funciton. The $content structure is same as above.
if you have a model with translation as above.
then you can
$mode->attribute_1;
this will automatically fetch the translation base on your current locale setting.
app()->getLocale();
Then defalut fallback is set to false
, no fallback if content is null
.
The fall back system is very simple. if you have set the config fallback_locale and set the eloquent model fallback to true
$model->fallback = true;
then if the translation for particular attribute is null
. It will try to get the translation for fallback locale.
$translation_array = $model->translatables;
this will return a array as above. The translation array has structure as below:
[
"language_code_1"=>[
"attribute_1" => "value 1",
"attribute_2" => "value 2",
],
"language_code_2"=>[
"attribute_1" => "value 3",
"attribute_2" => "value 4",
]
]
This will delete specfic attribute for all or speficied transaltion. If the original translation as below:
[
"language_code_1"=>[
"attribute_1" => "value 1",
"attribute_2" => "value 2",
],
"language_code_2"=>[
"attribute_1" => "value 3",
"attribute_2" => "value 4",
]
]
We call
$model->deleteTranslatableAttribute("attribute_2");
the translation will become
[
"language_code_1"=>[
"attribute_1" => "value 1",
],
"language_code_2"=>[
"attribute_1" => "value 3",
]
]
if we call
$model->deleteTranslatableAttribute("attribute_2", "language_code_2");
the result will as below
[
"language_code_1"=>[
"attribute_1" => "value 1",
"attribute_2" => "value 2",
],
"language_code_2"=>[
"attribute_1" => "value 3",
]
]
This will remove all translation with specified locale. if we call
$model->deleteTranslatableWithLocale("language_code_2");
the result will as below
[
"language_code_1"=>[
"attribute_1" => "value 1",
"attribute_2" => "value 2",
]
]