Prestashop 1.7 $this->translator->trans in customerformatter override
Prestashop 1.7 $this->translator->trans in customerformatter override
我正在覆盖 customerformatter class 中的 getFormat () 函数。
这里 $this->translator->trans 不起作用。
那么回忆类型字段翻译的最佳方式是什么
$format['company'] = (new FormField)
->setName('company')
->setType('text')
->setLabel($this->translator->trans(
'Company', [], 'Shop.Forms.Labels'
));
如果我覆盖整个 customerformatter class,显然一切正常。
谢谢
translator
和 language
出现错误,因为在主核心 class CustomerFormatterCore
中; translator
和 language
等属性具有 private
可见性,因此无法在子 class 中访问它,这在我们的 class CustomerFormatter
中被覆盖案件。
我们需要再次将这些属性声明为private
,并且需要将其注入到__construct()
方法中。
按照以下步骤实现您想要的效果。
1) 在 override\classes\form
文件夹中创建文件 CustomerFormatter.php
并在其中添加以下代码。
<?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) 从 var\cache\prod
和 var\cache\dev
文件夹中删除 class_index.php
文件。
3) 检查您的商店。
希望对您有所帮助!
我正在覆盖 customerformatter class 中的 getFormat () 函数。 这里 $this->translator->trans 不起作用。
那么回忆类型字段翻译的最佳方式是什么
$format['company'] = (new FormField)
->setName('company')
->setType('text')
->setLabel($this->translator->trans(
'Company', [], 'Shop.Forms.Labels'
));
如果我覆盖整个 customerformatter class,显然一切正常。 谢谢
translator
和 language
出现错误,因为在主核心 class CustomerFormatterCore
中; translator
和 language
等属性具有 private
可见性,因此无法在子 class 中访问它,这在我们的 class CustomerFormatter
中被覆盖案件。
我们需要再次将这些属性声明为private
,并且需要将其注入到__construct()
方法中。
按照以下步骤实现您想要的效果。
1) 在 override\classes\form
文件夹中创建文件 CustomerFormatter.php
并在其中添加以下代码。
<?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) 从 var\cache\prod
和 var\cache\dev
文件夹中删除 class_index.php
文件。
3) 检查您的商店。
希望对您有所帮助!