We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multilingual not working

Hi again,

i use the getTranslation() function of the documentation, but i get a Notice and an Error:

Notice: Undefined variable: t Fatal error: Call to a member function _() on null in

Here is the code:

class UsersController extends ControllerBase{

protected function getTranslation(){

    // Ask browser what is the best language
    $language = $this->request-> getBestLanguage();

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

    // Return a translation object
    return new NativeArray(
        array(
            "content" => $messages
        )
    );
}
.......

Can anyone help me please :-)

And how can i also do an Route for that?

Rgds

Stefan

In which line ? Are you sure there are request object ? How you are calling getTranslation method ?



59.9k
edited Nov '15

Hi Jurigag,

in line 8 of the template:

{{ content() }}
 <!-- welcome -->
<!-- String: hi => 'Hello' -->

 Line 8:     <p> <?php echo $t -> _('account'), ' ', $name; ?> </p>

Seems like you lack something that hands over t to your views. It could look like this

public function initialize()
{
    $this->view->setVar("t", $this->getTranslation());
}

in your controllerBase



59.9k

Hi Andreas,

thx for your reply :-)

I will try it this evening, have a nice day.

Kind Rgds

Stefan



59.9k
Accepted
answer
edited Nov '15

Hi Andreas and Jurigag,

it was just the path, sometimes you can't see the forest for the trees :-)

I changed this:

require "app/messages/en.php";

into this:

require "../app/messages/en.php";

Have a nice weekend.

Rgds

Stefan