问题
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