Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can extend laravel translator (any other component)?

Tags:

laravel

I want to implement some extra features to Illuminate\Translate\Translator.

So, i create my folder in ~/vendor directory, place there My/Traslator class, that will implement Symfony\Component\Translation\TranslatorInterface. Right?

Is it OK to extend laravel translator class (a lot of functionality will be duplicated otherwise) in my package?

If it is ok - it will be necessary to tie to current laravel version to keep code stable. But what will happen in case enduser laravel version will differ from one required in my package?

What should i do then to make laravel use my translator class in application (facades,etc)?

like image 878
avasin Avatar asked Jun 03 '15 09:06

avasin


2 Answers

This page has more information: http://laravel.com/docs/5.0/extending#container-based-extension

So what you need to do is:

  1. Extend the built-in class from the vendor directory
  2. Create a new service provider that add your translation class to the service container
  3. Replace Laravel’s translation service provider in your config/app.php file with the namespace of your translation service provider

Now when you ask for the translation service provider out of the service container—either directly (app('translator')) or with the Lang façade, it will return your translation class rather than Laravel’s.

like image 108
Martin Bean Avatar answered Sep 28 '22 09:09

Martin Bean


  1. Make a Translator class and make it extend Illuminate\Translation\Translator

    <?php
    
    namespace App\Helpers;
    
    use Illuminate\Translation\Translator as LaravelTranslator;
    
    
    class Translator extends LaravelTranslator
    {
    
    // here you can overwrite any functions you want/need
    
    }
    
  2. Create your own TranslationServiceProvider inside app/providers (just copy the laravel translation service provider and change the line where it uses Translator with your own Translator class where you have overwritten what you needed)

    <?php
    
    namespace App\Providers;
    
    use App\Helpers\Translator;             // <= Your own class
    use Illuminate\Translation\FileLoader;
    use Illuminate\Support\ServiceProvider;
    
    class TranslationServiceProvider extends ServiceProvider
    {
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerLoader();
    
        $this->app->singleton('translator', function ($app) {
            $loader = $app['translation.loader'];
    
            // When registering the translator component, we'll need to set the default
            // locale as well as the fallback locale. So, we'll grab the application
            // configuration so we can easily get both of these values from there.
            $locale = $app['config']['app.locale'];
    
            $trans = new Translator($loader, $locale);
    
            $trans->setFallback($app['config']['app.fallback_locale']);
    
            return $trans;
        });
    }
    
    /**
     * Register the translation line loader.
     *
     * @return void
     */
     protected function registerLoader()
     {
          $this->app->singleton('translation.loader', function ($app) {
              return new FileLoader($app['files'], $app['path.lang']);
          });
     }
    
     /**
      * Get the services provided by the provider.
      *
      * @return array
      */
     public function provides()
     {
         return ['translator', 'translation.loader'];
     }
    }
    
    1. Comment out or delete the Laravels translator service line inside config/app.php:

      //Illuminate\Translation\TranslationServiceProvider::class,

    2. Add your own Provider in that same array

      App\Providers\TranslationServiceProvider::class,

like image 22
lewis4u Avatar answered Sep 28 '22 09:09

lewis4u