aboutsummaryrefslogtreecommitdiff
path: root/engine/LangData.php
blob: 6f108f2c6de4a19545c0931707cb226fa28f5ace (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

class LangData implements ArrayAccess {

    private static ?LangData $instance = null;
    protected array $data = [];
    protected array $loaded = [];

    public static function getInstance(): static {
        if (is_null(self::$instance)) {
            self::$instance = new self();
            self::$instance->load('en');
        }
        return self::$instance;
    }

    public function __invoke(string $key, ...$args) {
        $val = $this[$key];
        return empty($args) ? $val : sprintf($val, ...$args);
    }

    public function load(string $name) {
        if (array_key_exists($name, $this->loaded))
            return;

        $data = require_once ROOT."/lang/{$name}.php";
        $this->data = array_replace($this->data,
            $data);

        $this->loaded[$name] = true;
    }

    public function offsetSet(mixed $offset, mixed $value): void {
        logError(__METHOD__ . ': not implemented');
    }

    public function offsetExists($offset): bool {
        return isset($this->data[$offset]);
    }

    public function offsetUnset(mixed $offset): void {
        logError(__METHOD__ . ': not implemented');
    }

    public function offsetGet(mixed $offset): mixed {
        return $this->data[$offset] ?? '{' . $offset . '}';
    }

    public function search(string $regexp): array|false {
        return preg_grep($regexp, array_keys($this->data));
    }

    // function plural(array $s, int $n, array $opts = []) {
    //     $opts = array_merge([
    //         'format' => true,
    //         'format_delim' => ' ',
    //         'lang' => 'en',
    //     ], $opts);
    //
    //     switch ($opts['lang']) {
    //         case 'ru':
    //             $n = $n % 100;
    //             if ($n > 19)
    //                 $n %= 10;
    //
    //             if ($n == 1) {
    //                 $word = 0;
    //             } else if ($n >= 2 && $n <= 4) {
    //                 $word = 1;
    //             } else if ($n == 0 && count($s) == 4) {
    //                 $word = 3;
    //             } else {
    //                 $word = 2;
    //             }
    //             break;
    //
    //         default:
    //             if (!$n && count($s) == 4) {
    //                 $word = 3;
    //             } else {
    //                 $word = (int)!!$n;
    //             }
    //             break;
    //     }
    //
    //     // if zero
    //     if ($word == 3)
    //         return $s[3];
    //
    //     if (is_callable($opts['format'])) {
    //         $num = $opts['format']($n);
    //     } else if ($opts['format'] === true) {
    //         $num = formatNumber($n, $opts['format_delim']);
    //     }
    //
    //     return sprintf($s[$word], $num);
    // }
    //
    // function formatNumber(int $num, string $delim = ' ', bool $short = false): string {
    //     if ($short) {
    //         if ($num >= 1000000)
    //             return floor($num / 1000000).'m';
    //         if ($num >= 1000)
    //             return floor($num / 1000).'k';
    //     }
    //     return number_format($num, 0, '.', $delim);
    // }
}