Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use html tags in prestashop's smarty translations?

I can't find a solution. What I've got to do is translate this:

se stai inserendo un indirizzo per consegna all'interno dell'area <span class=orange>EXPO</span>

into this:

If your delivery address is in the <span class=orange>EXPO</span> area

If I use tags, like this:

{l s="se stai inserendo un indirizzo per consegna all'interno dell'area <span class=orange>EXPO</span>"}

They won't be seen. So what?

like image 519
Luca Reghellin Avatar asked May 19 '15 07:05

Luca Reghellin


1 Answers

Prestashop provides an undocumented solution for this:

You can add a tags parameter inside the {l} function call. The value of this parameter is an array of string. To add a tag from this array in the string you need to use [i]x[/i] (where i is the tag index in the array starting from 1 and x is the text you want to see surrounded by the tag)

For example if I want to render this string in a single translation line:

<strong>Welcome</strong> <i class="name_class">Florian Lemaitre</i>!

I can use this code:

{l s='[1]Welcome[/1] [2]%s[/2]!' sprintf=[$name] tags=['<strong>', '<i class="name_class">']}

In your case you can use:

{l s="se stai inserendo un indirizzo per consegna all'interno dell'area [1]EXPO[/1]" tags=['<span class=orange>']}

You can find the related code in file classes/Translate.php :

/**
* Perform operations on translations after everything is escaped and before displaying it
*/
public static function postProcessTranslation($string, $params)
{
    // If tags were explicitely provided, we want to use them *after* the translation string is escaped.
    if (!empty($params['tags'])) {
        foreach ($params['tags'] as $index => $tag) {
            // Make positions start at 1 so that it behaves similar to the %1$d etc. sprintf positional params
            $position = $index + 1;
            // extract tag name
            $match = array();
            if (preg_match('/^\s*<\s*(\w+)/', $tag, $match)) {
                $opener = $tag;
                $closer = '</'.$match[1].'>';

                $string = str_replace('['.$position.']', $opener, $string);
                $string = str_replace('[/'.$position.']', $closer, $string);
                $string = str_replace('['.$position.'/]', $opener.$closer, $string);
            }
        }
    }

    return $string;
}
like image 132
Florian Lemaitre Avatar answered Nov 22 '22 05:11

Florian Lemaitre