PrestaShop:翻译重写的控制器

PrestaShop: Translating overrided controller

我创建了一个覆盖 AdminProductController.php 的模块并创建了一个新的 bulk_action。

<?php
class AdminProductsController extends AdminProductsControllerCore
{
    public function __construct()
    {
        parent::__construct();
        $this->bulk_actions['setprice'] = array(
            'text' => $this->l('Set a price for selected'),
            'icon' => 'icon-price',
        );
    }
}

现在我需要翻译操作文本并使用模块分发该翻译。 问题是我在模块翻译中看不到原文,而是在后台翻译中可见。

那么,有什么方法可以将此字符串添加到模块翻译而不是后台翻译中吗?

您可以通过创建一个您希望翻译所在的模块的实例来实现。

class AdminProductsController extends AdminProductsControllerCore
{
    public function __construct()
    {
        parent::__construct();
        $module = Module::getInstanceByName('modulename');
        $this->bulk_actions['setprice'] = array(
            'text' => $module->l('Set a price for selected'),
            'icon' => 'icon-price',
        );
    }
}

我在这里找到的主要问题描述:

This is because translations controller scans for $this->l((.*)) inside module folder using regex and adds the translatable strings to a file So we should in module do something like this:

class MyModule extends Module
{

    public static $l = null;
    public function __construct()
    {
        parent::__construct();
        $this::$l = $this->l('Set a price for selected');
    }
}

在控制器中,我们可以按照@TheDrot 的建议进行操作:

class AdminProductsController extends AdminProductsControllerCore
{
    public function __construct()
    {
        parent::__construct();
        $module = Module::getInstanceByName('modulename');
        $this->bulk_actions['setprice'] = array(
            'text' => $module->l('Set a price for selected'),
            'icon' => 'icon-price',
        );
    }
}

尝试使用以下代码代替 $this->l('Set a price for selected')

翻译::getModuleTranslation(YOUR_MODULE_NAME, 'Set a price for selected', FILE_NAME);