Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

carrick's avatar

The "dd( LaravelLocalization::getSupportedLocales() );" shows "Non-static method Mcamara\LaravelLocalization\LaravelLocalization::getSupportedLocales() should not be called statically".

lostdreamer_nl's avatar
dd( \LaravelLocalization::getSupportedLocales() );  // <-- added a \  in front of the classname

It seems you have the following line in that class instead of using the facade:

use Mcamara\LaravelLocalization\LaravelLocalization;

You should remove that line.

As for your last comment: "but the site content is not translated."

Do you have translation files setup and using them in your views? Or are you magically expecting the website to translate itself ?

1 like
carrick's avatar

Like that shows:

array:3 [▼
  "en" => array:4 [▶]
  "es" => array:4 [▶]
  "fr" => array:4 [▶]
]

Can you explain what do you mean by translation files?

lostdreamer_nl's avatar

a website does not automagically translate itself.

https://laravel.com/docs/5.6/localization

You need to setup the language files with the correct phrases that your application uses for each language.

in your views you then use those language keys instead of the actual texts, and when you switch the language variable, it uses the phrases from the other files.

1 like
carrick's avatar

Thanks, but so if the site has dynamic content, for example, the users can introduce any content, how to know what are the texts to translate? Is not possible to translate?

lostdreamer_nl's avatar

no, unless you simply use something like google translate....

or let the users translate it themselves :p

carrick's avatar

I removed the package and installed again and it shows the error "Class 'LaravelLocalization' not found ". Do you know why?



<?php

namespace App\Http\Middleware;

use Closure;
use Mcamara\LaravelLocalization\LaravelLocalization;


class Translate
{
    protected $default = 'en';

    protected $languages = [
        'en',
        'nl',
        'fr',
    ];

    public function handle($request, Closure $next)
    {
        // add the following line:
        //dd($request->has('language'), $request->language, $request->all());

        if($request->has('language')) {
            $lang = $request->language;
            if(array_search($lang, $this->languages) === false) {
                $lang = $this->default;
            }

            $request->session()->put('lang', $lang);
        }

        \LaravelLocalization::setLocale( $request->session()->get('lang') );

        return $next($request);
    }

}

It also shows "undefiend namespace LaravelLocalization".

lostdreamer_nl's avatar

"I removed the package and installed again "

Did you also add the facade to your config?

1 like
carrick's avatar

Thanks, I installed with composer update, but it seems that is necessary to use composer required.

carrick's avatar

For example, I have a menu that has a link "Search". In English it appears "Search", when the user clicks in "French" in the select menu it should appear "Chercher". Is necessary to create a folder "fr" inside "lang" folder.

But then is necessary to create some file like test.php and then return some values in an array? And after that instead of "Search" in the menu item what should be placed?

<li class="nav-item">
    <a class="nav-link" href="{{ route('search.index') }}">Search</a>
</li>
lostdreamer_nl's avatar

"Is necessary to create a folder "fr" inside "lang" folder"

  • Yes "But then is necessary to create some file like test.php and then return some values in an array?"
  • Yes " And after that instead of "Search" in the menu item what should be placed?"
  • Yes

I refer you back to the documentation:

https://laravel.com/docs/5.6/localization#retrieving-translation-strings

// View
<li class="nav-item">
    <a class="nav-link" href="{{ route('search.index') }}">@lang('forms.search')</a>
</li>

// lang/en/forms.php:
return [
    'search' => 'Search',
];
// lang/fr/forms.php:
return [
    'search' => 'Chercher',
];
1 like
Previous

Please or to participate in this conversation.