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

keys with empty string value are deleted

FrancoisDucobu opened this issue · comments

Hello everyone,

I'm facing an issue.
I found where this issue is happening but I don't know why the package do this behavior.

I try to save an empty string in the json but the package remove the associated key value.

[ 'name' => [ 'lb' => 'Urne *', 'de_BE' => 'Urne *', 'de_CH' => 'Urne *', 'de_DE' => 'Urne *', 'en_UK' => 'Urne *', 'fr_BE' => 'Urne *', 'fr_CH' => 'Urne *', 'fr_FR' => 'Urne ', 'fr_LU' => 'Urne *', 'it_CH' => 'Urne *', 'it_IT' => 'Urne *', 'nl_BE' => 'Urne *', 'nl_NL' => 'Urne *', 'pt_PT' => 'Urne *', 'sp_SP' => 'Urne *', ], 'remarks' => [ 'lb' => 'TEST', 'de_BE' => 'TEST', 'de_CH' => 'TEST', 'de_DE' => 'TEST', 'en_UK' => 'TEST', 'fr_BE' => 'test', 'fr_CH' => 'TEST', 'fr_FR' => '', 'fr_LU' => 'TEST', 'it_CH' => 'TEST', 'it_IT' => 'TEST', 'nl_BE' => 'TEST', 'nl_NL' => 'TEST', 'pt_PT' => 'TEST', 'sp_SP' => 'TEST', ] ]

below the SQL request

update items set remarks = '{"lb":"TEST","de_BE":"TEST","de_CH":"TEST","de_DE":"TEST","en_UK":"TEST","fr_BE":"test","fr_CH":"TEST","fr_LU":"TEST","it_CH":"TEST","it_IT":"TEST","nl_BE":"TEST","nl_NL":"TEST","pt_PT":"TEST","sp_SP":"TEST"}', items.updated_at = '2024-01-08 14:33:58' where id = '01hbba8q2phxqh249j250mkpde'

we can see in the SQL request that fr_FR key is missing.

I check in the package code and I found this in the HasTranslation.php trait

`protected function filterTranslations(mixed $value = null, string $locale = null, array $allowedLocales = null): bool
{
if ($value === null) {
return false;
}

    if ($value === '') {             <-- this is where the empty string is remove from the json
        return false;
    }

    if ($allowedLocales === null) {
        return true;
    }

    if (! in_array($locale, $allowedLocales)) {
        return false;
    }

    return true;
}`

Is this the behaviour intended by the package?

Regards,

francois