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.

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题