Skip to content

Commit 8ef8e7f

Browse files
committed
Allow to use a wrapper when creating multiple translations
1 parent 0f6f7e7 commit 8ef8e7f

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ $post = Post::create($data);
6868
echo $post->translate('fr')->title; // Mon premier post
6969
```
7070

71+
### Filling multiple translations wrapped
72+
73+
You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file:
74+
```php
75+
'translations_wrapper' => 'translations',
76+
```
77+
78+
Then just wrap multiple locales using that property:
79+
```php
80+
$data = [
81+
'author' => 'Gummibeer',
82+
'translations' => [
83+
'en' => ['title' => 'My first post'],
84+
'fr' => ['title' => 'Mon premier post'],
85+
],
86+
];
87+
$post = Post::create($data);
88+
89+
echo $post->translate('fr')->title; // Mon premier post
90+
```
91+
7192
## Tutorials
7293

7394
- [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent)

docs/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ $post = Post::create($data);
5757
echo $post->translate('fr')->title; // Mon premier post
5858
```
5959

60+
### Filling multiple translations wrapped
61+
62+
You may define a wrapper property when creating new translations. Set the `translations_wrapper` property in translatable config file:
63+
```php
64+
'translations_wrapper' => 'translations',
65+
```
66+
67+
Then just wrap multiple locales using that property:
68+
```php
69+
$data = [
70+
'author' => 'Gummibeer',
71+
'translations' => [
72+
'en' => ['title' => 'My first post'],
73+
'fr' => ['title' => 'Mon premier post'],
74+
],
75+
];
76+
$post = Post::create($data);
77+
78+
echo $post->translate('fr')->title; // Mon premier post
79+
```
80+
6081
## Tutorials
6182

6283
- [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent)

src/Translatable/Translatable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public function deleteTranslations($locales = null): void
115115
public function fill(array $attributes)
116116
{
117117
foreach ($attributes as $key => $values) {
118+
if ($this->isWrapperAttribute($key)) {
119+
$this->fill($values);
120+
}
118121
if (
119122
$this->getLocalesHelper()->has($key)
120123
&& is_array($values)
@@ -279,6 +282,11 @@ public function isTranslationAttribute(string $key): bool
279282
return in_array($key, $this->translatedAttributes);
280283
}
281284

285+
public function isWrapperAttribute(string $key): bool
286+
{
287+
return $key === config('translatable.translations_wrapper');
288+
}
289+
282290
public function replicateWithTranslations(?array $except = null): Model
283291
{
284292
$newInstance = $this->replicate($except);

src/config/translatable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,16 @@
146146
'prefix' => '%',
147147
'suffix' => '%',
148148
],
149+
150+
/*
151+
|--------------------------------------------------------------------------
152+
| Translation Wrapper
153+
|--------------------------------------------------------------------------
154+
| Defines the wrapper for translations when creating multiple translations.
155+
| It is set to null by default, so each locale will be model's property.
156+
| If you want to wrap the translations with their respective locales inside
157+
| a separate model's property, just set it here.
158+
|
159+
*/
160+
'translations_wrapper' => null,
149161
];

tests/TranslatableTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,28 @@ public function it_creates_translations_using_mass_assignment_and_locales(): voi
224224
self::assertEquals('Pois', $vegetable->translate('fr')->name);
225225
}
226226

227+
#[Test]
228+
public function it_creates_translations_using_wrapped_mass_assignment_and_locales(): void
229+
{
230+
$this->app->make('config')->set('translatable.translations_wrapper', 'translations');
231+
232+
$vegetable = Vegetable::create([
233+
'quantity' => 5,
234+
'translations' => [
235+
'en' => ['name' => 'Peas'],
236+
'fr' => ['name' => 'Pois'],
237+
],
238+
]);
239+
240+
self::assertEquals(5, $vegetable->quantity);
241+
self::assertEquals('Peas', $vegetable->translate('en')->name);
242+
self::assertEquals('Pois', $vegetable->translate('fr')->name);
243+
244+
$vegetable = Vegetable::first();
245+
self::assertEquals('Peas', $vegetable->translate('en')->name);
246+
self::assertEquals('Pois', $vegetable->translate('fr')->name);
247+
}
248+
227249
#[Test]
228250
public function it_skips_mass_assignment_if_attributes_non_fillable(): void
229251
{

0 commit comments

Comments
 (0)