Skip to content

Commit

Permalink
Merge pull request #409 from artemiyone/main
Browse files Browse the repository at this point in the history
Allow to use a wrapper when creating multiple translations
  • Loading branch information
Gummibeer authored Aug 26, 2024
2 parents 30ef101 + 6209f81 commit 3adf03d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
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 @@ -280,6 +283,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.
|
*/
'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

0 comments on commit 3adf03d

Please sign in to comment.