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
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.
Laravel 5 Translation Using the Double Underscore (__) Helper Function. The __ helper function can be used to retrieve lines of text from language files.
In Laravel, a controller is in the 'app/Http/Controllers' directory.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With