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 implement multilanguage on the Volt template, the controller and forms?

Following the documentation I could implement multilanguage on the Volt template through the view. But this way you must declare the method in all controllers individually. And how this is documented not translate flash messages on the controller itself, or translate messages or values that are in the forms.

I tried to follow this tip, but apache displays messages and displays a blank page:

PHP Notice: Undefined variable: in trans ...

PHP Fatal error: Call to a member function _ () ...

Thank you.

It is often recommended to have each of your controllers not extend \Phalcon\Mvc\Controller directly, but rather extend a base Controller class. That base controller class could then have the method described there in the documentation - saving you duplication.

edited Nov '14

I agree with quasipickle. I'm making a site that will be available in many different langs, so I've got one controller that looks like this, and all the others extend it

abstract class Controller extends \Phalcon\Mvc\Controller {
    public function initialize() {
        ....
        $this->setLocale()
        ....
    }
    public function setLocale() {
        $lang = $this->dispatcher->getParam('lang'); //pull lang from url
        $translator = new \Phalcon\Translate\Adapter\NativeArray( require "some/file/".$lang.".php" );
        $this->view->setVar('translate', $this->translator);
        $this->di['translator'] = $translator;
    }
}

I agree with quasipickle. I'm making a site that will be available in many different langs, so I've got one controller that looks like this, and all the others extend it

abstract class Controller extends \Phalcon\Mvc\Controller { public function initialize() { .... $this->setLocale() .... } public function setLocale() { $lang = $this->dispatcher->getParam('lang'); //pull lang from url $translator = new \Phalcon\Translate\Adapter\NativeArray( require "some/file/".$lang.".php" ); $this->view->setVar('translate', $this->translator); $this->di['translator'] = $translator; } }

That's cool. Can you share the complete example code?

What I posted above is really all there is to it. Just replace the 'require "some/file" part with the path to a php file with this in it

return array(
    'some_key1' => 'Your translated text message',
    'some_key2' => 'Another translated text message',
    ...
);

Then any time in your view, use 'echo $translator->_('some_key1');' to print out your message. If you need it in your controller, use '$txt = $this->translator->_('some_key');'

I agree with quasipickle. I'm making a site that will be available in many different langs, so I've got one controller that looks like this, and all the others extend it

abstract class Controller extends \Phalcon\Mvc\Controller { public function initialize() { .... $this->setLocale() .... } public function setLocale() { $lang = $this->dispatcher->getParam('lang'); //pull lang from url $translator = new \Phalcon\Translate\Adapter\NativeArray( require "some/file/".$lang.".php" ); $this->view->setVar('translate', $this->translator); $this->di['translator'] = $translator; } }

That's cool. Can you share the complete example code?