15

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?

6
  • 1
    that looks like it is talking about packages publishing their files for new laravel 9 projects Commented Feb 11, 2022 at 18:27
  • @lagbox there is no resources/lang directory in a new Laravel 9 project, so every language file should be inside the lang directory in the root of the project, not just packages files Commented Feb 11, 2022 at 18:40
  • 1
    @MarceloTheMageCoder but for upgrades, I also read that as only a change for package developers. It should be fine to leave the directory as /resources/lang Commented Feb 11, 2022 at 18:42
  • you don't have a "new" laravel 9 project you have an upgraded project ... the upgrade guide is talking about if you have a package publishing those files that you should use that method instead of hardcoding it, as it could be in different directories ... those paths are set by the Application Container itself so it shouldn't be something you would have had to change; there is code in the Laravel 9 version that checks if the lang folder "exists" at the original location (is that path a directory) still and it will use it there otherwise use the new location Commented Feb 11, 2022 at 18:48
  • 8
    make sure that the directory resources/lang doesn't exist ... based on the code in the Application Container: github.com/laravel/framework/blob/9.x/src/Illuminate/Foundation/… Commented Feb 11, 2022 at 19:18

3 Answers 3

40

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Same problem here, the resources/lang directory must be removed
It just happened to me. I installed a self-proclaimed "Laravel 10 compatible" package. However, it published its lang files inside resources/lang and this makes the whole Laravel 10 go bonkers and try to use this outdated directory instead of the new /lang directory.
8

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.