Advanced Features of Smarty Gettext

While the previous example was perfect for a simple web page, things start getting more complicated when we need to pass information from PHP to Smarty. This is what happens if we need to translate the following expression: There are X customers waiting for Y orders, where X and Y are both variable values.

For the expression above, we could just translate that expression by separately translating the following three messages:

  • There are
  • customers waiting for
  • orders

We would then have to join them together afterwards. That’s a valid way of doing this, but it’s much more complex than having to translate just one phrase. It’s also a potential translation problem as translators might not know what the context is for each of these expressions and even more because the required order of these expressions or the positions for the numbers might differ in different languages.

In any case, Smarty Gettext provides a nice and easy-to-use feature for these issues. You may pass parameters to a translated string, and the plug-in will take care of substituting the parameters in the resulting translated message. Let’s go for a simple example of this feature.

Here’s the source code for parameters.php:

<?php
include_once(‘libs/Smarty.class.php’);
$smarty = new Smarty;

$language_code = ‘pt_BR’;
putenv(“LANG=$language_code”);
setlocale(LC_ALL, $language_code);

$domain = ‘smartybook’;
bindtextdomain($domain, ‘./locale’);
textdomain($domain);

$smarty->assign(‘customers’, 6);
$smarty->assign(‘orders’, 9);

$smarty->display(‘parameters.tpl’);
?>

The following are the contents of the associated template file, called parameters.tpl:

<html>
<head>
<title>{t}Passing Parameters to Translations{/t}</title>
</head>

<body>

<h1>{t}Passing Parameters to Translations{/t}</h1>

<p>
{t 1=$customers 2=$orders}There are %1 customers waiting for %2 orders.{/t}
</p>
 
</body>
</html>

As you can see, we are passing two parameters to the text message. They are numeric arguments to make it easy for the actual translators. The plug-in feature will substitute the string %1 with the value of the template variable $customers, and it will replace %2 with the value of $orders.

By running the bundled tsmarty2c.php script against the parameters.tpl file, we will get the following source:

/* templates/parameters.tpl */
gettext(“Passing Parameters to Translations”);

/* templates/parameters.tpl */
gettext(“Passing Parameters to Translations”);

/* templates/parameters.tpl */
gettext(“There are %1 customers waiting for %2 orders.”);

Now we just need to go through the usual process of generating the PO file from the C file, and then converting into the binary catalog file and moving it to the correct place. Here’s the output when everything is done:

Advanced Features of Smarty Gettext

The Smarty Gettext plug-in is translating the message beforehand, and then replacing the parameters with the given template variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241