mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.32k stars 507 forks source link

Support contextual binding #879

Closed jordyvanderhaegen closed 3 months ago

jordyvanderhaegen commented 11 months ago

Hi, this PR adds support for contextual binding in Laravel.

This comes in handy when you want to inject a different implementation into the LaravelLocalization class.

A quick example: We have extended Laravel's translations loader to support loading translations from the database. We have done so by overwriting the app('translator') in the container. However, to improve performance we would like to only load translations from the filesystem when using this package. Before this PR, there was no way to tell Laravel that the LaravelLocalization class can only use the file loader. After this PR, we can now tell Laravel to inject the file loader instance when it is requested by the LaravelLocalization class:

        $this
            ->app
            ->when(LaravelLocalization::class)
            ->needs(Translator::class)
            ->give(function (Application $app) {
                $loader = new FileLoader($app['files'], [__DIR__.'/lang', $app['path.lang']]);

                return new Translator($loader, $app->getLocale());
            });

What has been changed in this PR?

What tests have been changed in this PR?

mcamara commented 11 months ago

Can you add this changes to the changelog and how to fix any breaking changes? I'll create a version 2.0.0 of the package with your changes.

jordyvanderhaegen commented 11 months ago

Done, included the upgrade process in a new UPGRADING.md file

mcamara commented 3 months ago

I'll merge this now and create version 2.0 of the package alongside laravel 11 update