-
Notifications
You must be signed in to change notification settings - Fork 10
/
lang.php
41 lines (35 loc) · 966 Bytes
/
lang.php
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
<?php
class Langs {
public $data;
public $language;
private $input;
public function __construct(string $file, $default_language = 'en') {
$this->input = $file;
$json = file_get_contents($file);
$this->data = json_decode($json);
$this->language = $default_language;
}
public function __get($key) {
$language = $this->language;
return $this->data->$language->$key ?? $key;
}
public function __call($key, $replacements = []) {
$language = $this->language;
$string = $this->data->$language->$key ?? $key;
return vsprintf($string, $replacements);
}
public function __isset($key) {
$language = $this->language;
return isset($this->data->$language->$key);
}
public function __set($key, $value) {
$language = $this->language;
$this->data->$language->$key = $value;
}
public function save($output = null) {
if (!$output) {
$output = $this->input;
}
return file_put_contents($output, json_encode($this->data));
}
}