Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fallback locale on a per model basis #380

Merged
merged 3 commits into from
Mar 19, 2023

Conversation

yoeriboven
Copy link
Contributor

@yoeriboven yoeriboven commented Mar 15, 2023

In our project we use this package on multiple models. Almost all of them need a fallback language except a new one. We access the attribute using $model->translatableAttribute but this always uses the fallback language.

PR adds the possibility to add a useFallbackLocale property to the model.

class Article extends Model
{
    use HasTranslations;

    protected $useFallbackLocale = false;
}

This was my solution without this PR:

Translatable::fallback(missingKeyCallback: function (
    Model $model,
    string $translationKey,
    string $locale,
    string $fallbackTranslation,
    string $fallbackLocale,
) {
    if ($model instanceof NewsNotification) {
        return '';
    }

    return $fallbackTranslation;
});

Feels like a lot of code to disable fallback language.

@freekmurze
Copy link
Member

Could you document this in the readme with a clear example?

@yoeriboven
Copy link
Contributor Author

yoeriboven commented Mar 17, 2023

Hope the docs are good. It's quite simple so let me know if you want me to add something.

We need this for a notification with a title and a CTA (simplified). Maybe the Dutch notification has only a title (the button/CTA would be optional) but the English notification has a title and CTA.

In that case Dutch users would see the Dutch title with the English CTA even though they should not see the button.

@freekmurze freekmurze merged commit f472fba into spatie:main Mar 19, 2023
@freekmurze
Copy link
Member

Thanks!

@yoeriboven yoeriboven deleted the fallback-per-model branch March 19, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants