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?
|
Aug '16 |
3 |
1509 |
0 |
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,