客户格式化程序中的Prestashop 1.7 $ this-> translator-> trans覆盖

时间:2018-10-03 11:01:17

标签: override prestashop prestashop-1.7

我正在重写customerformatter类中的getFormat()函数。 在这里$ this-> translator-> trans不起作用。

那么调用类型字段翻译的最佳方法是什么

$format['company'] = (new FormField)
->setName('company')
->setType('text')
->setLabel($this->translator->trans(
'Company', [], 'Shop.Forms.Labels'
));

如果我覆盖整个customerformatter类,显然一切正常。 谢谢

1 个答案:

答案 0 :(得分:0)

translatorlanguage出现错误,因为在主核心类CustomerFormatterCore中; translatorlanguage之类的属性具有private可见性,因此不能访问子类(在本例中为重写类CustomerFormatter)中。

我们需要再次将这些属性声明为private,并需要将其注入__construct()方法中。

按照以下步骤实现您想要的。

1)在CustomerFormatter.php文件夹中创建文件override\classes\form,并在其中添加以下代码。

<?php
/**
 * @Override CustomerFormatter
 */

use Symfony\Component\Translation\TranslatorInterface;

class CustomerFormatter extends CustomerFormatterCore
{
    private $translator;
    private $language;

    public function __construct(
        TranslatorInterface $translator,
        Language $language
    ) {
        parent::__construct($translator, $language);
        $this->translator = $translator;
        $this->language = $language;
    }

    public function getFormat()
    {
        $format = parent::getFormat();

        if (Configuration::get('PS_B2B_ENABLE')) {
            $format['company'] = (new FormField)
                ->setName('company')
                ->setType('text')
                ->setLabel($this->translator->trans(
                    'Company', [], 'Shop.Forms.Labels'
                ));
        }
        // add formatter here as per your need            

        return $format;
    }
}

2)从class_index.phpvar\cache\prod文件夹中删除var\cache\dev文件。

3)检查您的商店。

希望对您有帮助!