Skip to content

Commit 19c4238

Browse files
Add support to save the translation data with an alternative data structure
1 parent d6d9ab3 commit 19c4238

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Eloquent/Concerns/Translatable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public static function bootTranslatable(): void
5959
*/
6060
public function fill(array $attributes)
6161
{
62+
foreach (I18n::getLocaleKeys() as $key) {
63+
if (!isset($attributes[$key])) {
64+
continue;
65+
}
66+
67+
foreach ($this->getTranslatableAttributes() as $columnName) {
68+
if (isset($attributes[$key][$columnName])) {
69+
$this->translation(I18n::getLocale($key))->setAttribute($columnName, $attributes[$key][$columnName]);
70+
}
71+
}
72+
}
73+
6274
foreach ($this->getTranslatableAttributes() as $columnName) {
6375
if (isset($attributes[$columnName])) {
6476
$this->setAttribute($columnName, $attributes[$columnName]);

tests/Features/SavingTranslatableAttributesTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,76 @@ public function it_can_update_all_translation_records_with_single_assignment()
204204
self::assertEquals($locale->name . ' description 2', $product->description);
205205
}
206206
}
207+
208+
/** @test */
209+
public function it_can_be_filled_and_saved_with_alternative_data_structure()
210+
{
211+
$product = new Product();
212+
$product->fill([
213+
'product_category_id' => 3,
214+
'en' => [
215+
'title' => 'English title',
216+
'description' => 'English description',
217+
],
218+
'es' => [
219+
'title' => 'Spanish title',
220+
'description' => 'Spanish description',
221+
],
222+
'de' => [
223+
'title' => 'German title',
224+
'description' => 'German description',
225+
],
226+
'published' => true
227+
])->save();
228+
229+
$product = Product::orderBy('id', 'desc')->first();
230+
231+
foreach (I18n::getAllLocale() as $locale) {
232+
$product->translateTo($locale);
233+
234+
self::assertEquals($locale->name . ' title', $product->title);
235+
self::assertEquals($locale->name . ' description', $product->description);
236+
}
237+
}
238+
239+
/** @test */
240+
public function it_can_be_filled_and_updated_with_alternative_data_structure()
241+
{
242+
$original = Product::create([
243+
'product_category_id' => 3,
244+
'en' => [
245+
'title' => 'English title',
246+
'description' => 'English description',
247+
],
248+
'es' => [
249+
'title' => 'Spanish title',
250+
'description' => 'Spanish description',
251+
],
252+
'published' => true
253+
]);
254+
255+
$product = Product::find($original->id);
256+
$product->fill([
257+
'en' => [
258+
'title' => 'English title 2',
259+
'description' => 'English description 2',
260+
],
261+
'es' => [
262+
'title' => 'Spanish title 2',
263+
'description' => 'Spanish description 2',
264+
],
265+
'de' => [
266+
'title' => 'German title 2',
267+
'description' => 'German description 2',
268+
],
269+
])->save();
270+
$product = Product::find($original->id);
271+
272+
foreach (I18n::getAllLocale() as $locale) {
273+
$product->translateTo($locale);
274+
275+
self::assertEquals($locale->name . ' title 2', $product->title);
276+
self::assertEquals($locale->name . ' description 2', $product->description);
277+
}
278+
}
207279
}

0 commit comments

Comments
 (0)