spatie / laravel-translatable

Making Eloquent models translatable

Home Page:https://spatie.be/docs/laravel-translatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't find strings with accents

LBreda opened this issue · comments

Hi,

I'm using this library in Italian, and a lot of Italian words have accents. The encoded JSON strings are saved on the database with encoded unicode characters ("Thè" is encoded as "Th\u00e8"), therefore a Eloquent search for accented words does not work.

Is there any solution?

I'm thinking you could solve this by setting the correct encoding for your database (connection). I usually have to play around with it to get the right settings.

I'd accept a PR should you find a good way to handle this in the package itself.

I'll try, I set both the collation and the connection to utf8mb4(_unicode_ci), which seems pretty ok, and it can't find the ragù (yum).

Thank you very much!

commented

This is not about DB encoding.
It's about json_encode($value) in PHP escaping unicode chars by default.
In this library the setTranslation methods uses asJson method to transform the array to a json that is saved to DB.
To prevent escaping the accented characters you have to override the asJson method inside your model like this:

protected function asJson($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }