php - 语言切换器,如何用变量代替 _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);
}
解决方案
一个快速的解决方法是使用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!
推荐阅读
- odoo-12 - 从员工那里找到生日、结婚纪念日的详细信息并在看板视图中显示
- excel - 标记标准为 1 的顶部和底部范围单元格
- typescript - 在 TS 中的 momentjs 下添加一个函数来键入 Duration
- typo3 - TYPO3 - 如何禁用页面上特定内容元素的缓存?
- excel - 关闭工作簿时“对象变量或未设置块变量”
- php - 如何将数据从数据库调用到语言文件,然后将其调用到刀片文件
- vba - 如何使用 VBA 将 YES/NO 复选框列添加到现有表?
- ios - iOS 设备上未收到 Firebase 推送通知。虽然控制台记录了消息,但它在设备上收到
- c# - 我在这里制造泄漏吗?
- python - view.showMaximized() 在 PyQt5 中不起作用