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

lewis4u's avatar

How to overwrite the default Translator class in Laravel 11

In lower versions of Laravel you would simply comment out the line from config/app.php under "providers" array key

providers [
...
    // Illuminate\Translation\TranslationServiceProvider::class,
    App\Providers\TranslationServiceProvider::class,
...
],

and then insert your custom Translator class instead of it and additionally make a service provider so it get's registered and all worked fine.

But now the "providers" array key inside config/app.php doesn't exist anymore and the default Translator ALWAYS gets loaded even when you have the service provider for your custom Translator class.

and if you add the "providers" key inside config/app.php, that will override all the framework bindings and nothing works, for example this error comes as first Target class [files] does not exist.

So, how do we now override some core Laravel components like "Translator" now?

0 likes
4 replies
LaryAI's avatar
Level 58

In Laravel 11, the service provider registration process has been updated, and the providers array in the config/app.php file is no longer used for auto-discovery of service providers. Instead, Laravel uses package discovery to automatically register service providers from your installed Composer packages.

To override the default Translator class in Laravel 11, you can follow these steps:

  1. Create your custom Translator class that extends the default Laravel Translator class or implements the Illuminate\Contracts\Translation\Translator interface.

  2. Create a custom service provider that will bind your custom Translator class to the service container.

  3. Register your custom service provider in the bootstrap/app.php file.

Here's an example of how you can achieve this:

  1. Create your custom Translator class:
namespace App\Translation;

use Illuminate\Translation\Translator as BaseTranslator;

class Translator extends BaseTranslator
{
    // Your custom implementation
}
  1. Create a custom service provider:
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Translation\Translator;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;

class TranslationServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('translator', function ($app) {
            $loader = $app['translation.loader'];

            // Create your custom translator instance
            $translator = new Translator($loader, $app['config']['app.locale']);

            $translator->setFallback($app['config']['app.fallback_locale']);

            return $translator;
        });

        // Bind the custom translator as the implementation for the TranslatorContract
        $this->app->bind(TranslatorContract::class, function ($app) {
            return $app['translator'];
        });
    }
}
  1. Register your custom service provider in bootstrap/app.php:
$app->register(App\Providers\TranslationServiceProvider::class);

By registering your custom service provider, you're telling Laravel to use your custom Translator class instead of the default one. Make sure that your custom Translator class and service provider are correctly implemented and that they fulfill the contract required by Laravel's translation system.

Remember to clear the cache after making these changes to ensure that Laravel picks up the new configuration:

php artisan cache:clear

This should allow you to override the default Translator class in Laravel 11.

lewis4u's avatar

The bootstrap/app.php is not the same anymore and that code is not applicable for Laravel 11 dear Lary "Quickdraw" A.I.

1 like
puklipo's avatar
puklipo
Best Answer
Level 9

'providers' has already been changed in Laravel10. https://github.com/laravel/laravel/blob/10.x/config/app.php#L158

In Laravel 11, it was moved within the framework, but it will be merged, so you can write it in config/app.php.

// config/app.php

use Illuminate\Support\ServiceProvider;

    'providers' => ServiceProvider::defaultProviders()->replace([
        Illuminate\Foo\FooServiceProvider::class => Bar\BarServiceProvider::class,
    ])->toArray(),

Warning to others who see this later

It is better to avoid using custom Translator. Replacing the default ServiceProvider is bad practice.

lewis4u's avatar

I see, @puklipo can you suggest what would be the better way if someone just needs to make a small change, like in my place, so that before returning the translation from file it first checks if there is a cached translation (pulled from database table on user log in)

I normally do this inside CustomTranslator get() method by adding a bit of code which gets executed before default Translator procedure.

Please or to participate in this conversation.