Laravel Extend Vendor Class

Viewed 5016

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].'

4 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; 
    })

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

Try changing it to this:

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

That should then change the instance of the interface.

UPDATE

if you are just trying to add a new method, the Translator class is Macroable. So you can do the following

Translator::macro('parseKey', function ($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;
});

You would then be able to call your method as you normally would. For example:

app(Translator::class)->parseKey($key);

Check below example

namespace App\Repositories\Translation;

use Illuminate\Translation\Translator;
class TranslationTranslator extends Translator
{

    public function get()
    {
        ....
    }
}

No need to anything. you only need to add new functions or override base class functions. Then you can use this class as simple other classes.

Related