Skip to content
Advertisement

PHP MVC: How to add translation strings to views?

In my MVC view files, there exist strings I may have a translation for. In a file with access to the database (the model), I can do:

$Lang->say('Welcome');

Here is what it’s doing:

public function say($string) {

    if (empty(self::$vocabulary)) {
        self::$vocabulary = $this->loadLanguage($this->currentLanguageID()); // Load vocabulary for current language
    }

    if (isset(self::$vocabulary[$string])) {
        return self::$vocabulary[$string];  
    } 

    return $string;

}

I need access to this say() function from within my view. Short of passing the entire vocabulary array to the view, how would I do this?

Advertisement

Answer

What you need to do is require_once your .php file that contains your class. You can then instantiate your class into a object like $Lang and call $Lang->Say() from your view.

For example:

require_once "file_that_holds_class.php";

$Lang = new classNameHere();
$result = $obj->Say("whatever_string_value");

echo $result;

Now you can do whatever it is you need to do with the string.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement