douyin6188 2017-01-02 02:53
浏览 39
已采纳

PHP MVC:如何向视图添加翻译字符串?

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?

  • 写回答

1条回答 默认 最新

  • dongzhong9055 2017-01-02 03:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?