Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.22k stars 152 forks source link

Translation relation not working with fallback... #392

Closed yooplait closed 4 months ago

yooplait commented 4 months ago

Hi,

app()->setLocale('en');

Herb::with('translation')->first()->translation;

translation is null... the default fallback fr not loading ?

Only working like this...

Herb::query()->first()->translation

Thank you

Eyad-Bereh commented 4 months ago

You need to configure the package to load the translation of the default fallback. You can do this on application level by opening the translatable.php configuration file and setting the use_fallback key to true, for example:

    /*
    |--------------------------------------------------------------------------
    | Use fallback
    |--------------------------------------------------------------------------
    |
    | Determine if fallback locales are returned by default or not. To add
    | more flexibility and configure this option per "translatable"
    | instance, this value will be overridden by the property
    | $useTranslationFallback when defined
    |
    */
    'use_fallback' => false, // <-- change this to true

Or you can control this behavior on a per-model basis by defining a $useTranslationFallback attribute on your model, for example:

class Herb extends Model implements TranslatableContract
{
    use Translatable;

    private $useTranslationFallback = true; // <-- notice this addition
}

Learn more about it here: https://docs.astrotomic.info/laravel-translatable/package/fallback-locale#per-model

yooplait commented 4 months ago

You need to configure the package to load the translation of the default fallback. You can do this on application level by opening the translatable.php configuration file and setting the use_fallback key to true, for example:

    /*
    |--------------------------------------------------------------------------
    | Use fallback
    |--------------------------------------------------------------------------
    |
    | Determine if fallback locales are returned by default or not. To add
    | more flexibility and configure this option per "translatable"
    | instance, this value will be overridden by the property
    | $useTranslationFallback when defined
    |
    */
    'use_fallback' => false, // <-- change this to true

Or you can control this behavior on a per-model basis by defining a $useTranslationFallback attribute on your model, for example:

class Herb extends Model implements TranslatableContract
{
    use Translatable;

    private $useTranslationFallback = true; // <-- notice this addition
}

Learn more about it here: https://docs.astrotomic.info/laravel-translatable/package/fallback-locale#per-model

Thank you for your response :)

Actually, no, it doesn't work, unfortunately:

Herb::query()->with('translation')->first();

This will return null for the loaded relation (if it needs to load the fallback).

Herb::query()->first()->translation;

This will work :)

It must be a global issue (and very few must be eager loading the translation relation not to have noticed it - or is it normal).

Thanks again :)

Eyad-Bereh commented 4 months ago

Do you mind sharing the full code for your Herb model and HerbTranslation model and your translatable.php file and the value of locale and fallback_locale in your app.php file ?

I need to get to the bottom of this, then maybe we can submit a pull request to fix this bug if possible.

I would highly appreciate it!

yooplait commented 4 months ago

Do you mind sharing the full code for your Herb model and HerbTranslation model and your translatable.php file and the value of locale and fallback_locale in your app.php file ?

I need to get to the bottom of this, then maybe we can submit a pull request to fix this bug if possible.

I would highly appreciate it!

It's fixed, thank you :)

But with big table, orderByTrasnlation is kind of slow unfortunately...

Eyad-Bereh commented 4 months ago

It's fixed, thank you :)

I'm glad to hear this. you're more than welcome!

But with big table, orderByTrasnlation is kind of slow unfortunately...

Regarding this point, please close the current issue (in case it's solved) and open a new issue regarding the problem and provide some details there (e.g. schema definition of translations table, number of rows inside the translations table, number of used locales), and I'll be more than happy to help you again.

However, as a quick guess please make sure to put an index on your locale column inside the translations table, for example:

$table->string('locale')->index();
yooplait commented 4 months ago

It's fixed, thank you :)

I'm glad to hear this. you're more than welcome!

But with big table, orderByTrasnlation is kind of slow unfortunately...

Regarding this point, please close the current issue (in case it's solved) and open a new issue regarding the problem and provide some details there (e.g. schema definition of translations table, number of rows inside the translations table, number of used locales), and I'll be more than happy to help you again.

However, as a quick guess please make sure to put an index on your locale column inside the translations table, for example:

$table->string('locale')->index();

Thank you very much for your help and your response, I will make another message :)