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

unikat's avatar

Determining if a translation in specific locale exists

How can I check if a translation exists for the specific locale?

I've tried to use the Lang::has($key, $locale) but it doesn't work as intended. No matter what $locale is passed as a second argument, if the translation doesn't exist, it always fallback to the default locale and always returns TRUE:

// /lang/en/messages.php
return [
    'test' => 'ok'
]
// /lang/de/messages.php
return [
]
dd(Lang::has('messages.test'), 'en') // true
dd(Lang::has('messages.test'), 'de') // true !!! It should return FALSE
0 likes
20 replies
RachidLaasri's avatar

Maybe because you are adding the param to the dd function?

dd(Lang::has('messages.test', 'en'))
dd(Lang::has('messages.test', 'de'))
unikat's avatar

@RachidLaasri It was just a typo.

It's not working, because if the translation is not available in 'de' locale, the method fallbacks to the default 'en' locale and always returns true.

bobbybouwmann's avatar

That's how the translations work. It falls back to the default. That is not odd behaviour

unikat's avatar

I got that. I'm looking for a way to check if translation exist for specific locale. Any ideas?

So far I'm using the following:

// /lang/en/messages.php
return [
    'test' => 'ok'
]
// /lang/de/messages.php
return [
    'test' => ''
]
dd(empty(Lang::get('messages.test', [], 'de'))) // if true then translation doesn't exist!

but it's ugly...

bobbybouwmann's avatar

You can use Lang::has

if (Lang::has('messages.test', 'de')) {
    // Do your thing
}
unikat's avatar

@bobbybouwmann That's exactly what I did at first. See the top of the thread.

Lang::has() is not working and always returns true no matter if you have a translation or not in de locale.

bobbybouwmann's avatar

Look at your example and look at my example

dd(Lang::has('messages.text'), 'de');
dd(Lang::has('messages.text', 'de'));

See the difference? You have your brackets incorrect!!

unikat's avatar

As I already said: It was just a typo. It's not working - try it yourself and you'll see.

unikat's avatar

I'm using Laravel 5.1

Yes, the method exist but it's not working as intended. Or maybe it is, but not exactly the way I need.

In short:

  1. this method can't be used to check if translation key exist for a given locale and I need exactly that.

  2. this method can be used to ensure there always is a translation key even if that key is in the default/fallback locale, but I don't need that.

bobbybouwmann's avatar

I see now that it isn't working like the function name says! I dived into the code and found this

foreach ($this->parseLocale($locale) as $locale) {
            $this->load($namespace, $group, $locale);

            $line = $this->getLine(
                $namespace, $group, $locale, $item, $replace
            );

            if (! is_null($line)) {
                break;
            }
        }

However the function $this->parseLocale returns multiple locals and therefore it can return the alternative language as well!

unikat's avatar

Still, I can't use this method because it returns the same message key (eg. 'messages.text'), only if it doesn't find the translation in anyone of the locales including the default/fallback locale.

Obviously the default/fallback locale is always present and contains all the message keys.

RachidLaasri's avatar
Level 41

Try this one :

/**
 * Determine if phrase exist.
 * 
 * @param  string $phrase
 * @param  string $lang
 * @param  string $default
 * @return boolean
 */
function langHas($phrase, $lang, $default = 'en')
{
    if(Lang::has($phrase, $lang) == false || Lang::get($phrase, $lang) == Lang::get($phrase, $default)){
        return false;
    }

    return true;
}
unikat's avatar

@RachidLaasri This function doesn't work.

Also you've forgotten the second argument in the Lang::get() method.

RachidLaasri's avatar
This function doesn't work.

Does not help at all, good luck with your problem :)

unikat's avatar

Well, I didn't mean to be offensive, but have you tested it, because I did and it simply isn't working.?

Edit:

Actually the method works and I will accept this answer!!!

However there is one caveat: if your phrase in lets say de locale happens to be the same like in the en (default) locale then this method will fail.

Thank you.

unikat's avatar

Great. I hope Taylor will accept the pull request.

Please or to participate in this conversation.