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].'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With