我正在重写customerformatter类中的getFormat()函数。 在这里$ this-> translator-> trans不起作用。
那么调用类型字段翻译的最佳方法是什么
$format['company'] = (new FormField)
->setName('company')
->setType('text')
->setLabel($this->translator->trans(
'Company', [], 'Shop.Forms.Labels'
));
如果我覆盖整个customerformatter类,显然一切正常。 谢谢
答案 0 :(得分:0)
translator
和language
出现错误,因为在主核心类CustomerFormatterCore
中; translator
和language
之类的属性具有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.php
和var\cache\prod
文件夹中删除var\cache\dev
文件。
3)检查您的商店。
希望对您有帮助!