Back to Top

How to make a string translatable in any class of Prestashop

Updated 28 December 2016

Sometimes, we need to create a global function that have some strings and we must translate these strings. Basically for making our strings translatable, we use l() function of Prestashop’s Module class. So suppose if our string is ‘Product name is required.’ then we use –

$this->l('Product name is required.');

But it works only in our Prestashop controllers or module’s main class. So in this blog, we will learn that how to use l() function in classes of our Prestashop module for making our strings translatable.

In Prestashop, l() function is defined in Module class i.e.

/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2016 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

abstract class ModuleCore
{
    public function l($string, $specific = false)
    {
        if (self::$_generate_config_xml_mode) {
            return $string;
        }

        return Translate::getModuleTranslation($this, $string, ($specific) ? $specific : $this->name);
    }
}

Parameters of l() function –

  1. $string – First parameter in l() function will be our string.
  2. $specific – Second parameter is a source name that will be our class name in which we will implement l() function. This parameter is optional that means if you didn’t set this parameter then Prestashop will take by default –
    $this->name

So if we want to use l() function in any class for making our string translatable then we can implement with following way –

Searching for an experienced
Prestashop Company ?
Find out More

Suppose we have a class in our module that is ‘Product’ And here if want to use l() function in this class then we can implement below code in any function of Product class.

$objDemoModule = new DemoModule();
$objDemoModule->l('Product name is required.', 'Product');

Here DemoModule is our main class that extends Prestashop Module class. So we make an object of this class that call l() function with it. First parameter of l() function is our string and second parameter is our class name where we implement this function.

After this, we can see in Prestashop translations, A section of our class name ‘Product’ will display with our string ‘Product name is  required.’ So here we can translate this string.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • vince
    • Neeraj (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home