PrestaShop: Translating overrided controller

梦想的初衷 提交于 2019-12-25 18:02:25

问题


I've created a module that overrides an AdminProductController.php and make a new 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',
        );
    }
}

Now I need to translate the action text and distribute that translation with module. The problem is that I don't see the original text inside modules translation instead it is visible in back-office translations.

So, is there any way to add this string to module translations not to back-office translations?


回答1:


You can do it by creating an instance of a module you want the translation to be in.

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',
        );
    }
}



回答2:


The main problem description I've found here: How to get translation from other module in PrestaShop?

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');
    }
}

Than in controller we can do what was suggested by @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',
        );
    }
}



回答3:


Try using the following code in place of $this->l('Set a price for selected')

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



来源:https://stackoverflow.com/questions/40616964/prestashop-translating-overrided-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!