While creating a PrestaShop module, there are some cases when you need to write a separate php script, a new module tab, class, etc. Customers, who have purchased the module, very often complain that translations are not working in these files.
How to organize label translations properly over there? How to make these translations appear in the same group with translations of the module, created in Tools -> Translations?
In such cases it would be preferable to apply composition.
If a module has its own tab in the back-office or custom class, we should override l method of this class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php'); require_once _PS_MODULE_DIR_ . 'MyModule/MyModule.php'; class MyModuleTab extends AdminTab { public function __construct() { /*……*/ } public function l($string) { //create module class copy: $myModule = new MyModule; //return translated string, using module class method and indicating tab name: return $module->l($string, ' MyModuleTab'); } public function displayForm($isMainTab = true) { global $cookie; global $currentIndex; parent::displayForm(); if (!($obj = $this->loadObject(true))) return; } //example of using translation method echo $this->l(‘Hello World!!!’); } } |
If we create a script-controller (for example, http://my-store.com/modules/my_module/controller.php), don’t forget to add l function in there:
1 2 3 4 5 6 7 8 9 10 11 |
<?php require_once(dirname(__FILE__).'/../../../config/config.inc.php'); //enable store configs function l($string) { // create module class copy: $myModule = new MyModule; // return translated string, using module class method and indicating custom file name: return $myModule->l($string, basename(__FILE__, ‘.php’)); } // example of using translation function: echo l(‘Hello World!!!’); ?> |
Important note: If you omit the second parameter in the $ myModule-> l ($ string, ‘filename’) method, translations will not work correctly. It happens because translated strings will be saved only for the main script of ‘MyModule.php’ module. The second parameter specifies for which file translation is created.
PrestaShop Templates
Take your online store to the next level with BelVG PrestaShop Templates
Visit the store
Hi,
As we now have newer PS version… Could you be so kind and add update ?
I have described problem at main forum:
https://www.prestashop.com/forums/topic/468941-override-admin-class-by-module-how-to-add-translated-strings/
Thanks in advance ! :)