Search code examples
laravellaravel-4translation

Check line existence in laravel's trans()


Lets say in my lang/en/general.php there are multiple translation lines for example:

"token" => "This password reset token is invalid.",

"sent" => "Password reminder sent!",

"reset" => "Password has been reset!",

But in my lang/de/general.php these lines are missing.

So later, when I use the Lang::get('general.token') or simply trans('general.token')

The english version will return

This password reset token is invalid.

And the german (de) version will return

general.token

Is there any way I can handle a 'translation not found' function, like a filter but not creating a special class for it? For example, when a line has no translation, I want to throw an Exception.

Thanks in advance!


Solution

  • In Laravel 4 only, you can use Lang::has() as below, here is the doc

    if (\Lang::has('general.token')) {
    
        // line exists.
    
    } else {
    
       // line not exist.
    
    }