2

I am using ZendTranslate(csv adapter) with Smarty in my project. I use ZendTranslate as standalone not all Zend because it seems very nice and easy to use. But I have a problem. I cant find out best way to grab all content and send to Smarty. My simple code:

require_once 'Smarty/libs/Smarty.class.php';
require_once 'Zend/Translate.php';
$lang = $_GET["lang"];
$translate = new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => 'csv/lang_eng.csv',
        'locale'  => 'en'
    )
);
$translate->addTranslation(
    array(
        'content' => 'csv/lang_' . $lang . '.csv',
        'locale' => $lang
    )
);
$smarty->assign("m2", "" . $translate->translate('m2', $lang));
$smarty->display("views/page.tpl");

and this is ok for one translation but I have more then 30 on one page. How to grab all of that on best way and send to Smarty?

consigliere
  • 73
  • 3
  • 11

2 Answers2

1

It's better to register smarty modification. Add this to place, when you initialize Smarty:

require_once 'Smarty/Smarty.class.php';
// ...
$this->Smarty();
// ...
$obj = new TemplateTranslate();
$this->register_modifier('translate', array($obj, 'translate' ));

Main translate class:

class TemplateTranslate
{
    public static $zt;

    public function __construct()
    {
        self::$zt = new Zend_Translate(array(
            'adapter' => 'csv',
            'content' => 'csv/lang_eng.csv',
            'locale' => 'en'
        ));
    }

    public static function translate($string)
    {
        return self::$zt->_($string);
    }
}

lang_eng.csv:

"Text on another language";"English text"

Then you can use like this:

<p>{'Text on another language'|translate}</p> 
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • Hey this is good, really thanks! But I got a blank page when use: $smarty->register_modifier('translate', array($obj, 'translate' )); In your example u wrote $this->register_modifier('translate', array($obj, 'translate' )); $this should be a instance of smarty? – consigliere Sep 14 '12 at 15:32
  • Yes. Blank page? Are errors reporting is enabled? Give me your error message. – Alex Pliutau Sep 14 '12 at 17:46
  • Here is my error: Fatal error: Uncaught exception 'SmartyException' with message 'Call of unknown method 'register_modifier'.' in C:\wamp\www\test\Smarty\libs\sysplugins\smarty_internal_templatebase.php on line 806 Class TemplateTranslate works perfect without Smarty end I use the latest Smarty version. – consigliere Sep 14 '12 at 18:22
  • Can anyone tell me how to solve this problem?

    {'Text on another language'|translate}

    in every other call for a func translate will be every time instanced a class TemplateTranslate or just one time?
    – consigliere Sep 14 '12 at 22:10
  • @consigliere, as you can see from the script, the class itself is instantiated once before registered in Smarty. Also you can try **put modifier in plugins directory**. This approach is described [here](http://www.denisvlasov.net/155/adding-modifier-in-smarty-template-engine/). – Paul T. Rawkeen Apr 04 '13 at 14:58
1

This is how u have to register modifier in latest version of Smarty:

$smarty ->registerPlugin('modifier','translate', array($obj, 'translate' ));

May someone find this as helpful!

consigliere
  • 73
  • 3
  • 11