<?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';
        }

    }

}