1

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?

1 Answer 1

1

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

2
  • Thanks! Just one more question...What's the difference between ->setShared and ->set ?
    – Duveral
    Commented Apr 26, 2016 at 10:17
  • 1
    With ->setShared the same instance of the service is always returned, while ->set will return a new instance every time it is called. docs.phalconphp.com/en/latest/reference/di.html
    – PWD
    Commented Apr 26, 2016 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.