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

Antonella's avatar

Class 'App\Providers\SwitchLocale' not found

i installed this package on laravel nova https://github.com/u12206050/switch-locale.

after following the instructions to the letter I get the following error as soon as I load the following code into the provider:

	public function tools()
{
    return [
        new SwitchLocale([
            "locales" => [
                "en" => "English",
                "de" => "German"
            ],
            "useFallback" => false,
            "customDetailToolbar" => false //optional
        ])
    ];
}

the interesting thing is that I always get the same error. whether it has the package installed or not the thing that triggers the error is that you load that code into the provider

this following error:

Error Class 'App\Providers\SwitchLocale' not found

0 likes
7 replies
Talinon's avatar

@gianmarx You still need to tell your provider where to find that class.. so you need to either use the full namespace, or import it at the top of the class:

use Day4\SwitchLocale\SwitchLocale;

or:

new \Day4\SwitchLocale\SwitchLocale([
	//...
Antonella's avatar

I imported the classes as you see in my resources but I always get the same result:

<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\BooleanGroup;
use Day4\SwitchLocale\SwitchLocale;


class User extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\User::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'name';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'name', 'email',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Gravatar::make()->maxWidth(50),

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255'),

            Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:users,email')
                ->updateRules('unique:users,email,{{resourceId}}'),

            Password::make('Password')
                ->onlyOnForms()
                ->creationRules('required', 'string', 'min:8')
                ->updateRules('nullable', 'string', 'min:8'),
            Text::make('Api_token')
                ->sortable()
                ->rules('required', 'api_token', 'max:254')
                ->updateRules('unique:users,email,{{resourceId}}','api_token'),

            BooleanGroup::make(__('Allowed Locale'), 'locale')->options(SwitchLocale::getLocales()),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

@talinon

Talinon's avatar

@gianmarx That's code from your App\Nova\User class.. your error is apparently coming from the Service Provider.

You need to import or use the full namespace in the file wherever tools() is defined... which I suspect is app\Providers\NovaServiceProvider.php

Antonella's avatar

I changed the service Provider following the guide. so i changed the NovaProvider as follows:

public function tools()
{
    return [
        new SwitchLocale([
            "locales" => [
                "en" => "English",
                "de" => "German"
            ],
            "useFallback" => false,
            "customDetailToolbar" => false //optional
        ])
    ];
}

I repeat the problem that when I load the provider here in the tools that has installed the package or not it always gives the usual error

@talinon

Talinon's avatar

@gianmarx The error is very straight forward. It doesn't know what SwitchLocale is. It has nothing to do with the package, Laravel, or the Service Provider.. it is a basic PHP error that is saying it can't find SwitchLocale because you haven't told it where to find it. It doesn't matter if you imported it into your User resource, that is a different class and has nothing to do with your current error.

So I'll explain again.. you need to either import SwitchLocale into the class where tools() is defined, or supply the full namespace. I already pasted the exact line for you, but I'll provide the entire method this time:

public function tools()
{
    return [
        new \Day4\SwitchLocale\SwitchLocale([
            "locales" => [
                "en" => "English",
                "de" => "German"
            ],
            "useFallback" => false,
            "customDetailToolbar" => false //optional
        ])
    ];
}

I suspect the guide either missed that information, or assumed you knew to import it.

Antonella's avatar

give me same error:

Error

Class 'Day4\SwitchLocale\SwitchLocale\SwitchLocale' not found

@talinon

Talinon's avatar
Talinon
Best Answer
Level 51

@gianmarx

Sorry, that was a typo.. should be new \Day4\SwitchLocale\SwitchLocale([.. I'll update the snippet

Getting yourself a coding editor that automatically resolves the namespace can help in these situations.

Please or to participate in this conversation.