Home > OS >  Laravel 9 app (upgraded from 8) lang directory not working as expected
Laravel 9 app (upgraded from 8) lang directory not working as expected

Time:02-12

I have upgraded my Laravel 8 application to version 9, and according to the docs: upgrade guide, the resources/lang directory is now located in the root project directory (lang).

I've moved the lang directory to the root directory of my project, but it does not seem to work.

// config/app.php
'locale' => 'pt-BR',

and

// lang/pt-BR/messages.php
return [
    'welcome' => 'Welcome to the app!',
];

Controller

return response()->json([
    'message' => Lang::get('messages.welcome') // it returns "messages.welcome"
]);

But when I change the lang directory back to /resources/lang, it works fine like in previous laravel versions. So I created a new fresh project of Laravel 9, and it worked, which leads me to think that some additional configuration is needed, but it's not documented in the upgrade guide. My composer.json dependencies are precisely the same as the new laravel project. Is there any additional configuration that needs to be done for Laravel to recognize the directory?

CodePudding user response:

You could hardcode your lang path using the useLangPath method in your AppServiceProvider's boot method.

# app/Providers/AppServiceProvider.php

public function boot()
{
    app()->useLangPath(base_path('lang'));

    // or app()->useLangPath(app()->basePath('lang'));
}

CodePudding user response:

So I found the solution, the "problem" was that I didn't removed the resources/lang directory so Laravel was using that directory instead of the lang directory in the base path of the application.

After removing the resources/lang directory it worked as expected. Thanks to @lagbox comment.

@IGP answer also worked.

  • Related