Luca Reghellin
Luca Reghellin

Reputation: 8113

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?

Upvotes: 6

Views: 7742

Answers (4)

user2975076
user2975076

Reputation: 9

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

give us

[1]Welcome[/1] [2] Bob [/2]!

Upvotes: 0

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

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

Upvotes: 16

unloco
unloco

Reputation: 7320

The translation function will remove all html tags so you will have to use an alternative like so

{capture "string"}
  {l s="se stai inserendo un indirizzo per consegna all'interno dell'area _h1_EXPO_h2_"}
{/capture}
{$smarty.capture.string|replace:'_h1_':'<span class="orange">'|replace:'_h2_':'</span>'}

_h1_ and _h2_ are in the translation and they are replaced by <span class="orange"> and </span> respectively

The smarty capture function is used to get the translation into the variable string instead of dispaying it

So your English translation would be something like this
If your delivery address is in the _h1_EXPO_h2_ area

Upvotes: 2

crlsn
crlsn

Reputation: 42

Try to keep HTML out of your translation strings. You can do that by making two strings:

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

Upvotes: 0

Related Questions