Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use a wrapper when creating multiple translations #409

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
artemiyone marked this conversation as resolved.
Show resolved Hide resolved
|
*/
'translations_wrapper' => null,
];
22 changes: 22 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down