Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ?



21.3k
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



21.3k

Hi Andreas,

thx for your reply :-)

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

Kind Rgds

Stefan



21.3k
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