Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roulendz's avatar

Translation default fallback

For faster translation, I used this helper function to save time, if the translation does not exist just show default string.

if (! function_exists('tr')) {
    function tr($id, $fallback, $parameters = [], $domain = 'messages', $locale = null)
    {
        return ($id === ($translation = trans($id, $parameters, $domain, $locale))) ? $fallback : $translation;
    }
}

For example

{{ tr('frontend/wellcome.message', 'Wellcome to our new website') }}

In Laravel 5.3 all worked well but since 5.4 this helper function does not work.

I can not figure out where could be the problem!

0 likes
1 reply
roulendz's avatar

I found this


@lang('frontend/wellcome.message')

It kinda works, but if I have no translations, it just echo

frontend/wellcome.message

Can I somehow set default for @lang if no translations exist?

Please or to participate in this conversation.