如何在prestashop的智能翻译中使用html标签?

时间:2015-05-19 07:20:29

标签: templates smarty translation prestashop smarty3

我无法找到解决方案。我要做的就是翻译这个:

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

进入这个:

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

如果我使用标签,就像这样:

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

他们不会被人看见。那是什么?

4 个答案:

答案 0 :(得分:8)

Prestashop为此提供了一个未记录的解决方案:

您可以在tags函数调用中添加{l}参数。此参数的值是一个字符串数组。要在字符串中添加此数组中的标记,您需要使用[i]x[/i](其中i是从1开始的数组中的标记索引,x是您要查看的文本被包围通过标签)

例如,如果我想在单个翻译行中呈现此字符串:

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

我可以使用此代码:

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

在您的情况下,您可以使用:

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

您可以在文件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;
}

答案 1 :(得分:2)

翻译功能将删除所有html标签,因此您必须使用类似的替代方法

{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__h2_在翻译中,分别由<span class="orange"></span>替换

smarty capture函数用于将变换转换为变量string而不是发送它

所以你的英文翻译就像这样 If your delivery address is in the _h1_EXPO_h2_ area

答案 2 :(得分:1)

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

给我们

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

答案 3 :(得分:0)

尝试将HTML保留在翻译字符串之外。你可以通过制作两个字符串来做到这一点:

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