0
votes

I successefully added multilanguage abilities to WEB in Phalcon using the volt template.

But how can I use that translation within controller so I can translate flash messages.

My Base Controller:

protected function _t() {
    // Get language code
    if($this->session->has("lg")) {
        $language = $this->session->get("lg");
    } else {
        // Ask browser what is the best language
        $language = $this->request->getBestLanguage();
    }
    // Check if we have a translation file for that language
    if(file_exists(APP_PATH . "app/languages/".$language.".php")) {
        require APP_PATH . "app/languages/".$language.".php";
    } else {
        // Fallback to default language
        require APP_PATH . "app/languages/en.php";
    }
    // Return a translation object
    return new \Phalcon\Translate\Adapter\NativeArray(array(
        "content" => $t
    ));
}

Then I put that inside view like this:

$this->view->setVar("t", $this->_t());

Inside view I can use translation like:

<?php echo $t->_t('hello') ?>

But how can I use that inside other controllers that extends BaseController

class IndexController extends BaseController {
   ...
   $this->flashSession->error('HOW TO ADD TRANSLATION HERE');
   ...
}
2

2 Answers

4
votes

Here's an more appropriate approach if you're interested;

First register your translator as a service in your DI:

// $di initialization

// ...

$di->setShared('trans', function() use($di) { 
    $session = $di->getShared('session');
    $request = $di->getShared('request');

    // Get language code
    if($session->has("lg")) {
        $language = $session->get("lg");
    } else {
        // Ask browser what is the best language
        $language = $request->getBestLanguage();
    }

    // Check if we have a translation file for that language
    if(file_exists(APP_PATH . "app/languages/".$language.".php")) {
        require APP_PATH . "app/languages/".$language.".php";
    } else {
        // Fallback to default language
        require APP_PATH . "app/languages/en.php";
    }

    // Return a translation object
    return new \Phalcon\Translate\Adapter\NativeArray(array(
        "content" => $t
    ));
});

// ...

Now you do not need anymore to call this line everytime:

$this->view->setVar("t", $this->_t());

And inside a view you can use translation like:

<?= $trans->_('hello') ?>

And finally, from any controller (from the controller base or not) you can use:

class IndexController extends ControllerBase {
   $this->flashSession->error($this->trans->_('MY_ERROR_NAME'));
}

Also, I'd recommend to name your "base controller" as ControllerBase, not only because Phalcon conventions but also one may access your "/base" path.

0
votes

You can try to access parent function like this

$this->flashSession->error($this->_t()->_('whatever'));

or if you want to access it like in template you can add new variable inside _construct method of your child class

public function _construct() {
    $this->t = $this->_t();
}

then you can do it like in template

$this->flashSession->error($this->t->_('whatever'));