diff --git a/src/HasTranslations.php b/src/HasTranslations.php index b6ce506..3f0282e 100644 --- a/src/HasTranslations.php +++ b/src/HasTranslations.php @@ -49,18 +49,14 @@ protected function mutateAttributeForArray($key, $value): mixed public function setAttribute($key, $value) { - if ($this->isTranslatableAttribute($key) && is_array($value)) { - return $this->setTranslations($key, $value); - } - - // Pass arrays and untranslatable attributes to the parent method. - if (! $this->isTranslatableAttribute($key) || is_array($value)) { - return parent::setAttribute($key, $value); + if ($this->isTranslatableAttribute($key)) { + if (is_array($value) && ! array_is_list($value)) { + return $this->setTranslations($key, $value); + } + return $this->setTranslation($key, $this->getLocale(), $value); } - // If the attribute is translatable and not already translated, set a - // translation for the current app locale. - return $this->setTranslation($key, $this->getLocale(), $value); + return parent::setAttribute($key, $value); } public function translate(string $key, string $locale = '', bool $useFallbackLocale = true): mixed diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 6196882..7597115 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -906,3 +906,17 @@ public function setAttributesExternally(array $attributes) expect($translations)->toEqual([]); }); + +it('can set an array list as value for translation using `setTranslation`', function () { + $this->testModel->setTranslation('name', 'en', ['testValue_en']); + $this->testModel->save(); + + expect($this->testModel->getTranslation('name', 'en'))->toEqual(['testValue_en']); +}); + +it('can set an array list as value for translation using default local', function () { + $this->testModel->name = ['testValue_en']; + $this->testModel->save(); + + expect($this->testModel->getTranslation('name', 'en'))->toEqual(['testValue_en']); +});