luolongfei /
freenom
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * 语言包加载 |
||||
| 4 | * |
||||
| 5 | * @author mybsdc <[email protected]> |
||||
| 6 | * @date 2020/1/16 |
||||
| 7 | * @time 16:30 |
||||
| 8 | */ |
||||
| 9 | |||||
| 10 | namespace Luolongfei\Lib; |
||||
| 11 | |||||
| 12 | class Lang |
||||
| 13 | { |
||||
| 14 | /** |
||||
| 15 | * @var Lang |
||||
| 16 | */ |
||||
| 17 | protected static $instance; |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * @var array |
||||
| 21 | */ |
||||
| 22 | public $lang; |
||||
| 23 | |||||
| 24 | public function __construct() |
||||
| 25 | { |
||||
| 26 | $this->lang = require sprintf('%s/lang/%s.php', RESOURCES_PATH, config('locale')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
config('locale') can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * @param string $key |
||||
| 31 | * |
||||
| 32 | * @return array|mixed|null |
||||
| 33 | */ |
||||
| 34 | public function get($key = '') |
||||
| 35 | { |
||||
| 36 | $lang = $this->lang; |
||||
| 37 | |||||
| 38 | if (strlen($key)) { |
||||
| 39 | if (strpos($key, '.')) { |
||||
| 40 | $keys = explode('.', $key); |
||||
| 41 | $val = $lang; |
||||
| 42 | foreach ($keys as $k) { |
||||
| 43 | if (!isset($val[$k])) { |
||||
| 44 | return null; // 任一下标不存在就返回null |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | $val = $val[$k]; |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | return $val; |
||||
| 51 | } else { |
||||
| 52 | if (isset($lang[$key])) { |
||||
| 53 | return $lang[$key]; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | return null; |
||||
| 57 | } |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | return $lang; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @return Lang |
||||
| 65 | */ |
||||
| 66 | public static function instance() |
||||
| 67 | { |
||||
| 68 | if (!self::$instance instanceof self) { |
||||
|
0 ignored issues
–
show
|
|||||
| 69 | self::$instance = new self(); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | return self::$instance; |
||||
| 73 | } |
||||
| 74 | } |