Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 484
Koeno Avatar asked Mar 23 '15 12:03

Koeno


People also ask

What is Trans () in laravel?

The trans helper function is used to return the translation for the given key. It defines a $key parameter which corresponds to an array key within the group file.

What is {{ __ }} In laravel?

Laravel 5 Translation Using the Double Underscore (__) Helper Function. The __ helper function can be used to retrieve lines of text from language files.

What is @lang in laravel?

Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.


1 Answers

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.

}
like image 146
Kalhan.Toress Avatar answered Sep 21 '22 05:09

Kalhan.Toress