首页 > 解决方案 > 语言切换器,如何用变量代替 _lang 行的键来回显

问题描述

我喜欢将我的语言行存储在变量或数组中。

我有这个问题。我怎样才能回显$data["menu_production"] 而不是$this->lang->line("menu_production");?当我回显($this->lang->line("menu_production");)但不使用$data或其他变量时,它可以工作。在我的控制器中,我调用了这个函数:

function index() {
  $data["menu_production"] = $this->lang->line("menu_production");
  $this->load->view('menu', $data);
}

标签: phpcodeigniter

解决方案


一个快速的解决方法是使用sprintf

让你的语言文件有一个类似的键

$lang['custom_message'] = "Hello %s!";

然后使用语言类作为

echo sprintf($this->lang->line('custom_message'), 'world');
// output: Hello world!

$name = "John Doe";
echo sprintf($this->lang->line('custom_message'), $name);
// output: Hello John Doe!

推荐阅读