diff --git a/README.md b/README.md index 6b04000e..59f97988 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,27 @@ $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post ``` +#### Filling multiple translations wrapped + +You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file: +```php +'translations_wrapper' => 'translations', +``` + +Then just wrap multiple locales using that property: +```php +$data = [ + 'author' => 'Gummibeer', + 'translations' => [ + 'en' => ['title' => 'My first post'], + 'fr' => ['title' => 'Mon premier post'], + ], +]; +$post = Post::create($data); + +echo $post->translate('fr')->title; // Mon premier post +``` + ## Tutorials - [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent) diff --git a/docs/README.md b/docs/README.md index 124a834f..6a9ff4a1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -57,6 +57,27 @@ $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post ``` +#### Filling multiple translations wrapped + +You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file: +```php +'translations_wrapper' => 'translations', +``` + +Then just wrap multiple locales using that property: +```php +$data = [ + 'author' => 'Gummibeer', + 'translations' => [ + 'en' => ['title' => 'My first post'], + 'fr' => ['title' => 'Mon premier post'], + ], +]; +$post = Post::create($data); + +echo $post->translate('fr')->title; // Mon premier post +``` + ## Tutorials - [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 41f5f8d4..ac79f7fd 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -115,6 +115,9 @@ public function deleteTranslations($locales = null): void public function fill(array $attributes) { foreach ($attributes as $key => $values) { + if ($this->isWrapperAttribute($key)) { + $this->fill($values); + } if ( $this->getLocalesHelper()->has($key) && is_array($values) @@ -279,6 +282,11 @@ public function isTranslationAttribute(string $key): bool return in_array($key, $this->translatedAttributes); } + public function isWrapperAttribute(string $key): bool + { + return $key === config('translatable.translations_wrapper'); + } + public function replicateWithTranslations(?array $except = null): Model { $newInstance = $this->replicate($except); diff --git a/src/config/translatable.php b/src/config/translatable.php index ddf71d08..a1998504 100644 --- a/src/config/translatable.php +++ b/src/config/translatable.php @@ -146,4 +146,16 @@ 'prefix' => '%', 'suffix' => '%', ], + + /* + |-------------------------------------------------------------------------- + | Translation Wrapper + |-------------------------------------------------------------------------- + | Defines the wrapper for translations when creating multiple translations. + | It is set to null by default, so each locale will be model's property. + | If you want to wrap the translations with their respective locales inside + | a separate model's property, just set it here. + | + */ + 'translations_wrapper' => null, ]; diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index ed55ec5d..bde2517f 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -224,6 +224,28 @@ public function it_creates_translations_using_mass_assignment_and_locales(): voi self::assertEquals('Pois', $vegetable->translate('fr')->name); } + #[Test] + public function it_creates_translations_using_wrapped_mass_assignment_and_locales(): void + { + $this->app->make('config')->set('translatable.translations_wrapper', '_translation_wrapper'); + + $vegetable = Vegetable::create([ + 'quantity' => 5, + '_translation_wrapper' => [ + 'en' => ['name' => 'Peas'], + 'fr' => ['name' => 'Pois'], + ], + ]); + + self::assertEquals(5, $vegetable->quantity); + self::assertEquals('Peas', $vegetable->translate('en')->name); + self::assertEquals('Pois', $vegetable->translate('fr')->name); + + $vegetable = Vegetable::first(); + self::assertEquals('Peas', $vegetable->translate('en')->name); + self::assertEquals('Pois', $vegetable->translate('fr')->name); + } + #[Test] public function it_skips_mass_assignment_if_attributes_non_fillable(): void {