Gettext block.t.php issues and Smarty 3.1.7

285 views
Skip to first unread message

Fran Dieguez

unread,
Jan 23, 2012, 8:45:50 AM1/23/12
to smarty-d...@googlegroups.com
Hi all,

I'm trying to port one of my applications to Smarty 3.1.7 (currently
using 3.0.8. All seems to work properly but if I try to use gettext
block.t.php plugin, Smarty outputs the translation string appended to
the po file header:

Project-Id-Version: applicationname 0.8
Report-Msgid-Bugs-To:
POT-Creation-Date: 2012-01-14 04:33+0100
PO-Revision-Date: 2012-01-03 13:33+0100
Last-Translator: Translator name <translatormail###>
Language-Team: Group name <groupmail###>
Language: gl_ES
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Plural-Forms: nplurals=2; plural=(n!=1);
THE TRANSLATED STRING GOES HERE

All the translations managed directly by php (using _()), without smarty
code involved, are showed perfectly but those using {t}Original
text{/t} have this error.

All the translations and code was working properly with Smarty 3.0.8 and
I have cleared all the APC caches, smarty compile and cache files.

Below is the smarty block.t.php plugin code without comments (this code
is the standard smarty block.t.php plugin).

<?php
function smarty_gettext_strarg($str)
{
$tr = array();
$p = 0;

for ($i=1; $i < func_num_args(); $i++) {
$arg = func_get_arg($i);

if (is_array($arg)) {
foreach ($arg as $aarg) {
$tr['%'.++$p] = $aarg;
}
} else {
$tr['%'.++$p] = $arg;
}
}

return strtr($str, $tr);
}

function smarty_block_t($params, $text, &$smarty)
{
$text = stripslashes($text);

// set escape mode
if (isset($params['escape'])) {
$escape = $params['escape'];
unset($params['escape']);
}

// set plural version
if (isset($params['plural'])) {
$plural = $params['plural'];
unset($params['plural']);

// set count
if (isset($params['count'])) {
$count = $params['count'];
unset($params['count']);
}
}

// use plural if required parameters are set
if (isset($count) && isset($plural)) {
$text = ngettext($text, $plural, $count);
} else { // use normal
$text = gettext($text);
}

// run strarg if there are parameters
if (count($params)) {
$text = smarty_gettext_strarg($text, $params);
}

if (!isset($escape) || $escape == 'html') { // html escape, default
$text = nl2br(htmlspecialchars($text));
} elseif (isset($escape)) {
switch ($escape) {
case 'javascript':
case 'js':
// javascript escape
$text = str_replace('\'', '\\\'', stripslashes($text));
break;
case 'url':
// url escape
$text = urlencode($text);
break;
}
}

return $text;
}


Cheers
--
Fran Di�guez
http://www.mabishu.com
GPG: 43DD 1B00 035F A764 4986 E695 98BB 6626 A2A4 F9B8

uwe.tews

unread,
Jan 23, 2012, 5:38:26 PM1/23/12
to Smarty Developers
Smarty block functions get called twice, once at the opening tag (in
this case the $text parameter has a value = null) and second at the
closing tag.

See http://www.smarty.net/docs/en/plugins.block.functions.tpl

In your case the code should be execute only if $text !== null.

The change between Smarty 3.0 and 3.1 is that the returned value on
the opening gets also displayed and is no longer ignored as in 3.0.
This does enable output also at the opening tag.

It was bad coping of the plugin already at 3.0 that it did execute the
code for $text !== null.

Regards Uwe
> Fran Di guezhttp://www.mabishu.com

Fran Dieguez

unread,
Jan 23, 2012, 5:49:34 PM1/23/12
to smarty-d...@googlegroups.com, uwe.tews
Great is working like a charm.

Do you think that is a good idea to use this plugin with Smarty 3.X?

Really thanks.

Fran Diéguez


http://www.mabishu.com
GPG: 43DD 1B00 035F A764 4986 E695 98BB 6626 A2A4 F9B8

Reply all
Reply to author
Forward
0 new messages