dimsav / laravel-translatable

[Deprecated] A Laravel package for multilingual models
MIT License
1.95k stars 320 forks source link

How to choose lang when use create() method #485

Closed salipro4ever closed 6 years ago

salipro4ever commented 6 years ago

In case, it is a instance, i can assign a language to save:

$greece->translate('en')->name = 'No name';

When use mass create() method, is there any way to do the same thing? Example:

MyModel::translate('en')->create([...])

If use Filling multiple translations doc, i must change format of data. I dont want that.

My temporary solution:

App::setLocale('jp');
MyModel::create([...])
App::setLocale('en');
Gummibeer commented 6 years ago

This puts all data on the models during create() https://github.com/dimsav/laravel-translatable/blob/5f19c5b9253ea49baa935135ae2ffd37f81bc5d4/src/Translatable/Translatable.php#L280-L296

So you can use one of the following ways:

MyModel::create([
    'en'  => ['name' => 'Greece'],
    'fr'  => ['name' => 'Grèce'],
]);
MyModel::create([
    'name:en' => 'Greece',
    'name:fr' => 'Grèce',
]);
$model = new MyModel();
$model->setDefaultLocale('jp')->fill($data)->save();
$model->setDefaultLocale(null);
App::setLocale('jp');
MyModel::create([...])
App::setLocale('en');