We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use multi Language on volt template?

i have in my index Action:

 public function indexAction() {
         $this->view->setVar("t", $this->_getTranslation());
    }

but i dont know how to use on my index.volt view ...

on php code is: <?php echo $t->_("hi") ?> ut in volt code?

one more question, for Subjects of performance for load and compile, what recommend to use?, volt template or phtml template?



7.1k
Accepted
answer

I suppose your are using Phalcon documentation : https://docs.phalcon.io/en/latest/reference/translate.html

By this method you can use in volt like this :

{{ t._('hi') }}

But I suggest you to use a Volt filter :

For Example, create a Tool class with a static method ! getTranslation()

class Tool extends \Phalcon\Mvc\User\Component {

      public static function getTranslation()
    {   
        $language = $this->dispatcher->getParam('language');
        $config = \Phalcon\DI\FactoryDefault::getDefault()->getShared('config');

        if (file_exists($config->application->langDir . $language . ".php")) {
            require $config->application->langDir . $language . ".php";            
        } else {
            require $config->application->langDir . $config->application->DefaultLang . ".php";
        }

        return new \Phalcon\Translate\Adapter\NativeArray(array( "content" => $messages ));        
    }
}

In your View Service :

    $this->di->set('voltService', function ($view, $di) use ($config)
    {
          $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
          $voltOptions = array();
          $voltOptions = array(
          'compiledPath' => $config->application->voltDir,
          'compiledExtension' => '.volt-compiled'
          );

          if ($config->application->debug) {
          $voltOptions['compileAlways'] = true;
          }

          $volt->setOptions($voltOptions);

          $compiler = $volt->getCompiler();

          // Functions
          $compiler->addFunction('_', function($resolvedArgs) { return '\Tools::getTranslation()->_(' . $resolvedArgs . ')';});
          $compiler->addFilter('ucf', 'ucfirst');
          $compiler->addFilter('flv', 'floatval');

          return $volt;
    }    
    );

        $this->di->set('view', function () use($config)
        {
              $view = new \Phalcon\Mvc\View();        
              $view->registerEngines(array('.volt' => 'voltService'));        
              return $view;
        });

And then you can use translation in Volt like this :

{{ _('hi') }}

I recommend you to use Volt,

thanks a lot!, this works perfect!, ok! i'll use volt template!.

one more question, which is more fast engine?, this class Tool and Volt filter , or use Gettext ?

hi Again, i try to use the same function on controller, but nothing happen, how can i use in controller:

        public function indexAction() {
        echo _('Inicio de sesion');
        die();
    }


7.1k
edited Apr '15

Gettext is good too and better for plurial :) If your are looking performance, you can choose one of them.

And for your last question with Volt filter solution (PHP Array) you have to use underscore in your key.

public function indexAction() {
        echo _('Inicio_de_sesion');
        die();
}

i use now, gettext, so for volt template i change the volt service:

     // Functions
$compiler->addFunction('_', function($resolvedArgs) {
    return 'gettext(' . $resolvedArgs . ')';
});

gettext

{{ _('Olvidaste tu password?') }}

i use now, gettext, so for volt template i change the volt service:

     // Functions
$compiler->addFunction('_', function($resolvedArgs) {
    return 'gettext(' . $resolvedArgs . ')';
});

gettext

{{ _('Olvidaste tu password?') }}

Is good practice translate string in the view?.

call _t('string') every time, consume lot cpu resources.