prgrm prgrm - 6 days ago 5
PHP Question

I cannot retrieve locale strings in Laravel

For somewhat reason Laravel won't return the string but simply the key:

Here is my messages.php saved in /resources/lang/en/

return[
'Example' => 'Ejemplo',
'Otroejemplo' => 'Beispiel',
];


This is my routing:

Route::get('myexample/{locale}', function ($locale) {
App::setLocale($locale);
return view('admin.pages.forms.myexample');
});


And this is myexample.blade.php:

@lang('messages.Example');


I expect to get Ejemplo in return when I open localhost:8000/myexample/en but I get message.Example instead.

Answer

Tested your code, it's working fine the only issue I got is instead of

   Route::get('myexample/{locale}', function ($locale)  {
    App::setLocale($locale);
    return view('admin.pages.forms.myexample');
});

use

Route::get('myexample/{locale}', function ($locale)  {
    App::setLocale($locale);
    return view('myexample'); // in case your view is in resources folder
});
Comments