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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Marcelo The Mage Coder
  • 1,944
  • 2
  • 11
  • 29
  • 1
    that looks like it is talking about packages publishing their files for new laravel 9 projects – lagbox Feb 11 '22 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 – Marcelo The Mage Coder Feb 11 '22 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 – Rory Feb 11 '22 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 – lagbox Feb 11 '22 at 18:48
  • @lagbox but what if I want to use the new directory? – Marcelo The Mage Coder Feb 11 '22 at 19:11
  • 8
    make sure that the directory `resources/lang` doesn't exist ... based on the code in the Application Container: https://github.com/laravel/framework/blob/9.x/src/Illuminate/Foundation/Application.php#L324 – lagbox Feb 11 '22 at 19:18

3 Answers3

28

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.

Marcelo The Mage Coder
  • 1,944
  • 2
  • 11
  • 29
  • 3
    Same problem here, the `resources/lang` directory must be removed – Roberto Santana Apr 26 '22 at 15:48
  • 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. – Dario Fumagalli Apr 14 '23 at 15:17
7

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'));
}
IGP
  • 14,160
  • 4
  • 26
  • 43
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.