Lang.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace App\Mail\lib;
/**
* @time 2022/8/3 9:42
* Class Lang
* @package App\Mail\lib
*/
class Lang {
public static $l;
public static $data;
/**
* @param $key
* @param mixed ...$var
* @return mixed
* @time 2022/8/3 9:41
*/
public static function __($key,...$var){
// 是否加载语言
if (!isset(self::$data[self::$l])) self::load();
// 获取语言
$str = self::$data[self::$l][$key]??self::$data['en'][$key]??$key;
// 数组填充到10个元素,避免出现元素不够而返回空字符
$var = array_pad($var,10,'');
// 替换
return @sprintf($str, ...$var);
}
/**
* 加载
* @param string $l
* @time 2022/8/4 15:55
*/
public static function load(string $l = ''){
self::$l = strtolower($l ? $l : app()->getLocale());
self::$data[self::$l] = require_once __DIR__.'/../lang/'.self::$l.'.php';
// default
if(!isset(self::$data['en'])){
self::$data['en'] = require_once __DIR__.'/../lang/en.php';
}
}
}