我尝试扩展一个Illuminate Class Translator
我创建了一个类并扩展到翻译器 然后我在我的RepositoryServiceProvider中添加了这行代码
$this->app->bind(\Illuminate\Translation\Translator::class, \App\Repositories\Translation\TranslationTranslator::class);
但它没有起作用
我做错了什么?
类如下
<?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;
}
}
更新
显然,翻译器类有一个构造函数。
public function __construct(LoaderInterface $loader, $locale)
{
$this->loader = $loader;
$this->locale = $locale;
}
所以我的绑定必须通过接口传递..而接口是无法实例化的。
public function boot()
{
$app = $this->app;
$this->app->bind(\Illuminate\Translation\Translator::class, function() use ($app){
return $app->make(\App\Repositories\Translation\TranslationTranslator::class);
});
}
并且出现了以下错误
Illuminate\Contracts\Container\BindingResolutionException 错误信息为 '在构建 [App\Repositories\Translation\TranslationTranslator] 时,目标 [Illuminate\Translation\LoaderInterface] 无法实例化。'