Replies: 3 comments 1 reply
-
You can fork and replace that line: https://github.com/spatie/laravel-translatable/blob/master/src/HasTranslations.php#L110 with: $this->attributes[$key] = json_encode($translations, JSON_UNESCAPED_UNICODE); and than use your forked repo instead of original package. |
Beta Was this translation helpful? Give feedback.
-
I think it's better and easier to extend trait instead of fork and use your own package. Here is the patched trait: PS: It also contains patch for the issue #290. 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.
-
You can, instead, just override namespace App\Traits;
use Spatie\Translatable\HasTranslations as BaseHasTranslations;
trait HasTranslations
{
use BaseHasTranslations;
// ...
protected function asJson($value)
{
return json_encode($value, JSON_UNESCAPED_UNICODE);
}
} Or just use another trait for |
Beta Was this translation helpful? Give feedback.
-
I use Azerbaijani and Russian letters on my website. Sometimes some letters are converted to something like this {"az":"Velosiped","ru":"\u0412\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434"} but sometimes it doesn't do this. I thought maybe it's some specific letters but it's not the case. I need to manually change it back to how it should be, but I've thousands of data. They display correctly on the website, but a user can't do a search because it won't match.
Beta Was this translation helpful? Give feedback.
All reactions