From 8ef8e7ffb99f18f5cc30e2be0d43cd50224c68e4 Mon Sep 17 00:00:00 2001 From: Artemiy Date: Tue, 7 May 2024 12:33:16 +0200 Subject: [PATCH 1/2] Allow to use a wrapper when creating multiple translations --- README.md | 21 +++++++++++++++++++++ docs/README.md | 21 +++++++++++++++++++++ src/Translatable/Translatable.php | 8 ++++++++ src/config/translatable.php | 12 ++++++++++++ tests/TranslatableTest.php | 22 ++++++++++++++++++++++ 5 files changed, 84 insertions(+) diff --git a/README.md b/README.md index 6b04000e..5bf1dffe 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..8d17eed1 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..9b9d98eb 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', 'translations'); + + $vegetable = Vegetable::create([ + 'quantity' => 5, + 'translations' => [ + '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 { From 6209f81a9094cc4bdb24a5d7ec7951870c413cdb Mon Sep 17 00:00:00 2001 From: Artemiy Date: Wed, 17 Jul 2024 11:39:51 +0200 Subject: [PATCH 2/2] - Update example translations wrapper name to a more descriptive one "_translation_wrapper" - Make subtitles H4 --- README.md | 2 +- docs/README.md | 2 +- tests/TranslatableTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5bf1dffe..59f97988 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post ``` -### Filling multiple translations wrapped +#### Filling multiple translations wrapped You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file: ```php diff --git a/docs/README.md b/docs/README.md index 8d17eed1..6a9ff4a1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -57,7 +57,7 @@ $post = Post::create($data); echo $post->translate('fr')->title; // Mon premier post ``` -### Filling multiple translations wrapped +#### Filling multiple translations wrapped You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file: ```php diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 9b9d98eb..bde2517f 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -227,11 +227,11 @@ public function it_creates_translations_using_mass_assignment_and_locales(): voi #[Test] public function it_creates_translations_using_wrapped_mass_assignment_and_locales(): void { - $this->app->make('config')->set('translatable.translations_wrapper', 'translations'); + $this->app->make('config')->set('translatable.translations_wrapper', '_translation_wrapper'); $vegetable = Vegetable::create([ 'quantity' => 5, - 'translations' => [ + '_translation_wrapper' => [ 'en' => ['name' => 'Peas'], 'fr' => ['name' => 'Pois'], ],