Duveral
Duveral

Reputation: 335

Use variable outside views phalcon

I'm using Phalcon and his capability to make easier the translation with his Class Translate. So far I'm passing the t variable from the index to all the views, right when I set up volt, like this :

 $view = new View();
 $view->setViewsDir(WEBSITE_PATH.'/views/');
// Return a translation object
      $view->t = new Phalcon\Translate\Adapter\NativeArray([
        "content" => $localization
      ]);

That is working, but I also have pages to translate outside from the folder views, in .php, not .volt. How can I share/set/pass this variable 't' to other places?

Upvotes: 1

Views: 171

Answers (1)

PWD
PWD

Reputation: 45

You can register the translations in your dependency injector like

$di->setShared('translations', function() use($di) {

    // Include or set your translations here, must be an array
    $translations = ['Phalcon' => 'Falcon', 'Word' => 'Translation'];

    return new \Phalcon\Translate\Adapter\NativeArray(array(
        'content' => $translations
    ));

});

Then you can call the translations in any controller like

$this->translations->_('Phalcon')

and in views like

<?=$this->translations->_('Word') ?>

Further reading: https://docs.phalconphp.com/en/latest/reference/translate.html

Upvotes: 1

Related Questions