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

Add ability to return default value if no translation found

hsul4n opened this issue · comments

Hello there,

I'am wondering if some of my data can't be localized such as company names, acronym or even not translated yet.

How can I get the default value if transaction not found, should I add the two values into json localization.

What about adding property to enable/disable fallback so we can refer to default value if fallback is turned off.

For example: fallback_enabled => true|false

If I got you, I think you can add this 'fallback_locale' => 'en' in your config\translatable.php file.
If the local you want doesn't exists the fallback value will be returned

@AbdElrahmaN31,fallback_locale option already exists as shown in docs.

I mean that if one of my records is not localized I got empty rather than getting the default value.

So if we can have something like fallback_enabled option which can return the default value whenever localization fails.

Chiming in here to agree that there should be some fallback if the key is not translated yet.

For example, we have a $project->title = "Toy Story" in the DB already, and we just added translations to the title key.

If we pull $project->title (using, say en_US), and the hash hasn't been created yet, it should still return "Toy Story" instead of null.

This would be very useful so that we don't need to run a script every time we add a new translatable field.

I agree on this one, would be really handy!

At first I thought that this option is something new but I just tested it. My test had this entry in DB:

{
"en":"Actionlists",
"nl":"Actionlists"
}

And config/translatable.php has 'fallback_locale' => 'en'.

So, user can switch between English and Dutch and it will load appropriate translated strings. Then as a test I added another locale, fr and tried again. To my surprise, it loaded en. So what are you guys asking for? Since I assumed that there's no default fallback option available and we get empty string or NULL when requesting for a non-existing locale, but obviously we're getting back a default locale set in config, if requested locale is not found.

Isn't this exactly what you're asking for?

My use-case would be if we are adding translations to an existing string field.

{ title: "Toy Story" }

Would want $film->title to equal "Toy Story" however this will return null.

Once translated, this field looks like:

{ title: { "en": "Toy Story" } }

Both use-cases above should return "Toy Story" for $film->title, ideally.

So not about non-existing locale but about non-existing translations entirely, AKA when the JSON translations are not present it should return the value itself.

So if I am trying to read fr translations:

  1. Try to find fr key
  2. Use fallback en key
  3. Return value itself if no translations are present

I was having the same issue and as a solution, I have overwritten its existing trait.
Spatie\Translatable\HasTranslations

& used create a function where I store fallback lang value same as the current lang. value.

public static function setTranslationForFallbackLang($model, $attribute, $locale, $value)
    {
        if (config('translatable.fallback_locale') != null && config('translatable.fallback_locale') != $locale) {
            $model->setTranslation($attribute, config('translatable.fallback_locale'), $value);
        }
        return $model;
    }

I understand this might not be a perfect way to do this Also It might return the wrong translation but it fixes the issue of returning blank values.