Last locale always added even if null #290
jeffreybitpuma
started this conversation in
Ideas
Replies: 2 comments
-
You can extend Here is the patched trait: PS: It also contains patch for the issue #273. You can remove that line if you don't want it to behave like that. <?php
namespace App\Traits;
use Illuminate\Support\Str;
use Spatie\Translatable\Events\TranslationHasBeenSet;
use Spatie\Translatable\HasTranslations;
trait HasTranslationsFixed
{
use HasTranslations {
HasTranslations::setTranslation as faultySetTranslation;
}
public function setTranslation(string $key, string $locale, $value): self
{
$this->guardAgainstNonTranslatableAttribute($key);
$translations = $this->getTranslations($key);
$oldValue = $translations[$locale] ?? '';
if ($this->hasSetMutator($key)) {
$method = 'set' . Str::studly($key) . 'Attribute';
$this->{$method}($value, $locale);
$value = $this->attributes[$key];
}
$translations[$locale] = $value;
// fixes https://github.com/spatie/laravel-translatable/discussions/290
$translations = array_filter($translations, function ($value) {
return $value !== null;
});
// fixes https://github.com/spatie/laravel-translatable/discussions/273
$this->attributes[$key] = json_encode($translations, JSON_UNESCAPED_UNICODE);
// $this->attributes[$key] = $this->asJson($translations);
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value));
return $this;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
I made a pull request to fix this: #427 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When you add this array as data:
[ 'en' => 'Title', 'nl' => null, 'de' => null, ]
This is saved to the DB:
{en: 'Title', de: null}
This happens because setTranslation gets the translations and filters the null values. As a result this does not happen for the last one.
I think they should all be added as null or none of the null values should be added.
Beta Was this translation helpful? Give feedback.
All reactions