Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Extend Vendor Class

I tried extending an Illuminate Class Translator

I created a class and extended to translator then I added this line to my RepositoryServiceProvider

$this->app->bind(\Illuminate\Translation\Translator::class, \App\Repositories\Translation\TranslationTranslator::class);

But its not working

what am I doing wrong?

the class as follows

<?php

namespace App\Repositories\Translation;

use Countable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\NamespacedItemResolver;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\TranslatorInterface;


class TranslationTranslator extends \Illuminate\Translation\Translator
{
    /**
     * Parse a key into namespace, group, and item.
     *
     * @param  string  $key
     * @return array
     */
    public function parseKey($key)
    {
        \Log::info('got in');
        die;
        $segments = parent::parseKey($key);

        if (is_null($segments[0])) {
            $segments[0] = @explode('.', $segments[2])[0];
        }
        if ($segments[1] == 'translatable') {
            $segments[1] = @explode('.', $segments[2])[0] . '_' . @explode('.', $segments[2])[1];
        }
        return $segments;
    }
}

UPDATE

Apparently the Translator class has a constructor

 public function __construct(LoaderInterface $loader, $locale)
    {
        $this->loader = $loader;
        $this->locale = $locale;
    }

so my binding has to pass by the interface.. which cannot be instantiated

 public function boot()
{
    $app = $this->app;
    $this->app->bind(\Illuminate\Translation\Translator::class, function() use ($app){
        return $app->make(\App\Repositories\Translation\TranslationTranslator::class);
    });        
}

and getting this error

Illuminate\Contracts\Container\BindingResolutionException with message 'Target [Illuminate\Translation\LoaderInterface] is not instantiable while building [App\Repositories\Translation\TranslationTranslator].'

like image 602
Garine Pondikian Avatar asked Sep 13 '18 11:09

Garine Pondikian


2 Answers

You can use closure to resolve the classes

$this->app->bind(\Illuminate\Translation\Translator::class, function(){

    return new \App\Repositories\Translation\TranslationTranslator;
});

Secondly translator is binded with laravel in using translator alias.

You can also override it.

$this->app->bind('translator', function(){
        return new \App\Repositories\Translation\TranslationTranslator; 
    })
like image 75
Romantic Dev Avatar answered Sep 19 '22 14:09

Romantic Dev


This worked for me

$app = $this->app;
$loader = $app['translation.loader'];
$locale = $app['config']['app.locale'];

$this->app->bind('translator', function() use ($loader, $locale){
    return new \App\Repositories\Translation\TranslationTranslator($loader, $locale);
});

I hope this help you

like image 24
Daniel Velázquez Avatar answered Sep 17 '22 14:09

Daniel Velázquez