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

Viewed 2417

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?

3 Answers

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.

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'));
}

As per documentation you should use 'locale' => 'pt_BR' in config/app.php and name your lang folder lang/pt_BR/messages.php, (underscore) not pt-BR.

for languages that differ by territory, you should name the language directories according to the ISO 15897. For example, en_GB should be used for British English rather than en-gb.

Related