Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access my lang files from controller in laravel 5

Converting from Laravel 4 to Laravel 5. Trying to access Lang file in controller like so:

$var = Lang::get('directory/index.str1');

That gives me: Class 'App\Http\Controllers\Lang' not found. However

{{Lang::get('directory/index.str1');}}

Works fine in a view

like image 907
protagonist Avatar asked Jul 30 '15 16:07

protagonist


People also ask

Where is Lang file in Laravel?

Language Files Language strings are stored in files within the resources/lang directory. Within this directory there should be a subdirectory for each language supported by the application.

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.

Where are controllers file located in Laravel?

In Laravel, a controller is in the 'app/Http/Controllers' directory.

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

You can as well use the __ helper (Works for Laravel 5.5, 5.6 and 5.7 ... not sure about the other versions). e. g if your array of strings are stored in a file called messages.php inside lang directory, to get a string with the key myString, use the following in the controller:

__('messages.myString');

In blade template you would use:

@lang('messages.myString')
like image 125
Benjamin Mwendwa Munyoki Avatar answered Oct 17 '22 00:10

Benjamin Mwendwa Munyoki