1
This commit is contained in:
26
application/common.php
Normal file
26
application/common.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
if (!function_exists('json_lang')) {
|
||||
/**
|
||||
* 从 JSON 文件中加载语言包
|
||||
*
|
||||
* @param string $key 语言包的键
|
||||
* @param string $lang 语言类型,默认为当前语言
|
||||
* @return mixed 返回对应的语言值
|
||||
*/
|
||||
function json_lang($key, $lang = '') {
|
||||
$lang = \think\facade\Lang::detect();
|
||||
// 拼接文件路径
|
||||
$filePath = '../application/lang' . DIRECTORY_SEPARATOR . $lang . '.json';
|
||||
// 判断文件是否存在
|
||||
if (file_exists($filePath)) {
|
||||
// 读取 JSON 文件并解析为数组
|
||||
$data = json_decode(file_get_contents($filePath), true);
|
||||
if (isset($data[$key])) {
|
||||
return $data[$key];
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到对应的键,则返回原始键
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user